相关疑难解决方法(0)

如何在 Linux 中检测通过 GUI 登录的用户

我想在我的程序中捕获通过 GUI 登录的用户名。我的程序从 root 登录作为守护进程运行。如果非 root 用户通过 GUI 登录,则应通知我的程序。我正在粘贴我当前的程序,它调用一个 perl 脚本,利用系统调用来检查谁是当前登录的用户。我也在粘贴我的 perl 脚本以供参考。

#include <X11/Xlib.h>
#include <X11/Xos.h>
#include <X11/Xfuncs.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
int main()
{
    char *user;
    char buf[1024];
    int fd, ret;
    fd = open("/tmp/log", O_TRUNC|O_RDWR|O_CREAT);
    if (!fd) {
        printf("Error opening file\n");
        exit(1);
    }
    chmod("/tmp/log", S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP
            | S_IROTH | S_IWOTH | S_IXOTH);
    daemon(0, 0);
    while (1) {
        system("perl /home/curr-usr.pl");
        sleep(5);
    } 
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

用于获取当前用户登录的 perl 脚本。

#!/usr/bin/perl
my …
Run Code Online (Sandbox Code Playgroud)

c linux gnome xlib

2
推荐指数
1
解决办法
4367
查看次数

标签 统计

c ×1

gnome ×1

linux ×1

xlib ×1