1 | /* |
---|
2 | * Copyright (c) 1992-1998 Michael A. Cooper. |
---|
3 | * This software may be freely used and distributed provided it is not |
---|
4 | * sold for profit or used in part or in whole for commercial gain |
---|
5 | * without prior written agreement, and the author is credited |
---|
6 | * appropriately. |
---|
7 | */ |
---|
8 | |
---|
9 | #ifndef lint |
---|
10 | static char *RCSid = "$Revision: 1.1.1.3 $"; |
---|
11 | #endif |
---|
12 | |
---|
13 | /* |
---|
14 | * Boot Time information |
---|
15 | */ |
---|
16 | |
---|
17 | #include "defs.h" |
---|
18 | #include <utmp.h> |
---|
19 | #include <sys/stat.h> |
---|
20 | #include <sys/time.h> |
---|
21 | |
---|
22 | #if defined(BOOT_TIME) && defined(HAVE_GETUTID) |
---|
23 | /* |
---|
24 | * Look in utmp for the system boot time using getutid() |
---|
25 | */ |
---|
26 | extern char *GetBootTimeGetutid() |
---|
27 | { |
---|
28 | static struct utmp Utmp; |
---|
29 | struct utmp *Ptr; |
---|
30 | char *DateStr = NULL; |
---|
31 | register char *cp; |
---|
32 | |
---|
33 | setutent(); |
---|
34 | memset(&Utmp, 0, sizeof(Utmp)); |
---|
35 | Utmp.ut_type = BOOT_TIME; |
---|
36 | |
---|
37 | if (Ptr = getutid(&Utmp)) |
---|
38 | if (Ptr->ut_type == BOOT_TIME) { |
---|
39 | DateStr = (char *) ctime(&(Ptr->ut_time)); |
---|
40 | if (cp = strchr(DateStr, '\n')) |
---|
41 | *cp = CNULL; |
---|
42 | } |
---|
43 | |
---|
44 | endutent(); |
---|
45 | |
---|
46 | return(DateStr); |
---|
47 | } |
---|
48 | #endif /* BOOT_TIME && HAVE_GETUTID */ |
---|
49 | |
---|
50 | #if defined(BOOT_TIME) |
---|
51 | |
---|
52 | #ifndef UTMP_FILE |
---|
53 | #define UTMP_FILE "/etc/utmp" |
---|
54 | #endif |
---|
55 | |
---|
56 | /* |
---|
57 | * Look in utmp for the system boot time |
---|
58 | */ |
---|
59 | extern char *GetBootTimeUtmp() |
---|
60 | { |
---|
61 | struct utmp *Ptr; |
---|
62 | struct utmp *UtmpBuff; |
---|
63 | struct stat StatBuff; |
---|
64 | int fd; |
---|
65 | size_t Amt; |
---|
66 | char *DateStr = NULL; |
---|
67 | register char *cp; |
---|
68 | |
---|
69 | if (stat(UTMP_FILE, &StatBuff) < 0) { |
---|
70 | SImsg(SIM_GERR, "stat failed: %s: %s", UTMP_FILE, SYSERR); |
---|
71 | return((char *) NULL); |
---|
72 | } |
---|
73 | Amt = StatBuff.st_size; |
---|
74 | |
---|
75 | if ((fd = open(UTMP_FILE, O_RDONLY)) < 0) { |
---|
76 | SImsg(SIM_GERR, "open O_RDONLY failed: %s: %s", UTMP_FILE, SYSERR); |
---|
77 | return((char *) NULL); |
---|
78 | } |
---|
79 | |
---|
80 | UtmpBuff = (struct utmp *) xmalloc(Amt); |
---|
81 | if (read(fd, (char *)UtmpBuff, Amt) != Amt) { |
---|
82 | SImsg(SIM_GERR, "read failed: %s: %s", UTMP_FILE, SYSERR); |
---|
83 | (void) close(fd); |
---|
84 | return((char *) NULL); |
---|
85 | } |
---|
86 | (void) close(fd); |
---|
87 | |
---|
88 | for (Ptr = UtmpBuff; Ptr < UtmpBuff + Amt; ++Ptr) |
---|
89 | if (Ptr->ut_type == BOOT_TIME) { |
---|
90 | DateStr = (char *) ctime(&(Ptr->ut_time)); |
---|
91 | if (cp = strchr(DateStr, '\n')) |
---|
92 | *cp = CNULL; |
---|
93 | break; |
---|
94 | } |
---|
95 | |
---|
96 | (void) free(UtmpBuff); |
---|
97 | return(DateStr); |
---|
98 | } |
---|
99 | #endif /* BOOT_TIME */ |
---|
100 | |
---|
101 | #if defined(HAVE_KVM) && defined(HAVE_NLIST) |
---|
102 | #if !defined(BOOTTIME_SYM) |
---|
103 | # define BOOTTIME_SYM "_boottime" |
---|
104 | #endif |
---|
105 | |
---|
106 | /* |
---|
107 | * Get kernel BOOTTIME string by reading the |
---|
108 | * symbol "_boottime" from the kernel. |
---|
109 | */ |
---|
110 | extern char *GetBootTimeSym() |
---|
111 | { |
---|
112 | nlist_t *nlptr; |
---|
113 | static struct timeval BootTime; |
---|
114 | time_t TimeVal; |
---|
115 | register char *cp; |
---|
116 | char *DateStr = NULL; |
---|
117 | kvm_t *kd; |
---|
118 | |
---|
119 | if (kd = KVMopen()) { |
---|
120 | if ((nlptr = KVMnlist(kd, BOOTTIME_SYM, (nlist_t *)NULL, 0)) == NULL) |
---|
121 | return((char *) NULL); |
---|
122 | |
---|
123 | if (CheckNlist(nlptr)) |
---|
124 | return((char *) NULL); |
---|
125 | |
---|
126 | if (KVMget(kd, nlptr->n_value, (char *) &BootTime, |
---|
127 | sizeof(BootTime), KDT_DATA)){ |
---|
128 | SImsg(SIM_GERR, "Read of \"%s\" from kernel failed.", |
---|
129 | BOOTTIME_SYM); |
---|
130 | } else { |
---|
131 | TimeVal = BootTime.tv_sec; |
---|
132 | DateStr = (char *) ctime(&TimeVal); |
---|
133 | if (cp = strchr(DateStr, '\n')) |
---|
134 | *cp = CNULL; |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | if (kd) |
---|
139 | KVMclose(kd); |
---|
140 | |
---|
141 | return(DateStr); |
---|
142 | } |
---|
143 | #endif /* HAVE_KVM && HAVE_NLIST */ |
---|
144 | |
---|
145 | /* |
---|
146 | * Get Boot Time |
---|
147 | */ |
---|
148 | extern char *GetBootTime() |
---|
149 | { |
---|
150 | extern PSI_t GetBootTimePSI[]; |
---|
151 | |
---|
152 | return(PSIquery(GetBootTimePSI)); |
---|
153 | } |
---|