Mercurial > projects > xwrited
comparison xwrited-utmp-utmpx.c @ 0:52694b49dcc4
Initial revision
author | Guido Berhoerster <guido+xwrited@berhoerster.name> |
---|---|
date | Sun, 27 Apr 2014 23:07:51 +0200 |
parents | |
children | 4a5330979433 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:52694b49dcc4 |
---|---|
1 /* | |
2 * Copyright (C) 2010 Guido Berhoerster <guido+xwrited@berhoerster.name> | |
3 * | |
4 * Permission is hereby granted, free of charge, to any person obtaining | |
5 * a copy of this software and associated documentation files (the | |
6 * "Software"), to deal in the Software without restriction, including | |
7 * without limitation the rights to use, copy, modify, merge, publish, | |
8 * distribute, sublicense, and/or sell copies of the Software, and to | |
9 * permit persons to whom the Software is furnished to do so, subject to | |
10 * the following conditions: | |
11 * | |
12 * The above copyright notice and this permission notice shall be included | |
13 * in all copies or substantial portions of the Software. | |
14 * | |
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
22 */ | |
23 | |
24 #define _XOPEN_SOURCE 600 | |
25 | |
26 #include <glib.h> | |
27 #include <stdlib.h> | |
28 #include <string.h> | |
29 #include <unistd.h> | |
30 #include <pwd.h> | |
31 #include <utmpx.h> | |
32 #include <errno.h> | |
33 #include <sys/time.h> | |
34 | |
35 #define DEV_PREFIX "/dev/" | |
36 | |
37 static void | |
38 utmp_write_entry(int fd, gboolean add) | |
39 { | |
40 struct utmpx utmpx; | |
41 char *line = NULL; | |
42 size_t line_len; | |
43 char *id; | |
44 struct passwd *pwd; | |
45 | |
46 line = ptsname(fd); | |
47 if (line == NULL) { | |
48 g_critical("failed to obtain slave pty name"); | |
49 return; | |
50 } | |
51 if (g_str_has_prefix(line, DEV_PREFIX)) { | |
52 line += strlen(DEV_PREFIX); | |
53 } | |
54 | |
55 line_len = strlen(line); | |
56 id = (line_len >= sizeof (utmpx.ut_pid)) ? | |
57 line + (line_len - sizeof (utmpx.ut_pid)) : | |
58 line; | |
59 | |
60 pwd = getpwuid(getuid()); | |
61 if (pwd == NULL) { | |
62 g_critical("failed to get username: %s", g_strerror(errno)); | |
63 return; | |
64 } | |
65 | |
66 memset(&utmpx, 0, sizeof (utmpx)); | |
67 strncpy(utmpx.ut_name, pwd->pw_name, sizeof (utmpx.ut_name)); | |
68 strncpy(utmpx.ut_id, id, sizeof (utmpx.ut_id)); | |
69 strncpy(utmpx.ut_line, line, sizeof (utmpx.ut_line)); | |
70 utmpx.ut_pid = getpid(); | |
71 utmpx.ut_type = add ? USER_PROCESS : DEAD_PROCESS; | |
72 gettimeofday(&utmpx.ut_tv, NULL); | |
73 | |
74 setutxent(); | |
75 if (pututxline(&utmpx) == NULL) { | |
76 g_critical("failed to write to utmpx database: %s", | |
77 g_strerror(errno)); | |
78 return; | |
79 } | |
80 endutxent(); | |
81 } | |
82 | |
83 void | |
84 xwrited_utmp_add_entry(int fd) | |
85 { | |
86 utmp_write_entry(fd, TRUE); | |
87 } | |
88 | |
89 void | |
90 xwrited_utmp_remove_entry(int fd) | |
91 { | |
92 utmp_write_entry(fd, FALSE); | |
93 } |