chiark
/
gitweb
/
~ianmdlvl
/
elogind.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
shared: add process-util.[ch]
[elogind.git]
/
src
/
shared
/
utmp-wtmp.c
diff --git
a/src/shared/utmp-wtmp.c
b/src/shared/utmp-wtmp.c
index 32996fad36cd711fcf03a12286742bbd35de05cd..7fb6fe3a6751602718039b2a8da55772cbaca2e7 100644
(file)
--- a/
src/shared/utmp-wtmp.c
+++ b/
src/shared/utmp-wtmp.c
@@
-21,12
+21,11
@@
#include <utmpx.h>
#include <errno.h>
#include <utmpx.h>
#include <errno.h>
-#include <assert.h>
#include <string.h>
#include <sys/utsname.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/utsname.h>
#include <fcntl.h>
#include <unistd.h>
-#include <
sys/
poll.h>
+#include <poll.h>
#include "macro.h"
#include "path-util.h"
#include "macro.h"
#include "path-util.h"
@@
-92,8
+91,6
@@
int utmp_get_runlevel(int *runlevel, int *previous) {
static void init_timestamp(struct utmpx *store, usec_t t) {
assert(store);
static void init_timestamp(struct utmpx *store, usec_t t) {
assert(store);
- zero(*store);
-
if (t <= 0)
t = now(CLOCK_REALTIME);
if (t <= 0)
t = now(CLOCK_REALTIME);
@@
-143,7
+140,7
@@
static int write_entry_wtmp(const struct utmpx *store) {
assert(store);
/* wtmp is a simple append-only file where each entry is
assert(store);
/* wtmp is a simple append-only file where each entry is
- simply appended to
*
the end; i.e. basically a log. */
+ simply appended to the end; i.e. basically a log. */
errno = 0;
updwtmpx(_PATH_WTMPX, store);
errno = 0;
updwtmpx(_PATH_WTMPX, store);
@@
-172,7
+169,7
@@
static int write_entry_both(const struct utmpx *store) {
}
int utmp_put_shutdown(void) {
}
int utmp_put_shutdown(void) {
- struct utmpx store;
+ struct utmpx store
= {}
;
init_entry(&store, 0);
init_entry(&store, 0);
@@
-183,7
+180,7
@@
int utmp_put_shutdown(void) {
}
int utmp_put_reboot(usec_t t) {
}
int utmp_put_reboot(usec_t t) {
- struct utmpx store;
+ struct utmpx store
= {}
;
init_entry(&store, t);
init_entry(&store, t);
@@
-206,16
+203,17
@@
_pure_ static const char *sanitize_id(const char *id) {
}
int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line) {
}
int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line) {
- struct utmpx store;
+ struct utmpx store = {
+ .ut_type = INIT_PROCESS,
+ .ut_pid = pid,
+ .ut_session = sid,
+ };
assert(id);
init_timestamp(&store, 0);
assert(id);
init_timestamp(&store, 0);
- store.ut_type = INIT_PROCESS;
- store.ut_pid = pid;
- store.ut_session = sid;
-
+ /* ut_id needs only be nul-terminated if it is shorter than sizeof(ut_id) */
strncpy(store.ut_id, sanitize_id(id), sizeof(store.ut_id));
if (line)
strncpy(store.ut_id, sanitize_id(id), sizeof(store.ut_id));
if (line)
@@
-225,14
+223,15
@@
int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line
}
int utmp_put_dead_process(const char *id, pid_t pid, int code, int status) {
}
int utmp_put_dead_process(const char *id, pid_t pid, int code, int status) {
- struct utmpx lookup, store, store_wtmp, *found;
+ struct utmpx lookup = {
+ .ut_type = INIT_PROCESS /* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */
+ }, store, store_wtmp, *found;
assert(id);
setutxent();
assert(id);
setutxent();
- zero(lookup);
- lookup.ut_type = INIT_PROCESS; /* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */
+ /* ut_id needs only be nul-terminated if it is shorter than sizeof(ut_id) */
strncpy(lookup.ut_id, sanitize_id(id), sizeof(lookup.ut_id));
found = getutxid(&lookup);
strncpy(lookup.ut_id, sanitize_id(id), sizeof(lookup.ut_id));
found = getutxid(&lookup);
@@
-260,7
+259,7
@@
int utmp_put_dead_process(const char *id, pid_t pid, int code, int status) {
int utmp_put_runlevel(int runlevel, int previous) {
int utmp_put_runlevel(int runlevel, int previous) {
- struct utmpx store;
+ struct utmpx store
= {}
;
int r;
assert(runlevel > 0);
int r;
assert(runlevel > 0);
@@
-347,16
+346,20
@@
static int write_to_terminal(const char *tty, const char *message) {
return 0;
}
return 0;
}
-int utmp_wall(const char *message, bool (*match_tty)(const char *tty)) {
+int utmp_wall(const char *message,
const char *username,
bool (*match_tty)(const char *tty)) {
_cleanup_free_ char *text = NULL, *hn = NULL, *un = NULL, *tty = NULL;
char date[FORMAT_TIMESTAMP_MAX];
struct utmpx *u;
int r;
hn = gethostname_malloc();
_cleanup_free_ char *text = NULL, *hn = NULL, *un = NULL, *tty = NULL;
char date[FORMAT_TIMESTAMP_MAX];
struct utmpx *u;
int r;
hn = gethostname_malloc();
- un = getlogname_malloc();
- if (!hn || !un)
+ if (!hn)
return -ENOMEM;
return -ENOMEM;
+ if (!username) {
+ un = getlogname_malloc();
+ if (!un)
+ return -ENOMEM;
+ }
getttyname_harder(STDIN_FILENO, &tty);
getttyname_harder(STDIN_FILENO, &tty);
@@
-364,7
+367,7
@@
int utmp_wall(const char *message, bool (*match_tty)(const char *tty)) {
"\a\r\n"
"Broadcast message from %s@%s%s%s (%s):\r\n\r\n"
"%s\r\n\r\n",
"\a\r\n"
"Broadcast message from %s@%s%s%s (%s):\r\n\r\n"
"%s\r\n\r\n",
- un, hn,
+ un
?: username
, hn,
tty ? " on " : "", strempty(tty),
format_timestamp(date, sizeof(date), now(CLOCK_REALTIME)),
message) < 0)
tty ? " on " : "", strempty(tty),
format_timestamp(date, sizeof(date), now(CLOCK_REALTIME)),
message) < 0)