1 | /*
|
---|
2 | log - log the shutdown's and the halt's
|
---|
3 |
|
---|
4 | Author: Edvard Tuinder <v892231@si.hhs.NL>
|
---|
5 |
|
---|
6 | shutdown is logged in /usr/adm/wtmp and in /usr/adm/log (if desired)
|
---|
7 | halt is logged only in /usr/adm/wtmp as `halt' to prevent last from
|
---|
8 | reporting halt's as crashes.
|
---|
9 |
|
---|
10 | */
|
---|
11 |
|
---|
12 | #define _POSIX_SOURCE 1
|
---|
13 | #include <sys/types.h>
|
---|
14 | #include <stdio.h>
|
---|
15 | #include <utmp.h>
|
---|
16 | #include <pwd.h>
|
---|
17 | #include <fcntl.h>
|
---|
18 | #include <time.h>
|
---|
19 | #include <string.h>
|
---|
20 | #include <unistd.h>
|
---|
21 | #include <sys/utsname.h>
|
---|
22 | #undef WTMP
|
---|
23 |
|
---|
24 | static char WTMP[] = "/usr/adm/wtmp"; /* Record of logins and logouts. */
|
---|
25 | static char SHUT_LOG[] = "/usr/adm/log";
|
---|
26 |
|
---|
27 | char who[8];
|
---|
28 | extern char *prog;
|
---|
29 | static char *month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
---|
30 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
---|
31 |
|
---|
32 | void write_log _ARGS(( void ));
|
---|
33 |
|
---|
34 | void write_log()
|
---|
35 | {
|
---|
36 | int fd;
|
---|
37 | static struct utmp wtmp;
|
---|
38 | static struct passwd *pwd;
|
---|
39 | char mes[90];
|
---|
40 | struct tm *tm;
|
---|
41 | time_t now;
|
---|
42 | struct utsname utsname;
|
---|
43 | char *host = "localhost";
|
---|
44 |
|
---|
45 | time(&now);
|
---|
46 | tm = localtime(&now);
|
---|
47 |
|
---|
48 | if (uname(&utsname) >= 0) host = utsname.nodename;
|
---|
49 |
|
---|
50 | pwd = getpwuid(getuid());
|
---|
51 | if (pwd == (struct passwd *)0)
|
---|
52 | strcpy (who,"root");
|
---|
53 | else
|
---|
54 | strcpy (who,pwd->pw_name);
|
---|
55 | fd = open(WTMP,O_APPEND|O_WRONLY,1);
|
---|
56 | if (fd) {
|
---|
57 | if (strcmp(prog,"reboot"))
|
---|
58 | strcpy (wtmp.ut_user, prog);
|
---|
59 | else
|
---|
60 | strcpy (wtmp.ut_user, "shutdown"); /* last ... */
|
---|
61 | strcpy (wtmp.ut_id, "~~");
|
---|
62 | strcpy (wtmp.ut_line, "~");
|
---|
63 | wtmp.ut_pid = 0;
|
---|
64 | wtmp.ut_type = BOOT_TIME;
|
---|
65 | wtmp.ut_time = now;
|
---|
66 | wtmp.ut_host[0]= '\0';
|
---|
67 | write (fd, (char *) &wtmp,sizeof(struct utmp));
|
---|
68 | close(fd);
|
---|
69 | }
|
---|
70 | fd = open(SHUT_LOG,O_APPEND|O_WRONLY,1);
|
---|
71 | if (!fd)
|
---|
72 | perror ("open");
|
---|
73 | else {
|
---|
74 | sprintf (mes,"%s %02d %02d:%02d:%02d %s: system %s by %s@%s\n",
|
---|
75 | month[tm->tm_mon],tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
|
---|
76 | prog,prog,who,host);
|
---|
77 | write (fd,mes,strlen(mes));
|
---|
78 | close(fd);
|
---|
79 | }
|
---|
80 | return;
|
---|
81 | }
|
---|