comparison 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
comparison
equal deleted inserted replaced
18:4a5330979433 19:f0accfc74f7b
1 /*
2 * Copyright (C) 2010 Guido Berhoerster <guido+xwd@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 #include <glib.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <pwd.h>
29 #include <utmpx.h>
30 #include <errno.h>
31 #include <sys/time.h>
32
33 #ifndef DEV_PREFIX
34 #define DEV_PREFIX "/dev/"
35 #endif /* !DEV_PREFIX */
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 xwd_utmp_add_entry(int fd)
85 {
86 utmp_write_entry(fd, TRUE);
87 }
88
89 void
90 xwd_utmp_remove_entry(int fd)
91 {
92 utmp_write_entry(fd, FALSE);
93 }