开发者

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 com开发者_如何学Gomand line arguments.


#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.


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?


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


utmp is your friend.

man 5 utmp


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


#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;
}


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'?


In bash:

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


linux shell command "who" maybe help you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜