Tizen 2.1 base
[platform/upstream/sysvinit.git] / src / utmp.c
1 /*
2  * utmp.c       Routines to read/write the utmp and wtmp files.
3  *              Basically just wrappers around the library routines.
4  *
5  * Version:     @(#)utmp.c  2.77  09-Jun-1999  miquels@cistron.nl
6  *
7  */
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <sys/ioctl.h>
11 #include <sys/utsname.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <errno.h>
15 #include <stdio.h>
16 #include <time.h>
17 #include <fcntl.h>
18 #include <string.h>
19 #include <utmp.h>
20
21 #include "init.h"
22 #include "initreq.h"
23 #include "paths.h"
24
25
26 #if defined(__GLIBC__)
27 #  if (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0) && defined(__powerpc__)
28 #    define HAVE_UPDWTMP 0
29 #  else
30 #    define HAVE_UPDWTMP 1
31 #  endif
32 #else
33 #  define HAVE_UPDWTMP 0
34 #endif
35
36
37 /*
38  *      Log an event in the wtmp file (reboot, runlevel)
39  */
40 void write_wtmp(
41 char *user,                     /* name of user */
42 char *id,                       /* inittab ID */
43 int pid,                        /* PID of process */
44 int type,                       /* TYPE of entry */
45 char *line)                     /* Which line is this */
46 {
47         int fd;
48         struct utmp utmp;
49         struct utsname uname_buf;
50         struct timeval tv;
51         
52         /*
53          *      Try to open the wtmp file. Note that we even try
54          *      this if we have updwtmp() so we can see if the
55          *      wtmp file is accessible.
56          */
57         if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND)) < 0) return;
58
59 #ifdef INIT_MAIN
60         /*
61          *      Note if we are going to write a boot record.
62          */
63         if (type == BOOT_TIME) wrote_wtmp_reboot++;
64
65         /*
66          *      See if we need to write a reboot record. The reason that
67          *      we are being so paranoid is that when we first tried to
68          *      write the reboot record, /var was possibly not mounted
69          *      yet. As soon as we can open WTMP we write a delayed boot record.
70          */
71         if (wrote_wtmp_reboot == 0 && type != BOOT_TIME)
72                 write_wtmp("reboot", "~~", 0, BOOT_TIME, "~");
73 #endif
74
75         /*
76          *      Zero the fields and enter new fields.
77          */
78         memset(&utmp, 0, sizeof(utmp));
79 #if defined(__GLIBC__)
80         gettimeofday(&tv, NULL);
81         utmp.ut_tv.tv_sec = tv.tv_sec;
82         utmp.ut_tv.tv_usec = tv.tv_usec;
83 #else
84         time(&utmp.ut_time);
85 #endif
86         utmp.ut_pid  = pid;
87         utmp.ut_type = type;
88         strncpy(utmp.ut_name, user, sizeof(utmp.ut_name));
89         strncpy(utmp.ut_id  , id  , sizeof(utmp.ut_id  ));
90         strncpy(utmp.ut_line, line, sizeof(utmp.ut_line));
91         
92         /* Put the OS version in place of the hostname */
93         if (uname(&uname_buf) == 0)
94                 strncpy(utmp.ut_host, uname_buf.release, sizeof(utmp.ut_host));
95
96 #if HAVE_UPDWTMP
97         updwtmp(WTMP_FILE, &utmp);
98 #else
99         write(fd, (char *)&utmp, sizeof(utmp));
100 #endif
101         close(fd);
102 }
103
104 /*
105  *      Write an entry to the UTMP file. For DEAD_PROCESS, put
106  *      the previous ut_line into oldline if oldline != NULL.
107  */
108 static void write_utmp(
109 char *user,                     /* name of user */
110 char *id,                       /* inittab ID */
111 int pid,                        /* PID of process */
112 int type,                       /* TYPE of entry */
113 char *line,                     /* LINE if used. */
114 char *oldline)                  /* Line of old utmp entry. */
115 {
116         struct utmp utmp;
117         struct utmp tmp;
118         struct utmp *utmptr;
119         struct timeval tv;
120
121         /*
122          *      Can't do much if UTMP_FILE is not present.
123          */
124         if (access(UTMP_FILE, F_OK) < 0)
125                 return;
126
127 #ifdef INIT_MAIN
128         /*
129          *      Note if we are going to write a boot record.
130          */
131         if (type == BOOT_TIME) wrote_utmp_reboot++;
132
133         /*
134          *      See if we need to write a reboot record. The reason that
135          *      we are being so paranoid is that when we first tried to
136          *      write the reboot record, /var was possibly not mounted
137          *      yet. As soon as we can open WTMP we write a delayed boot record.
138          */
139         if (wrote_utmp_reboot == 0 && type != BOOT_TIME)
140                 write_utmp("reboot", "~~", 0, BOOT_TIME, "~", NULL);
141 #endif
142
143         /*
144          *      Fill out an utmp struct.
145          */
146         memset(&utmp, 0, sizeof(utmp));
147         utmp.ut_type = type;
148         utmp.ut_pid = pid;
149         strncpy(utmp.ut_id, id, sizeof(utmp.ut_id));
150 #if defined(__GLIBC__)
151         gettimeofday(&tv, NULL);
152         utmp.ut_tv.tv_sec = tv.tv_sec;
153         utmp.ut_tv.tv_usec = tv.tv_usec;
154 #else
155         time(&utmp.ut_time);
156 #endif
157         strncpy(utmp.ut_user, user, UT_NAMESIZE);
158         if (line) strncpy(utmp.ut_line, line, UT_LINESIZE);
159         
160         /*
161          *      We might need to find the existing entry first, to
162          *      find the tty of the process (for wtmp accounting).
163          */
164         if (type == DEAD_PROCESS) {
165                 /*
166                  *      Find existing entry for the tty line.
167                  */
168                 setutent();
169                 tmp = utmp;
170                 if ((utmptr = getutid(&tmp)) != NULL) {
171                         strncpy(utmp.ut_line, utmptr->ut_line, UT_LINESIZE);
172                         if (oldline)
173                                 strncpy(oldline, utmptr->ut_line, UT_LINESIZE);
174                 }
175         }
176
177         /*
178          *      Update existing utmp file.
179          */
180         setutent();
181         pututline(&utmp);
182         endutent();
183 }
184
185 /*
186  *      Write a record to both utmp and wtmp.
187  */
188 void write_utmp_wtmp(
189 char *user,                     /* name of user */
190 char *id,                       /* inittab ID */
191 int pid,                        /* PID of process */
192 int type,                       /* TYPE of entry */
193 char *line)                     /* LINE if used. */
194 {
195         char    oldline[UT_LINESIZE];
196
197         /*
198          *      For backwards compatibility we just return
199          *      if user == NULL (means : clean up utmp file).
200          */
201         if (user == NULL)
202                 return;
203
204         oldline[0] = 0;
205         write_utmp(user, id, pid, type, line, oldline);
206         write_wtmp(user, id, pid, type, line && line[0] ? line : oldline);
207 }
208