OSDN Git Service

add more test dirs, touchup output, and some other misc touchups
[uclinux-h8/uClibc.git] / libutil / logwtmp.c
1 /* wtmp support rubbish (i.e. complete crap)
2
3    Written by Erik Andersen <andersee@debian.org> 
4
5    The GNU C Library is free software
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #include <string.h>
22 #include <sys/time.h>
23 #include <time.h>
24 #include <unistd.h>
25 #include <utmp.h>
26 #include <fcntl.h>
27 #include <sys/file.h>
28
29
30 void logwtmp (const char *line, const char *name, const char *host)
31 {
32     struct utmp lutmp;
33     memset (&(lutmp), 0, sizeof (struct utmp));
34
35     lutmp.ut_type = (name && *name)? USER_PROCESS : DEAD_PROCESS;
36     lutmp.ut_pid = getpid();
37     strncpy(lutmp.ut_line, line, sizeof(lutmp.ut_line)-1);
38     strncpy(lutmp.ut_name, name, sizeof(lutmp.ut_name)-1);
39     strncpy(lutmp.ut_host, host, sizeof(lutmp.ut_host)-1);
40 #if __WORDSIZE_COMPAT32 == 0
41     gettimeofday(&(lutmp.ut_tv), NULL);
42 #else
43     {
44       struct timeval tv;
45       gettimeofday (&tv, NULL);
46       lutmp.ut_tv.tv_sec = tv.tv_sec;
47       lutmp.ut_tv.tv_usec = tv.tv_usec;
48     }
49 #endif
50
51     updwtmp(_PATH_WTMP, &(lutmp));
52 }
53
54 #if 0
55 /* This is enabled in uClibc/libc/misc/utmp/wtent.c */
56 extern void updwtmp(const char *wtmp_file, const struct utmp *lutmp)
57 {
58     int fd;
59
60     fd = open(wtmp_file, O_APPEND | O_WRONLY, 0);
61     if (fd >= 0) {
62         if (lockf(fd, F_LOCK, 0)==0) {
63             write(fd, (const char *) lutmp, sizeof(struct utmp));
64             lockf(fd, F_ULOCK, 0);
65             close(fd);
66         }
67     }
68 }
69 #endif