'How to programmatically get the number of users logged in on a linux machine?

I was wondering if it was possible to programmatically get the number of users logged in on a Linux machine in C? I did some researching and found out about utmp.h but since not all programs use utmp logging, I did not think it would be accurate enough. Thanks in advance to anyone willing to help.

EDIT: I apologize guys for not being more specific but when I say logged in users, I am referring to any logged in via shell. Basically what you get when you run the who command with no command line arguments.



Solution 1:[1]

#include <utmp.h>
#include <err.h>

#define NAME_WIDTH  8

    FILE *ufp;
    int numberOfUsers = 0;
    struct utmp usr;
    ufp = file(_PATH_UTMP);
    while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1) {
    if (*usr.ut_name && *usr.ut_line && *usr.ut_line != '~') {
         numberOfUsers++;
        }
    }

    FILE *file(char *name)
    {
        FILE *ufp;

        if (!(ufp = fopen(name, "r"))) {
            err(1, "%s", name);
        }
        return(ufp);
    }

After a couple of days playing around with utmp, I figured it out. Thanks for the help guys.

Solution 2:[2]

You're targeting Linux, and you say you want to do what who does. If your software is not going to be distributed or is GPL licensed, you can just crib from the open source implementation of who that is running on your system.

But those are quite the restrictions. So, how can you find where to start without consulting source code? You can get a pretty good idea of where to look by running nm on the binary using nm `which who`. who calls very few external functions (34 under Mac OS X 10.6.4). The functions you are looking for must be among these. Likely candidates are getutxent, utmpxname, and getpwuid. You can check the man pages to validate this guess.

But first, why not try apropos/man -k? A quick search for "users" shows up the users utility, which just lists the logged-in users. (Note: This seems to be a BSDism, so you might not have it under Linux. Quick searches with apropos for relevant tools and functions are still a good idea.) users calls even fewer external functions (only 15), and of those, the only interesting one that overlaps with the interesting functions called by who is getutxent.

So, how about trying getutxent?

Solution 3:[3]

The system call getutent is guaranteed to return a utmp like record regardless of the internal implementation. So you can rely upon that api.

Solution 4:[4]

utmp is your friend.

man 5 utmp

Solution 5:[5]

Not a C programmer, so I can't help in that arena, but can you have your program execute shell commands?

who | awk -F' ' '{print $1}' | sort -u | wc -l

Solution 6:[6]

I'm pretty sure that regardless of utmp logging, you'll still have all the user information you want from that call.

What programs are you concerned about that don't use utmp logging as your linux may (or may not) use utmp logging.

And, as a commenter noted, what do you call 'logged in'?

Solution 7:[7]

In bash:

$ ps aux | cut -d' ' -f1 | sort -d | uniq | wc -l

Solution 8:[8]

#include <stdio.h>
#include <utmp.h>
#include <string.h>
#define STREQ(A,B) (0==strcmp((A),(B)))

int main (int argc, char *argv[])
{
    struct utmp *u;
    int count = 0;
    setutent();
    while ((u = getutent()) != NULL) {
        if (u->ut_line[0] == '~') continue;
        if (u->ut_user[0] && (!STREQ(u->ut_user, "LOGIN")) ) {
            count++;
        }
    }
    endutent();
    printf("%d\n", count);
    return 0;
}

Solution 9:[9]

linux shell command "who" maybe help you.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Error1f1f
Solution 2
Solution 3 ennuikiller
Solution 4 Brad
Solution 5 Alison R.
Solution 6
Solution 7 pr1268
Solution 8 vyom
Solution 9 Tony