2 log - log the shutdown's and the halt's
4 Author: Edvard Tuinder <v892231@si.hhs.NL>
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.
12 #define _POSIX_SOURCE 1
13 #include <sys/types.h>
21 #include <sys/utsname.h>
23 static char SHUT_LOG
[] = "/usr/adm/log";
27 static char *month
[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
28 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
30 void write_log(char *wtmpfile
)
33 static struct utmp wtmp
;
34 static struct passwd
*pwd
;
38 struct utsname utsname
;
39 char *host
= "localhost";
44 if (uname(&utsname
) >= 0) host
= utsname
.nodename
;
46 pwd
= getpwuid(getuid());
47 if (pwd
== (struct passwd
*)0)
50 strcpy (who
,pwd
->pw_name
);
51 fd
= open(wtmpfile
,O_APPEND
|O_WRONLY
|O_CREAT
,1);
53 if (strcmp(prog
,"reboot"))
55 strcpy (wtmp
.ut_name
, prog
);
57 strcpy (wtmp
.ut_user
, prog
);
61 strcpy (wtmp
.ut_name
, "shutdown"); /* last ... */
63 strcpy (wtmp
.ut_user
, "shutdown"); /* last ... */
65 strcpy (wtmp
.ut_id
, "~~");
66 strcpy (wtmp
.ut_line
, "~");
68 wtmp
.ut_type
= BOOT_TIME
;
70 wtmp
.ut_host
[0]= '\0';
71 write (fd
, (char *) &wtmp
,sizeof(struct utmp
));
74 fd
= open(SHUT_LOG
,O_APPEND
|O_WRONLY
,1);
78 sprintf (mes
,"%s %02d %02d:%02d:%02d %s: system %s by %s@%s\n",
79 month
[tm
->tm_mon
],tm
->tm_mday
,tm
->tm_hour
,tm
->tm_min
,tm
->tm_sec
,
81 write (fd
,mes
,strlen(mes
));