This repository has been archived by the owner on Sep 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
session.c
83 lines (69 loc) · 1.54 KB
/
session.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "physlock.h"
#include <paths.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <unistd.h>
#include <errno.h>
#include <utmp.h>
#if HAVE_SYSTEMD
#include <systemd/sd-login.h>
#elif HAVE_ELOGIND
#include <elogind/sd-login.h>
#endif
#if HAVE_SYSTEMD || HAVE_ELOGIND
int get_user_logind(userinfo_t *uinfo, int vt) {
int ret = -1, i, n;
char **sessions = NULL;
unsigned int sess_vt;
uid_t sess_uid;
n = sd_get_sessions(&sessions);
for (i = 0; i < n; i++) {
if (sd_session_get_vt(sessions[i], &sess_vt) < 0)
continue;
if (sess_vt == (unsigned)vt) {
if (sd_session_get_uid(sessions[i], &sess_uid) < 0)
continue;
get_user_by_id(uinfo, sess_uid);
ret = 0;
break;
}
}
for (i = 0; i < n; i++)
free(sessions[i]);
free(sessions);
return ret;
}
#else
int get_user_logind(userinfo_t *uinfo, int vt) {
(void)uinfo;
(void)vt;
return -1;
}
#endif
int get_user_utmp(userinfo_t *uinfo, int vt) {
int ret = -1;
FILE *uf;
struct utmp r;
char tty[UT_LINESIZE+1], name[UT_NAMESIZE+1];
name[0] = '\0';
while ((uf = fopen(_PATH_UTMP, "r")) == NULL && errno == EINTR);
if (uf != NULL) {
snprintf(tty, sizeof(tty), "tty%d", vt);
while (!feof(uf) && !ferror(uf)) {
if (fread(&r, sizeof(r), 1, uf) != 1)
continue;
if (r.ut_type != USER_PROCESS || r.ut_user[0] == '\0')
continue;
if (strncmp(r.ut_line, tty, UT_LINESIZE) == 0) {
strncpy(name, r.ut_user, UT_NAMESIZE);
name[UT_NAMESIZE] = '\0';
get_user_by_name(uinfo, name);
ret = 0;
break;
}
}
fclose(uf);
}
return ret;
}