]> git.saurik.com Git - apple/libc.git/blob - gen/NetBSD/utmpx.c.patch
Libc-391.4.1.tar.gz
[apple/libc.git] / gen / NetBSD / utmpx.c.patch
1 --- utmpx.c.orig 2004-07-13 13:02:37.000000000 -0700
2 +++ utmpx.c 2004-08-05 15:25:10.000000000 -0700
3 @@ -50,7 +50,6 @@
4 #include <sys/wait.h>
5
6 #include <assert.h>
7 -#include <db.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <stdio.h>
11 @@ -63,6 +62,13 @@
12 #include <utmpx.h>
13 #include <vis.h>
14
15 +#ifdef UTMP_COMPAT
16 +#include <utmp.h>
17 +#include <ttyent.h>
18 +
19 +static void _utmp_compat(const struct utmpx *);
20 +#endif /* UTMP_COMPAT */
21 +
22 __warn_references(getlastlogx,
23 "warning: reference to compatibility getlastlogx(); include <utmpx.h> for correct reference")
24 __warn_references(lastlogxname,
25 @@ -72,7 +78,6 @@
26 static int readonly = 0;
27 static struct utmpx ut;
28 static char utfile[MAXPATHLEN] = _PATH_UTMPX;
29 -static char llfile[MAXPATHLEN] = _PATH_LASTLOGX;
30
31 static struct utmpx *utmp_update(const struct utmpx *);
32
33 @@ -270,6 +275,9 @@
34 goto fail;
35
36 u = memcpy(&ut, &temp, sizeof(ut));
37 +#ifdef UTMP_COMPAT
38 + _utmp_compat(u);
39 +#endif /* UTMP_COMPAT */
40 fail:
41 if (gotlock) {
42 if (lockf(fileno(fp), F_ULOCK, (off_t)0) == -1)
43 @@ -308,185 +316,50 @@
44
45 }
46
47 -/*
48 - * The following are extensions and not part of the X/Open spec.
49 - */
50 -int
51 -updwtmpx(const char *file, const struct utmpx *utx)
52 -{
53 - int fd;
54 - int saved_errno;
55 -
56 - _DIAGASSERT(file != NULL);
57 - _DIAGASSERT(utx != NULL);
58 -
59 - fd = open(file, O_WRONLY|O_APPEND|O_SHLOCK);
60 -
61 - if (fd == -1) {
62 - if ((fd = open(file, O_CREAT|O_WRONLY|O_EXLOCK, 0644)) == -1)
63 - return -1;
64 - (void)memset(&ut, 0, sizeof(ut));
65 - ut.ut_type = SIGNATURE;
66 - (void)memcpy(ut.ut_user, vers, sizeof(vers));
67 - if (write(fd, &ut, sizeof(ut)) == -1)
68 - goto failed;
69 +#ifdef UTMP_COMPAT
70 +static void
71 +_utmp_compat(const struct utmpx *ux)
72 +{
73 + struct utmp u;
74 + int fd, slot;
75 + struct ttyent *ttyp;
76 +
77 + switch (ux->ut_type) {
78 + case INIT_PROCESS:
79 + case LOGIN_PROCESS:
80 + case USER_PROCESS:
81 + break;
82 + case DEAD_PROCESS:
83 + logout(ux->ut_line);
84 + return;
85 + default:
86 + return;
87 }
88 - if (write(fd, utx, sizeof(*utx)) == -1)
89 - goto failed;
90 - if (close(fd) == -1)
91 - return -1;
92 - return 0;
93 -
94 - failed:
95 - saved_errno = errno;
96 - (void) close(fd);
97 - errno = saved_errno;
98 - return -1;
99 -}
100 -
101 -
102 -int
103 -utmpxname(const char *fname)
104 -{
105 - size_t len;
106 -
107 - _DIAGASSERT(fname != NULL);
108 -
109 - len = strlen(fname);
110 -
111 - if (len >= sizeof(utfile))
112 - return 0;
113 -
114 - /* must end in x! */
115 - if (fname[len - 1] != 'x')
116 - return 0;
117 -
118 - (void)strlcpy(utfile, fname, sizeof(utfile));
119 - endutxent();
120 - return 1;
121 -}
122 -
123 -
124 -void
125 -getutmp(const struct utmpx *ux, struct utmp *u)
126 -{
127 -
128 - _DIAGASSERT(ux != NULL);
129 - _DIAGASSERT(u != NULL);
130 -
131 - (void)memcpy(u->ut_name, ux->ut_name, sizeof(u->ut_name));
132 - (void)memcpy(u->ut_line, ux->ut_line, sizeof(u->ut_line));
133 - (void)memcpy(u->ut_host, ux->ut_host, sizeof(u->ut_host));
134 - u->ut_time = ux->ut_tv.tv_sec;
135 -}
136 -
137 -void
138 -getutmpx(const struct utmp *u, struct utmpx *ux)
139 -{
140 -
141 - _DIAGASSERT(ux != NULL);
142 - _DIAGASSERT(u != NULL);
143 -
144 - (void)memcpy(ux->ut_name, u->ut_name, sizeof(u->ut_name));
145 - (void)memcpy(ux->ut_line, u->ut_line, sizeof(u->ut_line));
146 - (void)memcpy(ux->ut_host, u->ut_host, sizeof(u->ut_host));
147 - ux->ut_tv.tv_sec = u->ut_time;
148 - ux->ut_tv.tv_usec = 0;
149 - (void)memset(&ux->ut_ss, 0, sizeof(ux->ut_ss));
150 - ux->ut_pid = 0;
151 - ux->ut_type = USER_PROCESS;
152 - ux->ut_session = 0;
153 - ux->ut_exit.e_termination = 0;
154 - ux->ut_exit.e_exit = 0;
155 -}
156 -
157 -int
158 -lastlogxname(const char *fname)
159 -{
160 - size_t len;
161 -
162 - _DIAGASSERT(fname != NULL);
163 -
164 - len = strlen(fname);
165 -
166 - if (len >= sizeof(llfile))
167 - return 0;
168 -
169 - /* must end in x! */
170 - if (fname[len - 1] != 'x')
171 - return 0;
172 -
173 - (void)strlcpy(llfile, fname, sizeof(llfile));
174 - return 1;
175 -}
176 -
177 -struct lastlogx *
178 -getlastlogx(uid_t uid, struct lastlogx *ll)
179 -{
180 -
181 - return __getlastlogx13(_PATH_LASTLOGX, uid, ll);
182 -}
183 -
184 -struct lastlogx *
185 -__getlastlogx13(const char *fname, uid_t uid, struct lastlogx *ll)
186 -{
187 - DBT key, data;
188 - DB *db;
189 -
190 - _DIAGASSERT(fname != NULL);
191 - _DIAGASSERT(ll != NULL);
192 -
193 - db = dbopen(fname, O_RDONLY|O_SHLOCK, 0, DB_HASH, NULL);
194 -
195 - if (db == NULL)
196 - return NULL;
197 -
198 - key.data = &uid;
199 - key.size = sizeof(uid);
200 -
201 - if ((db->get)(db, &key, &data, 0) != 0)
202 - goto error;
203 -
204 - if (data.size != sizeof(*ll)) {
205 - errno = EFTYPE;
206 - goto error;
207 + /* do equivalent of ttyslot(), but using ux->ut_slot */
208 + setttyent();
209 + slot = 1;
210 + for(;;) {
211 + if ((ttyp = getttyent()) == NULL) {
212 + endttyent();
213 + return;
214 + }
215 + if (!strcmp(ttyp->ty_name, ux->ut_line)) {
216 + endttyent();
217 + break;
218 + }
219 + slot++;
220 }
221
222 - if (ll == NULL)
223 - if ((ll = malloc(sizeof(*ll))) == NULL)
224 - goto done;
225 -
226 - (void)memcpy(ll, data.data, sizeof(*ll));
227 - goto done;
228 -error:
229 - ll = NULL;
230 -done:
231 - (db->close)(db);
232 - return ll;
233 -}
234 -
235 -int
236 -updlastlogx(const char *fname, uid_t uid, struct lastlogx *ll)
237 -{
238 - DBT key, data;
239 - int error = 0;
240 - DB *db;
241 -
242 - _DIAGASSERT(fname != NULL);
243 - _DIAGASSERT(ll != NULL);
244 -
245 - db = dbopen(fname, O_RDWR|O_CREAT|O_EXLOCK, 0, DB_HASH, NULL);
246 -
247 - if (db == NULL)
248 - return -1;
249 -
250 - key.data = &uid;
251 - key.size = sizeof(uid);
252 - data.data = ll;
253 - data.size = sizeof(*ll);
254 - if ((db->put)(db, &key, &data, 0) != 0)
255 - error = -1;
256 -
257 - (db->close)(db);
258 - return error;
259 + /* now write utmp */
260 + (void)memset(&u, 0, sizeof(u));
261 + strncpy(u.ut_line, ux->ut_line, UT_LINESIZE);
262 + strncpy(u.ut_name, ux->ut_user, UT_NAMESIZE);
263 + strncpy(u.ut_host, ux->ut_host, UT_HOSTSIZE);
264 + u.ut_time = ux->ut_tv.tv_sec;
265 + if ((fd = open(_PATH_UTMP, O_WRONLY|O_CREAT, 0644)) >= 0) {
266 + (void)lseek(fd, (off_t)(slot * sizeof(struct utmp)), L_SET);
267 + (void)write(fd, &u, sizeof(struct utmp));
268 + (void)close(fd);
269 + }
270 }
271 +#endif /* UTMP_COMPAT */