0
|
1 /*
|
|
2 * $Id: setutmp.c,v 1.11 2006/07/23 17:30:33 aonoto Exp $
|
|
3 */
|
|
4
|
|
5 /*
|
|
6 * FreeWnn is a network-extensible Kana-to-Kanji conversion system.
|
|
7 * This file is part of FreeWnn.
|
|
8 *
|
|
9 * Copyright Kyoto University Research Institute for Mathematical Sciences
|
|
10 * 1987, 1988, 1989, 1990, 1991, 1992
|
|
11 * Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1999
|
|
12 * Copyright ASTEC, Inc. 1987, 1988, 1989, 1990, 1991, 1992
|
|
13 * Copyright FreeWnn Project 1999, 2000, 2002, 2006
|
|
14 *
|
|
15 * Maintainer: FreeWnn Project
|
|
16 *
|
|
17 * This program is free software; you can redistribute it and/or modify
|
|
18 * it under the terms of the GNU General Public License as published by
|
|
19 * the Free Software Foundation; either version 2 of the License, or
|
|
20 * (at your option) any later version.
|
|
21 *
|
|
22 * This program is distributed in the hope that it will be useful,
|
|
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
25 * GNU General Public License for more details.
|
|
26 *
|
|
27 * You should have received a copy of the GNU General Public License
|
|
28 * along with this program; if not, write to the Free Software
|
|
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
30 */
|
|
31 #ifdef HAVE_CONFIG_H
|
|
32 # include <config.h>
|
|
33 #endif
|
|
34
|
|
35 #include <stdio.h>
|
|
36 #if STDC_HEADERS
|
|
37 # include <string.h>
|
|
38 #else
|
|
39 # if HAVE_STRINGS_H
|
|
40 # include <strings.h>
|
|
41 # endif
|
|
42 # ifdef HAVE_MEMORY_H
|
|
43 # include <memory.h>
|
|
44 # endif
|
|
45 #endif /* STDC_HEADERS */
|
|
46 #include <sys/types.h>
|
|
47 #include <pwd.h>
|
|
48 #if HAVE_FCNTL_H
|
|
49 # include <fcntl.h>
|
|
50 #endif
|
|
51 #if TIME_WITH_SYS_TIME
|
|
52 # include <sys/time.h>
|
|
53 # include <time.h>
|
|
54 #else
|
|
55 # if HAVE_SYS_TIME_H
|
|
56 # include <sys/time.h>
|
|
57 # else
|
|
58 # include <time.h>
|
|
59 # endif /* HAVE_SYS_TIME_H */
|
|
60 #endif /* TIME_WITH_SYS_TIME */
|
|
61 #if HAVE_UNISTD_H
|
|
62 # include <unistd.h>
|
|
63 #endif
|
|
64 #if HAVE_UTMPX_H
|
|
65 # include <utmpx.h>
|
|
66 #endif
|
|
67 #if HAVE_UTMP_H
|
|
68 # include <utmp.h>
|
|
69 #endif
|
|
70 #if !HAVE_UTMPX_H && !HAVE_UTMP_H
|
|
71 # error "No utmp/utmpx header."
|
|
72 #endif
|
|
73
|
|
74 #if HAVE_STRUCT_UTMP_UT_NAME
|
|
75 # define UT_USER ut_name
|
|
76 #elif HAVE_STRUCT_UTMP_UT_USER
|
|
77 # define UT_USER ut_user
|
|
78 #else
|
|
79 # error "No member that indicates user in struct utmp."
|
|
80 #endif
|
|
81
|
|
82 #include "commonhd.h"
|
|
83 #include "sdefine.h"
|
|
84 #include "sheader.h"
|
|
85
|
|
86 #define public
|
|
87
|
|
88 #if USE_UTMP && !HAVE_LIBSPT
|
|
89
|
|
90 /* Set alias macro UTMP_TRADITIONAL */
|
|
91 #if !HAVE_PUTUTXLINE && !HAVE_PUTUTLINE /* && !(defined(BSD) && (BSD >= 199306)) */
|
|
92 # define UTMP_TRADITIONAL 1
|
|
93 #endif
|
|
94
|
|
95 #ifdef UTMP_TRADITIONAL
|
|
96 static struct utmp saveut;
|
|
97 static struct utmp nullut;
|
|
98
|
|
99 static int savslotnum = 0;
|
|
100 static char savttynm[8];
|
|
101 static int suf = 0;
|
|
102
|
|
103 #ifndef _PATH_UTMP
|
|
104 # ifdef UTMP_FILE
|
|
105 # define _PATH_UTMP UTMP_FILE
|
|
106 # else
|
|
107 # define _PATH_UTMP "/etc/utmp"
|
|
108 # endif
|
|
109 #endif
|
|
110
|
|
111 static int
|
|
112 saveutmp_traditional (void)
|
|
113 {
|
|
114 register int utmpFd;
|
|
115 register char *p;
|
|
116
|
|
117 if (suf > 0)
|
|
118 return 0;
|
|
119 suf = -1;
|
|
120
|
|
121 bzero (&nullut, sizeof nullut);
|
|
122 if ((p = ttyname (0)) == NULL)
|
|
123 return -1;
|
|
124 strncpy (savttynm, strrchr (p, '/') + 1, 8);
|
|
125 if (!(savslotnum = ttyslot ()))
|
|
126 return -1;
|
|
127 if ((utmpFd = open (_PATH_UTMP, O_RDONLY, 0)) < 0)
|
|
128 return -1;
|
|
129 lseek (utmpFd, savslotnum * (sizeof saveut), 0);
|
|
130 read (utmpFd, &saveut, sizeof saveut);
|
|
131 close (utmpFd);
|
|
132 strncpy (nullut.ut_line, saveut.ut_line, sizeof(nullut.ut_line));
|
|
133 strncpy (nullut.ut_host, saveut.ut_host, sizeof(nullut.ut_host));
|
|
134 nullut.ut_time = saveut.ut_time;
|
|
135 suf = 1;
|
|
136 return 0;
|
|
137 }
|
|
138
|
|
139 static int
|
|
140 setutmp_traditional (int ttyFd)
|
|
141 {
|
|
142 int utmpFd;
|
|
143 struct utmp ut;
|
|
144 char *p;
|
|
145 int i;
|
|
146
|
|
147 if (suf <= 0)
|
|
148 return -1;
|
|
149 bzero (&ut, sizeof ut);
|
|
150 if ((p = ttyname (ttyFd)) == NULL)
|
|
151 return -1;
|
|
152
|
|
153 if (!strncmp(p, "/dev/", 5))
|
|
154 p += 5;
|
|
155 strncpy (ut.ut_line, p, sizeof (ut.ut_line));
|
|
156 strncpy (ut.UT_USER, getpwuid (getuid ())->pw_name, sizeof(ut.UT_USER));
|
|
157 ut.ut_time = time (0);
|
|
158 strncpy (ut.ut_host, savttynm, 8);
|
|
159 if (!(i = ttyfdslot (ttyFd)))
|
|
160 return -1;
|
|
161 if ((utmpFd = open (_PATH_UTMP, O_RDWR, 0)) < 0)
|
|
162 return -1;
|
|
163 lseek (utmpFd, savslotnum * (sizeof nullut), 0);
|
|
164 write (utmpFd, &nullut, sizeof nullut);
|
|
165 lseek (utmpFd, i * (sizeof ut), 0);
|
|
166 write (utmpFd, &ut, sizeof ut);
|
|
167 close (utmpFd);
|
|
168 return 0;
|
|
169 }
|
|
170
|
|
171 static int
|
|
172 resetutmp_traditional (int ttyFd)
|
|
173 {
|
|
174 int utmpFd;
|
|
175 struct utmp ut;
|
|
176 char *p;
|
|
177 int i;
|
|
178
|
|
179 bzero (&ut, sizeof ut);
|
|
180 if ((p = ttyname (ttyFd)) == NULL)
|
|
181 return -1;
|
|
182 if (!strncmp(p, "/dev/", 5))
|
|
183 p += 5;
|
|
184 strncpy (ut.ut_line, p, sizeof(ut.ut_line));
|
|
185 /* strncpy (ut.ut_line, strrchr (p, '/') + 1, 8); */
|
|
186 ut.ut_time = time (0);
|
|
187 if (!(i = ttyfdslot (ttyFd)))
|
|
188 return -1;
|
|
189 if ((utmpFd = open (_PATH_UTMP, O_RDWR, 0)) < 0)
|
|
190 return -1;
|
|
191 lseek (utmpFd, savslotnum * (sizeof saveut), 0);
|
|
192 write (utmpFd, &saveut, sizeof saveut);
|
|
193 lseek (utmpFd, i * (sizeof ut), 0);
|
|
194 write (utmpFd, &ut, sizeof ut);
|
|
195 close (utmpFd);
|
|
196 return 0;
|
|
197 }
|
|
198 #endif /* UTMP_TRADITIONAL */
|
|
199
|
|
200 static void
|
|
201 build_utid (char *ut_id, char *ut_line, int size)
|
|
202 {
|
|
203 #ifdef DGUX
|
|
204 strncpy (ut_id, &ut_line[3], size);
|
|
205 #else
|
|
206 /* FIXME: ¤³¤ÎÊÕ¤ê¤Î̿̾ˡ§¤¬¤è¤¯Ê¬¤«¤é¤Ê¤¤ */
|
|
207 strncpy (ut_id, &ut_line[2], size);
|
|
208 ut_id[0] = 't';
|
|
209 #endif /* DGUX */
|
|
210 }
|
|
211
|
|
212 public int
|
|
213 saveutmp (void)
|
|
214 {
|
|
215 #if UTMP_TRADITIONAL && !defined(DGUX)
|
|
216 return saveutmp_traditional();
|
|
217 #else
|
|
218 return 0;
|
|
219 #endif
|
|
220 }
|
|
221
|
|
222 public int
|
|
223 setutmp (int ttyFd)
|
|
224 {
|
|
225 #if UTMP_TRADITIONAL
|
|
226 return setutmp_traditional (ttyFd);
|
|
227 #else /* !UTMP_TRADITIONAL */
|
|
228
|
|
229 #if HAVE_PUTUTXLINE
|
|
230 struct utmpx utx;
|
|
231 #endif
|
|
232 struct utmp ut;
|
|
233 int ut_err = 0;
|
|
234 char *p;
|
|
235 /* struct passwd *getpwuid (); */
|
|
236
|
|
237 memset (&ut, 0, sizeof ut);
|
|
238 if ((p = ttyname (ttyFd)) == NULL)
|
|
239 return -1;
|
|
240
|
|
241 if (!strncmp(p, "/dev/", 5))
|
|
242 p += 5;
|
|
243 strncpy (ut.ut_line, p, sizeof (ut.ut_line));
|
|
244 strncpy (ut.UT_USER, getpwuid (getuid ())->pw_name, sizeof(ut.UT_USER));
|
|
245 ut.ut_time = time (0);
|
|
246 #if HAVE_STRUCT_UTMP_UT_ID
|
|
247 build_utid(ut.ut_id, ut.ut_line, 4);
|
|
248 /*
|
|
249 * Maybe systems that does not have struct utmp.ut_id
|
|
250 * does not have utmp.ut_pid / ut_type ...
|
|
251 */
|
|
252 ut.ut_pid = getpid ();
|
|
253 ut.ut_type = USER_PROCESS;
|
|
254 #endif
|
|
255
|
|
256 #if HAVE_PUTUTXLINE
|
|
257 getutmpx (&ut, &utx);
|
|
258 # if ! HAVE_STRUCT_UTMP_UT_ID
|
|
259 /* Assume all struct utmpx has this parameters ... */
|
|
260 build_utid(utx.ut_id, utx.ut_line, 4);
|
|
261 utx.ut_pid = getpid ();
|
|
262 utx.ut_type = USER_PROCESS;
|
|
263 # endif
|
|
264 setutxent (); /* is it necessary? */
|
|
265 getutxid (&utx);
|
|
266 if (pututxline (&utx) == NULL) {
|
|
267 ut_err = 1;
|
|
268 }
|
|
269 endutxent();
|
|
270 #endif /* HAVE_PUTUTXLINE */
|
|
271 #if HAVE_PUTUTLINE
|
|
272 /* Set utmp if we also have utmpx */
|
|
273 if (ut_err != 0) {
|
|
274 setutent (); /* is it necessary? */
|
|
275 getutid (&ut);
|
|
276 if (pututline (&ut) == NULL) {
|
|
277 ut_err = 1;
|
|
278 }
|
|
279 endutent ();
|
|
280 }
|
|
281 #endif /* HAVE_PUTUTLINE */
|
|
282 if (ut_err == 0) {
|
|
283 return 0;
|
|
284 } else {
|
|
285 return -1;
|
|
286 }
|
|
287 #endif /* !UTMP_TRADITIONAL */
|
|
288 }
|
|
289
|
|
290 public int
|
|
291 resetutmp (int ttyFd)
|
|
292 {
|
|
293 #if UTMP_TRADITIONAL
|
|
294 return resetutmp_traditional (ttyFd);
|
|
295 #else /* !UTMP_TRADITIONAL */
|
|
296
|
|
297 #if HAVE_PUTUTXLINE
|
|
298 struct utmpx utx;
|
|
299 #endif
|
|
300 struct utmp ut;
|
|
301 int ut_err = 0;
|
|
302 char *p;
|
|
303 /* struct passwd *getpwuid (); */
|
|
304
|
|
305 memset (&ut, 0, sizeof ut);
|
|
306 if ((p = ttyname (ttyFd)) == NULL)
|
|
307 return -1;
|
|
308 if (!strncmp(p, "/dev/", 5))
|
|
309 p += 5;
|
|
310 strncpy (ut.ut_line, p, sizeof (ut.ut_line));
|
|
311 /* strncpy (ut.ut_line, strrchr (p, '/') + 1, 12); */
|
|
312 strncpy (ut.UT_USER, getpwuid (getuid ())->pw_name, sizeof(ut.UT_USER));
|
|
313 ut.ut_time = time (0);
|
|
314 #if HAVE_STRUCT_UTMP_UT_ID
|
|
315 build_utid(ut.ut_id, ut.ut_line, 4);
|
|
316 /*
|
|
317 * Maybe systems that does not have struct utmp.ut_id
|
|
318 * does not have utmp.ut_pid / ut_type ...
|
|
319 */
|
|
320 ut.ut_pid = getpid ();
|
|
321 ut.ut_type = DEAD_PROCESS; /* not sure */
|
|
322 #endif
|
|
323
|
|
324 #if HAVE_PUTUTXLINE
|
|
325 getutmpx (&ut, &utx);
|
|
326 # if ! HAVE_STRUCT_UTMP_UT_ID
|
|
327 /* Assume all struct utmpx has this parameters ... */
|
|
328 build_utid(utx.ut_id, utx.ut_line, 4);
|
|
329 utx.ut_pid = getpid ();
|
|
330 utx.ut_type = DEAD_PROCESS; /* not sure */
|
|
331 # endif
|
|
332 setutxent (); /* is it necessary? */
|
|
333 getutxid (&utx);
|
|
334 if (pututxline (&utx) == NULL) {
|
|
335 ut_err = 1;
|
|
336 }
|
|
337 endutxent();
|
|
338 #endif /* HAVE_PUTUTXLINE */
|
|
339 #if HAVE_PUTUTLINE
|
|
340 /* Set utmp if we also have utmpx */
|
|
341 if (ut_err != 0) {
|
|
342 setutent (); /* is it necessary? */
|
|
343 getutid (&ut);
|
|
344 if (pututline (&ut) == NULL) {
|
|
345 ut_err = 1;
|
|
346 }
|
|
347 endutent ();
|
|
348 }
|
|
349 #endif /* HAVE_PUTUTLINE */
|
|
350 if (ut_err == 0) {
|
|
351 return 0;
|
|
352 } else {
|
|
353 return -1;
|
|
354 }
|
|
355 return 0;
|
|
356 #endif /* !UTMP_TRADITIONAL */
|
|
357 }
|
|
358
|
|
359 #else /* ! USE_UTMP */
|
|
360 /* We don't use setutmp feature. Set dummy function. */
|
|
361 public int
|
|
362 saveutmp (void)
|
|
363 {
|
|
364 return 1;
|
|
365 }
|
|
366
|
|
367 public int
|
|
368 setutmp (int ttyFd)
|
|
369 {
|
|
370 return 1;
|
|
371 }
|
|
372
|
|
373 public int
|
|
374 resetutmp (int ttyFd)
|
|
375 {
|
|
376 return 1;
|
|
377 }
|
|
378 #endif /* ! USE_UTMP */
|