]>
git.ipfire.org Git - people/ms/systemd.git/blob - utmp-wtmp.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/utsname.h>
29 #include "utmp-wtmp.h"
31 int utmp_get_runlevel(int *runlevel
, int *previous
) {
32 struct utmpx lookup
, *found
;
38 /* If these values are set in the environment this takes
39 * precedence. Presumably, sysvinit does this to work around a
40 * race condition that would otherwise exist where we'd always
41 * go to disk and hence might read runlevel data that might be
42 * very new and does not apply to the current script being
45 if ((e
= getenv("RUNLEVEL")) && e
[0] > 0) {
49 /* $PREVLEVEL seems to be an Upstart thing */
51 if ((e
= getenv("PREVLEVEL")) && e
[0] > 0)
60 if (utmpxname(_PATH_UTMPX
) < 0)
66 lookup
.ut_type
= RUN_LVL
;
68 if (!(found
= getutxid(&lookup
)))
73 a
= found
->ut_pid
& 0xFF;
74 b
= (found
->ut_pid
>> 8) & 0xFF;
92 static void init_entry(struct utmpx
*store
, usec_t timestamp
) {
101 timestamp
= now(CLOCK_REALTIME
);
103 store
->ut_tv
.tv_sec
= timestamp
/ USEC_PER_SEC
;
104 store
->ut_tv
.tv_usec
= timestamp
% USEC_PER_SEC
;
106 if (uname(&uts
) >= 0)
107 strncpy(store
->ut_host
, uts
.release
, sizeof(store
->ut_host
));
109 strncpy(store
->ut_line
, "~", sizeof(store
->ut_line
)); /* or ~~ ? */
110 strncpy(store
->ut_id
, "~~", sizeof(store
->ut_id
));
113 static int write_entry_utmp(const struct utmpx
*store
) {
118 /* utmp is similar to wtmp, but there is only one entry for
119 * each entry type resp. user; i.e. basically a key/value
122 if (utmpxname(_PATH_UTMPX
) < 0)
127 if (!pututxline(store
))
137 static int write_entry_wtmp(const struct utmpx
*store
) {
140 /* wtmp is a simple append-only file where each entry is
141 simply appended to * the end; i.e. basically a log. */
144 updwtmpx(_PATH_WTMPX
, store
);
148 static int write_entry_both(const struct utmpx
*store
) {
151 r
= write_entry_utmp(store
);
152 s
= write_entry_wtmp(store
);
157 /* If utmp/wtmp have been disabled, that's a good thing, hence
158 * ignore the errors */
165 int utmp_put_shutdown(usec_t timestamp
) {
168 init_entry(&store
, timestamp
);
170 store
.ut_type
= RUN_LVL
;
171 strncpy(store
.ut_user
, "shutdown", sizeof(store
.ut_user
));
173 return write_entry_both(&store
);
176 int utmp_put_reboot(usec_t timestamp
) {
179 init_entry(&store
, timestamp
);
181 store
.ut_type
= BOOT_TIME
;
182 strncpy(store
.ut_user
, "reboot", sizeof(store
.ut_type
));
184 return write_entry_both(&store
);
187 int utmp_put_runlevel(usec_t timestamp
, int runlevel
, int previous
) {
191 assert(runlevel
> 0);
194 /* Find the old runlevel automatically */
196 if ((r
= utmp_get_runlevel(&previous
, NULL
)) < 0)
199 if (previous
== runlevel
)
203 init_entry(&store
, timestamp
);
205 store
.ut_type
= RUN_LVL
;
206 store
.ut_pid
= (runlevel
& 0xFF) | ((previous
& 0xFF) << 8);
207 strncpy(store
.ut_user
, "runlevel", sizeof(store
.ut_user
));
209 return write_entry_both(&store
);