mkfs: support indirect blocks in directories
[minix.git] / commands / reboot / log.c
blob74e7bd0f47be7ff9f9257b9736d512970388051e
1 /*
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>
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>
23 static char SHUT_LOG[] = "/usr/adm/log";
25 char who[8];
26 extern char *prog;
27 static char *month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
28 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
30 void write_log(char *wtmpfile)
32 int fd;
33 static struct utmp wtmp;
34 static struct passwd *pwd;
35 char mes[90];
36 struct tm *tm;
37 time_t now;
38 struct utsname utsname;
39 char *host = "localhost";
41 time(&now);
42 tm = localtime(&now);
44 if (uname(&utsname) >= 0) host = utsname.nodename;
46 pwd = getpwuid(getuid());
47 if (pwd == (struct passwd *)0)
48 strcpy (who,"root");
49 else
50 strcpy (who,pwd->pw_name);
51 fd = open(wtmpfile,O_APPEND|O_WRONLY|O_CREAT,1);
52 if (fd) {
53 if (strcmp(prog,"reboot"))
54 #ifdef __NBSD_LIBC
55 strcpy (wtmp.ut_name, prog);
56 #else
57 strcpy (wtmp.ut_user, prog);
58 #endif
59 else
60 #ifdef __NBSD_LIBC
61 strcpy (wtmp.ut_name, "shutdown"); /* last ... */
62 #else
63 strcpy (wtmp.ut_user, "shutdown"); /* last ... */
64 #endif
65 strcpy (wtmp.ut_id, "~~");
66 strcpy (wtmp.ut_line, "~");
67 wtmp.ut_pid = 0;
68 wtmp.ut_type = BOOT_TIME;
69 wtmp.ut_time = now;
70 wtmp.ut_host[0]= '\0';
71 write (fd, (char *) &wtmp,sizeof(struct utmp));
72 close(fd);
74 fd = open(SHUT_LOG,O_APPEND|O_WRONLY,1);
75 if (!fd)
76 perror ("open");
77 else {
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,
80 prog,prog,who,host);
81 write (fd,mes,strlen(mes));
82 close(fd);
84 return;