annotate xwd-utmp-utmpx.c @ 19:f0accfc74f7b

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