journal: add preliminary incomplete implementation

This commit is contained in:
Lennart Poettering 2011-10-07 21:06:39 +02:00
parent 9a36607584
commit 87d2c1ff6a
30 changed files with 4076 additions and 275 deletions

6
.gitignore vendored
View File

@ -1,3 +1,7 @@
systemd-journalctl
systemd-journald
test-id128
test-journal
test-install
org.freedesktop.hostname1.xml
org.freedesktop.locale1.xml
@ -91,7 +95,7 @@ install-sh
missing
stamp-*
*.stamp
Makefile
/Makefile
ltmain.sh
*.tar.bz2
*.tar.gz

View File

@ -134,7 +134,8 @@ rootbin_PROGRAMS = \
systemd-ask-password \
systemd-tty-ask-password-agent \
systemd-tmpfiles \
systemd-machine-id-setup
systemd-machine-id-setup \
systemd-journalctl
bin_PROGRAMS = \
systemd-cgls \
@ -173,7 +174,8 @@ rootlibexec_PROGRAMS = \
systemd-detect-virt \
systemd-sysctl \
systemd-logind \
systemd-uaccess
systemd-uaccess \
systemd-journald
if ENABLE_BINFMT
rootlibexec_PROGRAMS += \
@ -225,7 +227,9 @@ noinst_PROGRAMS = \
test-env-replace \
test-strv \
test-login \
test-install
test-install \
test-id128 \
test-journal
if HAVE_PAM
pamlib_LTLIBRARIES = \
@ -685,7 +689,8 @@ libsystemd_core_la_SOURCES = \
src/dbus-common.c \
src/sd-daemon.c \
src/install.c \
src/cgroup-attr.c
src/cgroup-attr.c \
src/sd-id128.c
nodist_libsystemd_core_la_SOURCES = \
src/load-fragment-gperf.c \
@ -947,6 +952,53 @@ test_install_CFLAGS = \
test_install_LDADD = \
libsystemd-basic.la
test_id128_SOURCES = \
src/test-id128.c \
src/sd-id128.c
test_id128_CFLAGS = \
$(AM_CFLAGS)
test_id128_LDADD = \
libsystemd-basic.la
test_journal_SOURCES = \
src/journal/test-journal.c \
src/journal/sd-journal.c \
src/journal/lookup3.c \
src/sd-id128.c
test_journal_CFLAGS = \
$(AM_CFLAGS)
test_journal_LDADD = \
libsystemd-basic.la
systemd_journald_SOURCES = \
src/journal/journald.c \
src/journal/sd-journal.c \
src/journal/lookup3.c \
src/sd-id128.c
systemd_journald_CFLAGS = \
$(AM_CFLAGS)
systemd_journald_LDADD = \
libsystemd-basic.la \
libsystemd-daemon.la
systemd_journalctl_SOURCES = \
src/journal/journalctl.c \
src/journal/sd-journal.c \
src/journal/lookup3.c \
src/sd-id128.c
systemd_journalctl_CFLAGS = \
$(AM_CFLAGS)
systemd_journalctl_LDADD = \
libsystemd-basic.la
systemd_stdout_syslog_bridge_SOURCES = \
src/stdout-syslog-bridge.c \
src/tcpwrap.c
@ -1142,7 +1194,8 @@ systemd_tmpfiles_LDADD = \
systemd_machine_id_setup_SOURCES = \
src/machine-id-setup.c \
src/machine-id-main.c
src/machine-id-main.c \
src/sd-id128.c
systemd_machine_id_setup_CFLAGS = \
$(AM_CFLAGS)

1
po/.gitignore vendored
View File

@ -1,3 +1,4 @@
POTFILES
Makefile.in.in
.intltool-merge-cache
Makefile

View File

@ -770,7 +770,7 @@ static void automount_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
if (packet.v5_packet.pid > 0) {
char *p = NULL;
get_process_name(packet.v5_packet.pid, &p);
get_process_comm(packet.v5_packet.pid, &p);
log_debug("Got direct mount request for %s, triggered by %lu (%s)", packet.v5_packet.name, (unsigned long) packet.v5_packet.pid, strna(p));
free(p);

View File

@ -133,7 +133,7 @@ static int show_cgroup_one_by_path(const char *path, const char *prefix, unsigne
for (i = 0; i < n; i++) {
char *t = NULL;
get_process_cmdline(pids[i], n_columns, &t);
get_process_cmdline(pids[i], n_columns, true, &t);
printf("%s%s %*lu %s\n",
prefix,

1
src/journal/Makefile Symbolic link
View File

@ -0,0 +1 @@
../Makefile

139
src/journal/journal-def.h Normal file
View File

@ -0,0 +1,139 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
#ifndef foojournaldefhfoo
#define foojournaldefhfoo
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <inttypes.h>
#include "macro.h"
#include "sd-id128.h"
typedef struct Header Header;
typedef struct ObjectHeader ObjectHeader;
typedef union Object Object;
typedef struct DataObject DataObject;
typedef struct EntryObject EntryObject;
typedef struct HashTableObject HashTableObject;
typedef struct BisectTableObject BisectTableObject;
typedef struct EntryItem EntryItem;
typedef struct HashItem HashItem;
/* Object types */
enum {
OBJECT_UNUSED,
OBJECT_DATA,
OBJECT_ENTRY,
OBJECT_HASH_TABLE,
OBJECT_BISECT_TABLE
};
_packed_ struct ObjectHeader {
uint8_t type;
uint8_t reserved[3];
uint64_t size;
uint8_t payload[];
};
_packed_ struct DataObject {
ObjectHeader object;
uint64_t hash;
uint64_t head_entry_offset;
uint64_t tail_entry_offset;
uint64_t prev_hash_offset;
uint64_t next_hash_offset;
uint8_t payload[];
};
_packed_ struct EntryItem {
uint64_t object_offset;
uint64_t prev_entry_offset;
uint64_t next_entry_offset;
};
_packed_ struct EntryObject {
ObjectHeader object;
uint64_t seqnum;
uint64_t realtime;
uint64_t monotonic;
uint64_t prev_entry_offset;
uint64_t next_entry_offset;
EntryItem items[];
};
_packed_ struct HashItem {
uint64_t head_hash_offset;
uint64_t tail_hash_offset;
};
_packed_ struct HashTableObject {
ObjectHeader object;
HashItem table[];
};
_packed_ struct BisectTableObject {
ObjectHeader object;
uint64_t table[];
};
union Object {
ObjectHeader object;
DataObject data;
EntryObject entry;
HashTableObject hash_table;
BisectTableObject bisect_table;
};
enum {
STATE_OFFLINE,
STATE_ONLINE,
STATE_ARCHIVED
};
_packed_ struct Header {
uint8_t signature[8]; /* "LPKSHHRH" */
uint32_t compatible_flags;
uint32_t incompatible_flags;
uint32_t state;
uint8_t reserved[4];
sd_id128_t file_id;
sd_id128_t machine_id;
sd_id128_t boot_id;
uint64_t arena_offset;
uint64_t arena_size;
uint64_t arena_max_size;
uint64_t arena_min_size;
uint64_t arena_keep_free;
uint64_t hash_table_offset; /* for looking up data objects */
uint64_t hash_table_size;
uint64_t bisect_table_offset; /* for looking up entry objects */
uint64_t bisect_table_size;
uint64_t head_object_offset;
uint64_t tail_object_offset;
uint64_t head_entry_offset;
uint64_t tail_entry_offset;
uint64_t last_bisect_offset;
uint64_t n_objects;
uint64_t seqnum_base;
uint64_t seqnum;
};
#endif

View File

@ -0,0 +1,53 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
#ifndef foojournalprivatehfoo
#define foojournalprivatehfoo
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <inttypes.h>
#include "sd-journal.h"
#include "journal-def.h"
#include "util.h"
typedef struct JournalFile JournalFile;
int journal_file_open(sd_journal *j, const char *fname, int flags, mode_t mode, JournalFile **ret);
void journal_file_close(JournalFile *j);
int journal_file_move_to_object(JournalFile *f, uint64_t offset, Object **ret);
uint64_t journal_file_entry_n_items(Object *o);
int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, Object **ret, uint64_t *offset);
int journal_file_move_to_entry(JournalFile *f, uint64_t seqnum, Object **ret, uint64_t *offset);
int journal_file_find_first_entry(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset);
int journal_file_find_last_entry(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset);
int journal_file_next_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset);
int journal_file_prev_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset);
void journal_file_dump(JournalFile *f);
#endif

91
src/journal/journalctl.c Normal file
View File

@ -0,0 +1,91 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <fcntl.h>
#include <errno.h>
#include <stddef.h>
#include "journal-private.h"
int main(int argc, char *argv[]) {
int r;
JournalFile *f;
Object *o = NULL;
log_parse_environment();
log_open();
r = journal_file_open(NULL, "/var/log/journal/system.journal", O_RDONLY, 0644, &f);
if (r == -ENOENT)
r = journal_file_open(NULL, "/run/log/journal/system.journal", O_RDONLY, 0644, &f);
if (r < 0) {
log_error("Failed to open journal: %s", strerror(-r));
return EXIT_FAILURE;
}
for (;;) {
uint64_t offset;
uint64_t n, i;
r = journal_file_next_entry(f, o, &o, &offset);
if (r < 0) {
log_error("Failed to read journal: %s", strerror(-r));
goto finish;
}
if (r == 0)
break;
printf("entry: %llu\n", (unsigned long long) le64toh(o->entry.seqnum));
n = journal_file_entry_n_items(o);
for (i = 0; i < n; i++) {
uint64_t p, l;
p = le64toh(o->entry.items[i].object_offset);
r = journal_file_move_to_object(f, p, &o);
if (r < 0) {
log_error("Failed to move to data: %s", strerror(-r));
goto finish;
}
if (le64toh(o->object.type) != OBJECT_DATA) {
log_error("Invalid file");
goto finish;
}
l = o->object.size - offsetof(Object, data.payload);
printf("\t[%.*s]\n", (int) l, o->data.payload);
r = journal_file_move_to_object(f, offset, &o);
if (r < 0) {
log_error("Failed to move back to entry: %s", strerror(-r));
goto finish;
}
}
}
finish:
journal_file_close(f);
return 0;
}

460
src/journal/journald.c Normal file
View File

@ -0,0 +1,460 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <sys/epoll.h>
#include <sys/socket.h>
#include <errno.h>
#include <sys/signalfd.h>
#include <unistd.h>
#include <fcntl.h>
#include "hashmap.h"
#include "journal-private.h"
#include "sd-daemon.h"
#include "socket-util.h"
typedef struct Server {
int syslog_fd;
int epoll_fd;
int signal_fd;
JournalFile *system_journal;
Hashmap *user_journals;
} Server;
static void process_message(Server *s, const char *buf, struct ucred *ucred, struct timeval *tv) {
char *message = NULL, *pid = NULL, *uid = NULL, *gid = NULL,
*source_time = NULL, *boot_id = NULL, *machine_id = NULL,
*comm = NULL, *cmdline = NULL, *hostname = NULL,
*audit_session = NULL, *audit_loginuid = NULL,
*syslog_priority = NULL, *syslog_facility = NULL,
*exe = NULL;
dual_timestamp ts;
struct iovec iovec[15];
unsigned n = 0;
char idbuf[33];
sd_id128_t id;
int r;
char *t;
int priority = LOG_USER | LOG_INFO;
dual_timestamp_get(&ts);
parse_syslog_priority((char**) &buf, &priority);
skip_syslog_date((char**) &buf);
if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
IOVEC_SET_STRING(iovec[n++], syslog_priority);
if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
IOVEC_SET_STRING(iovec[n++], syslog_facility);
message = strappend("MESSAGE=", buf);
if (message)
IOVEC_SET_STRING(iovec[n++], message);
if (ucred) {
uint32_t session;
uid_t loginuid;
if (asprintf(&pid, "PID=%lu", (unsigned long) ucred->pid) >= 0)
IOVEC_SET_STRING(iovec[n++], pid);
if (asprintf(&uid, "UID=%lu", (unsigned long) ucred->uid) >= 0)
IOVEC_SET_STRING(iovec[n++], uid);
if (asprintf(&gid, "GID=%lu", (unsigned long) ucred->gid) >= 0)
IOVEC_SET_STRING(iovec[n++], gid);
r = get_process_comm(ucred->pid, &t);
if (r >= 0) {
comm = strappend("COMM=", t);
if (comm)
IOVEC_SET_STRING(iovec[n++], comm);
free(t);
}
r = get_process_exe(ucred->pid, &t);
if (r >= 0) {
exe = strappend("EXE=", t);
if (comm)
IOVEC_SET_STRING(iovec[n++], exe);
free(t);
}
r = get_process_cmdline(ucred->pid, LINE_MAX, false, &t);
if (r >= 0) {
cmdline = strappend("CMDLINE=", t);
if (cmdline)
IOVEC_SET_STRING(iovec[n++], cmdline);
free(t);
}
r = audit_session_from_pid(ucred->pid, &session);
if (r >= 0)
if (asprintf(&audit_session, "AUDIT_SESSION=%lu", (unsigned long) session) >= 0)
IOVEC_SET_STRING(iovec[n++], audit_session);
r = audit_loginuid_from_pid(ucred->pid, &loginuid);
if (r >= 0)
if (asprintf(&audit_loginuid, "AUDIT_LOGINUID=%lu", (unsigned long) loginuid) >= 0)
IOVEC_SET_STRING(iovec[n++], audit_loginuid);
}
if (tv) {
if (asprintf(&source_time, "SOURCE_REALTIME_TIMESTAMP=%llu",
(unsigned long long) timeval_load(tv)) >= 0)
IOVEC_SET_STRING(iovec[n++], source_time);
}
r = sd_id128_get_boot(&id);
if (r >= 0)
if (asprintf(&boot_id, "BOOT_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
IOVEC_SET_STRING(iovec[n++], boot_id);
r = sd_id128_get_machine(&id);
if (r >= 0)
if (asprintf(&machine_id, "MACHINE_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
IOVEC_SET_STRING(iovec[n++], machine_id);
t = gethostname_malloc();
if (t) {
hostname = strappend("HOSTNAME=", t);
if (hostname)
IOVEC_SET_STRING(iovec[n++], hostname);
free(t);
}
r = journal_file_append_entry(s->system_journal, &ts, iovec, n, NULL, NULL);
if (r < 0)
log_error("Failed to write entry: %s", strerror(-r));
free(message);
free(pid);
free(uid);
free(gid);
free(comm);
free(cmdline);
free(source_time);
free(boot_id);
free(machine_id);
free(hostname);
free(audit_session);
free(audit_loginuid);
free(syslog_facility);
free(syslog_priority);
}
static int process_event(Server *s, struct epoll_event *ev) {
assert(s);
if (ev->events != EPOLLIN) {
log_info("Got invalid event from epoll.");
return -EIO;
}
if (ev->data.fd == s->signal_fd) {
struct signalfd_siginfo sfsi;
ssize_t n;
n = read(s->signal_fd, &sfsi, sizeof(sfsi));
if (n != sizeof(sfsi)) {
if (n >= 0)
return -EIO;
if (errno == EINTR || errno == EAGAIN)
return 0;
return -errno;
}
log_debug("Received SIG%s", signal_to_string(sfsi.ssi_signo));
return 0;
} else {
for (;;) {
char buf[LINE_MAX+1];
struct msghdr msghdr;
struct iovec iovec;
struct ucred *ucred = NULL;
struct timeval *tv = NULL;
struct cmsghdr *cmsg;
union {
struct cmsghdr cmsghdr;
uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
CMSG_SPACE(sizeof(struct timeval))];
} control;
ssize_t n;
char *e;
zero(iovec);
iovec.iov_base = buf;
iovec.iov_len = sizeof(buf)-1;
zero(control);
zero(msghdr);
msghdr.msg_iov = &iovec;
msghdr.msg_iovlen = 1;
msghdr.msg_control = &control;
msghdr.msg_controllen = sizeof(control);
n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT);
if (n < 0) {
if (errno == EINTR || errno == EAGAIN)
return 1;
log_error("recvmsg() failed: %m");
return -errno;
}
for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
if (cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SCM_CREDENTIALS &&
cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)))
ucred = (struct ucred*) CMSG_DATA(cmsg);
else if (cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SO_TIMESTAMP &&
cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
tv = (struct timeval*) CMSG_DATA(cmsg);
}
e = memchr(buf, '\n', n);
if (e)
*e = 0;
else
buf[n] = 0;
process_message(s, strstrip(buf), ucred, tv);
}
}
return 1;
}
static int open_system_journal(JournalFile **f) {
int r;
r = journal_file_open(NULL, "/var/log/journal/system.journal", O_RDWR|O_CREAT, 0644, f);
if (r == -ENOENT) {
mkdir_p("/run/log/journal", 0755);
r = journal_file_open(NULL, "/run/log/journal/system.journal", O_RDWR|O_CREAT, 0644, f);
}
return r;
}
static int server_init(Server *s) {
int n, one, r;
struct epoll_event ev;
sigset_t mask;
assert(s);
zero(*s);
s->syslog_fd = s->signal_fd = -1;
s->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
if (s->epoll_fd < 0) {
log_error("Failed to create epoll object: %m");
return -errno;
}
n = sd_listen_fds(true);
if (n < 0) {
log_error("Failed to read listening file descriptors from environment: %s", strerror(-n));
return n;
}
if (n > 1) {
log_error("Too many file descriptors passed.");
return -EINVAL;
}
if (n == 1)
s->syslog_fd = SD_LISTEN_FDS_START;
else {
union sockaddr_union sa;
s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
if (s->syslog_fd < 0) {
log_error("socket() failed: %m");
return -errno;
}
zero(sa);
sa.un.sun_family = AF_UNIX;
strncpy(sa.un.sun_path, "/run/systemd/syslog", sizeof(sa.un.sun_path));
unlink(sa.un.sun_path);
r = bind(s->syslog_fd, &sa.sa, sizeof(sa.un));
if (r < 0) {
log_error("bind() failed: %m");
return -errno;
}
chmod(sa.un.sun_path, 0666);
}
one = 1;
r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
if (r < 0) {
log_error("SO_PASSCRED failed: %m");
return -errno;
}
one = 1;
r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
if (r < 0) {
log_error("SO_TIMESTAMP failed: %m");
return -errno;
}
zero(ev);
ev.events = EPOLLIN;
ev.data.fd = s->syslog_fd;
if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->syslog_fd, &ev) < 0) {
log_error("Failed to add server fd to epoll object: %m");
return -errno;
}
s->user_journals = hashmap_new(trivial_hash_func, trivial_compare_func);
if (!s->user_journals) {
log_error("Out of memory.");
return -ENOMEM;
}
r = open_system_journal(&s->system_journal);
if (r < 0) {
log_error("Failed to open journal: %s", strerror(-r));
return r;
}
assert_se(sigemptyset(&mask) == 0);
sigset_add_many(&mask, SIGINT, SIGTERM, -1);
assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
if (s->signal_fd < 0) {
log_error("signalfd(): %m");
return -errno;
}
zero(ev);
ev.events = EPOLLIN;
ev.data.fd = s->signal_fd;
if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->signal_fd, &ev) < 0) {
log_error("epoll_ctl(): %m");
return -errno;
}
return 0;
}
static void server_done(Server *s) {
JournalFile *f;
assert(s);
if (s->system_journal)
journal_file_close(s->system_journal);
while ((f = hashmap_steal_first(s->user_journals)))
journal_file_close(f);
hashmap_free(s->user_journals);
if (s->epoll_fd >= 0)
close_nointr_nofail(s->epoll_fd);
if (s->signal_fd >= 0)
close_nointr_nofail(s->signal_fd);
if (s->syslog_fd >= 0)
close_nointr_nofail(s->syslog_fd);
}
int main(int argc, char *argv[]) {
Server server;
int r;
/* if (getppid() != 1) { */
/* log_error("This program should be invoked by init only."); */
/* return EXIT_FAILURE; */
/* } */
if (argc > 1) {
log_error("This program does not take arguments.");
return EXIT_FAILURE;
}
log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
umask(0022);
r = server_init(&server);
if (r < 0)
goto finish;
log_debug("systemd-journald running as pid %lu", (unsigned long) getpid());
sd_notify(false,
"READY=1\n"
"STATUS=Processing messages...");
for (;;) {
struct epoll_event event;
r = epoll_wait(server.epoll_fd, &event, 1, -1);
if (r < 0) {
if (errno == EINTR)
continue;
log_error("epoll_wait() failed: %m");
r = -errno;
goto finish;
} else if (r == 0)
break;
r = process_event(&server, &event);
if (r < 0)
goto finish;
else if (r == 0)
break;
}
finish:
sd_notify(false,
"STATUS=Shutting down...");
server_done(&server);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}

1003
src/journal/lookup3.c Normal file

File diff suppressed because it is too large Load Diff

25
src/journal/lookup3.h Normal file
View File

@ -0,0 +1,25 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
#ifndef foolookup3hfoo
#define foolookup3hfoo
#include <inttypes.h>
#include <sys/types.h>
uint32_t jenkins_hashword(const uint32_t *k, size_t length, uint32_t initval);
void jenkins_hashword2(const uint32_t *k, size_t length, uint32_t *pc, uint32_t *pb);
uint32_t jenkins_hashlittle(const void *key, size_t length, uint32_t initval);
void jenkins_hashlittle2(const void *key, size_t length, uint32_t *pc, uint32_t *pb);
uint32_t jenkins_hashbig(const void *key, size_t length, uint32_t initval);
static inline uint64_t hash64(const void *data, size_t length) {
uint32_t a = 0, b = 0;
jenkins_hashlittle2(data, length, &a, &b);
return ((uint64_t) a << 32ULL) | (uint64_t) b;
}
#endif

1333
src/journal/sd-journal.c Normal file

File diff suppressed because it is too large Load Diff

74
src/journal/sd-journal.h Normal file
View File

@ -0,0 +1,74 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
#ifndef foojournalhfoo
#define foojournalhfoo
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <inttypes.h>
#include <sys/types.h>
#include "sd-id128.h"
/* TODO:
*
* - implement rotation
* - check LE/BE conversion for 8bit, 16bit, 32bit values
* - implement parallel traversal
* - implement audit gateway
* - implement native gateway
* - extend hash table/bisect table as we go
*/
typedef struct sd_journal sd_journal;
int sd_journal_open(sd_journal **ret);
void sd_journal_close(sd_journal *j);
int sd_journal_previous(sd_journal *j);
int sd_journal_next(sd_journal *j);
void* sd_journal_get(sd_journal *j, const char *field, size_t *size);
uint64_t sd_journal_get_seqnum(sd_journal *j);
uint64_t sd_journal_get_realtime_usec(sd_journal *j);
uint64_t sd_journal_get_monotonic_usec(sd_journal *j);
int sd_journal_add_match(sd_journal *j, const char *item, size_t *size);
int sd_journal_seek_head(sd_journal *j);
int sd_journal_seek_tail(sd_journal *j);
int sd_journal_seek_seqnum(sd_journal *j, uint64_t seqnum);
int sd_journal_seek_monotonic_usec(sd_journal *j, uint64_t usec);
int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec);
uint64_t sd_journal_get_max_size(sd_journal *j);
uint64_t sd_journal_get_min_size(sd_journal *j);
uint64_t sd_journal_get_keep_free(sd_journal *j);
int sd_journal_set_max_size(sd_journal *j, uint64_t size);
int sd_journal_set_min_size(sd_journal *j, uint64_t size);
int sd_journal_set_keep_free(sd_journal *j, uint64_t size);
sd_id128_t sd_journal_get_file_id(sd_journal *j);
sd_id128_t sd_journal_get_machine_id(sd_journal *j);
sd_id128_t sd_journal_get_boot_id(sd_journal *j);
#endif

View File

@ -0,0 +1,93 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <fcntl.h>
#include "journal-private.h"
#include "log.h"
int main(int argc, char *argv[]) {
dual_timestamp ts;
JournalFile *f;
struct iovec iovec;
static const char test[] = "test", test2[] = "test2";
Object *o;
log_set_max_level(LOG_DEBUG);
assert_se(journal_file_open(NULL, "test", O_RDWR|O_CREAT, 0666, &f) == 0);
dual_timestamp_get(&ts);
iovec.iov_base = (void*) test;
iovec.iov_len = strlen(test);
assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL) == 0);
iovec.iov_base = (void*) test2;
iovec.iov_len = strlen(test2);
assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL) == 0);
iovec.iov_base = (void*) test;
iovec.iov_len = strlen(test);
assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL) == 0);
journal_file_dump(f);
assert(journal_file_next_entry(f, NULL, &o, NULL) == 1);
assert(le64toh(o->entry.seqnum) == 1);
assert(journal_file_next_entry(f, o, &o, NULL) == 1);
assert(le64toh(o->entry.seqnum) == 2);
assert(journal_file_next_entry(f, o, &o, NULL) == 1);
assert(le64toh(o->entry.seqnum) == 3);
assert(journal_file_next_entry(f, o, &o, NULL) == 0);
assert(journal_file_find_first_entry(f, test, strlen(test), &o, NULL) == 1);
assert(le64toh(o->entry.seqnum) == 1);
assert(journal_file_find_last_entry(f, test, strlen(test), &o, NULL) == 1);
assert(le64toh(o->entry.seqnum) == 3);
assert(journal_file_find_last_entry(f, test2, strlen(test2), &o, NULL) == 1);
assert(le64toh(o->entry.seqnum) == 2);
assert(journal_file_find_first_entry(f, test2, strlen(test2), &o, NULL) == 1);
assert(le64toh(o->entry.seqnum) == 2);
assert(journal_file_find_first_entry(f, "quux", 4, &o, NULL) == 0);
assert(journal_file_move_to_entry(f, 1, &o, NULL) == 1);
assert(le64toh(o->entry.seqnum) == 1);
assert(journal_file_move_to_entry(f, 3, &o, NULL) == 1);
assert(le64toh(o->entry.seqnum) == 3);
assert(journal_file_move_to_entry(f, 2, &o, NULL) == 1);
assert(le64toh(o->entry.seqnum) == 2);
assert(journal_file_move_to_entry(f, 10, &o, NULL) == 0);
journal_file_close(f);
return 0;
}

57
src/journal/wjournal.c Normal file
View File

@ -0,0 +1,57 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "wjournal.h"
#include "journal-def.h"
struct WJournal {
int fd;
Header *header;
HashItem *hash_table;
uint64_t *bisect_table;
};
int wjournal_open(const char *fn, WJournal **ret) {
assert(fn);
assert(ret);
}
void wjournal_close(WJournal *j) {
assert(j);
if (j->fd >= 0)
close_nointr_nofail(j->fd);
if (j->header) {
munmap(j->header, PAGE_ALIGN(sizeof(Header)));
}
free(j);
}
int wjournal_write_object_begin(WJournal *j, uint64_t type, uint64_t size, Object **ret);
int wjournal_write_object_finish(WJournal *j, Object *ret);
int wjournal_write_field(WJournal *j, const char *buffer, uint64_t size, Object **ret);
int wjournal_write_entry(WJournal *j, const Field *fields, unsigned n_fields, Object **ret);
int wjournal_write_eof(WJournal *j);

39
src/journal/wjournal.h Normal file
View File

@ -0,0 +1,39 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
#ifndef foojournalhfoo
#define foojournalhfoo
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <inttypes.h>
typedef struct WJournal WJournal;
int wjournal_open(const char *fn, WJournal **ret);
void wjournal_close(WJournal *j);
int wjournal_write_object_begin(WJournal *j, uint64_t type, uint64_t size, Object **ret);
int wjournal_write_object_finish(WJournal *j, Object *ret);
int wjournal_write_field(WJournal *j, const char *buffer, uint64_t size, Object **ret);
int wjournal_write_entry(WJournal *j, const Field *fields, unsigned n_fields, Object **ret);
int wjournal_write_eof(WJournal *j);
#endif

View File

@ -65,45 +65,53 @@ static void server_done(Server *s) {
fdset_free(s->syslog_fds);
}
static int server_init(Server *s, unsigned n_sockets) {
int r;
unsigned i;
static int server_init(Server *s) {
int i, r, n;
struct epoll_event ev;
sigset_t mask;
assert(s);
assert(n_sockets > 0);
zero(*s);
s->kmsg_fd = s->signal_fd = -1;
if ((s->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0) {
r = -errno;
log_error("Failed to create epoll object: %s", strerror(errno));
goto fail;
s->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
if (s->epoll_fd < 0) {
log_error("Failed to create epoll object: %m");
return -errno;
}
if (!(s->syslog_fds = fdset_new())) {
r = -ENOMEM;
log_error("Failed to allocate file descriptor set: %s", strerror(errno));
goto fail;
s->syslog_fds = fdset_new();
if (!s->syslog_fds) {
log_error("Failed to allocate file descriptor set: %s", strerror(ENOMEM));
return -ENOMEM;
}
for (i = 0; i < n_sockets; i++) {
n = sd_listen_fds(true);
if (n < 0) {
log_error("Failed to read listening file descriptors from environment: %s", strerror(-n));
return n;
}
if (n <= 0 || n > SERVER_FD_MAX) {
log_error("No or too many file descriptors passed.");
return -EINVAL;
}
for (i = 0; i < n; i++) {
int fd, one = 1;
fd = SD_LISTEN_FDS_START+i;
if ((r = sd_is_socket(fd, AF_UNSPEC, SOCK_DGRAM, -1)) < 0) {
r = sd_is_socket(fd, AF_UNSPEC, SOCK_DGRAM, -1);
if (r < 0) {
log_error("Failed to determine file descriptor type: %s", strerror(-r));
goto fail;
return r;
}
if (!r) {
log_error("Wrong file descriptor type.");
r = -EINVAL;
goto fail;
return -EINVAL;
}
if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0)
@ -113,18 +121,19 @@ static int server_init(Server *s, unsigned n_sockets) {
ev.events = EPOLLIN;
ev.data.fd = fd;
if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
r = -errno;
log_error("Failed to add server fd to epoll object: %s", strerror(errno));
goto fail;
log_error("Failed to add server fd to epoll object: %m");
return -errno;
}
if ((r = fdset_put(s->syslog_fds, fd)) < 0) {
r = fdset_put(s->syslog_fds, fd);
if (r < 0) {
log_error("Failed to store file descriptor in set: %s", strerror(-r));
goto fail;
return r;
}
}
if ((s->kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) {
s->kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
if (s->kmsg_fd < 0) {
log_error("Failed to open /dev/kmsg for logging: %m");
return -errno;
}
@ -133,7 +142,8 @@ static int server_init(Server *s, unsigned n_sockets) {
sigset_add_many(&mask, SIGINT, SIGTERM, -1);
assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
if ((s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
if (s->signal_fd < 0) {
log_error("signalfd(): %m");
return -errno;
}
@ -148,80 +158,6 @@ static int server_init(Server *s, unsigned n_sockets) {
}
return 0;
fail:
server_done(s);
return r;
}
static void skip_date(const char **buf) {
enum {
LETTER,
SPACE,
NUMBER,
SPACE_OR_NUMBER,
COLON
} sequence[] = {
LETTER, LETTER, LETTER,
SPACE,
SPACE_OR_NUMBER, NUMBER,
SPACE,
SPACE_OR_NUMBER, NUMBER,
COLON,
SPACE_OR_NUMBER, NUMBER,
COLON,
SPACE_OR_NUMBER, NUMBER,
SPACE
};
const char *p;
unsigned i;
assert(buf);
assert(*buf);
p = *buf;
for (i = 0; i < ELEMENTSOF(sequence); i++, p++) {
if (!*p)
return;
switch (sequence[i]) {
case SPACE:
if (*p != ' ')
return;
break;
case SPACE_OR_NUMBER:
if (*p == ' ')
break;
/* fall through */
case NUMBER:
if (*p < '0' || *p > '9')
return;
break;
case LETTER:
if (!(*p >= 'A' && *p <= 'Z') &&
!(*p >= 'a' && *p <= 'z'))
return;
break;
case COLON:
if (*p != ':')
return;
break;
}
}
*buf = p;
}
static int read_process(const char **buf, struct iovec *iovec) {
@ -266,28 +202,6 @@ static int read_process(const char **buf, struct iovec *iovec) {
return 1;
}
static void skip_pid(const char **buf) {
const char *p;
assert(buf);
assert(*buf);
p = *buf;
if (*p != '[')
return;
p++;
p += strspn(p, "0123456789");
if (*p != ']')
return;
p++;
*buf = p;
}
static int write_message(Server *s, const char *buf, struct ucred *ucred) {
ssize_t k;
char priority[6], pid[16];
@ -314,14 +228,14 @@ static int write_message(Server *s, const char *buf, struct ucred *ucred) {
IOVEC_SET_STRING(iovec[i++], priority);
/* Second, skip date */
skip_date(&buf);
skip_syslog_date((char**) &buf);
/* Then, add process if set */
if (read_process(&buf, &iovec[i]) > 0)
i++;
else if (ucred &&
ucred->pid > 0 &&
get_process_name(ucred->pid, &process) >= 0)
get_process_comm(ucred->pid, &process) >= 0)
IOVEC_SET_STRING(iovec[i++], process);
/* Skip the stored PID if we have a better one */
@ -330,7 +244,7 @@ static int write_message(Server *s, const char *buf, struct ucred *ucred) {
char_array_0(pid);
IOVEC_SET_STRING(iovec[i++], pid);
skip_pid(&buf);
skip_syslog_pid((char**) &buf);
if (*buf == ':')
buf++;
@ -368,7 +282,8 @@ static int process_event(Server *s, struct epoll_event *ev) {
struct signalfd_siginfo sfsi;
ssize_t n;
if ((n = read(s->signal_fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) {
n = read(s->signal_fd, &sfsi, sizeof(sfsi));
if (n != sizeof(sfsi)) {
if (n >= 0)
return -EIO;
@ -407,7 +322,8 @@ static int process_event(Server *s, struct epoll_event *ev) {
msghdr.msg_control = &control;
msghdr.msg_controllen = sizeof(control);
if ((n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT)) < 0) {
n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT);
if (n < 0) {
if (errno == EINTR || errno == EAGAIN)
return 1;
@ -424,12 +340,14 @@ static int process_event(Server *s, struct epoll_event *ev) {
else
ucred = NULL;
if ((e = memchr(buf, '\n', n)))
e = memchr(buf, '\n', n);
if (e)
*e = 0;
else
buf[n] = 0;
if ((k = write_message(s, strstrip(buf), ucred)) < 0)
k = write_message(s, strstrip(buf), ucred);
if (k < 0)
return k;
}
}
@ -439,7 +357,7 @@ static int process_event(Server *s, struct epoll_event *ev) {
int main(int argc, char *argv[]) {
Server server;
int r = EXIT_FAILURE, n;
int r;
if (getppid() != 1) {
log_error("This program should be invoked by init only.");
@ -457,18 +375,9 @@ int main(int argc, char *argv[]) {
umask(0022);
if ((n = sd_listen_fds(true)) < 0) {
log_error("Failed to read listening file descriptors from environment: %s", strerror(-r));
return EXIT_FAILURE;
}
if (n <= 0 || n > SERVER_FD_MAX) {
log_error("No or too many file descriptors passed.");
return EXIT_FAILURE;
}
if (server_init(&server, (unsigned) n) < 0)
return EXIT_FAILURE;
r = server_init(&server);
if (r < 0)
goto finish;
log_debug("systemd-kmsg-syslogd running as pid %lu", (unsigned long) getpid());
@ -478,36 +387,33 @@ int main(int argc, char *argv[]) {
for (;;) {
struct epoll_event event;
int k;
if ((k = epoll_wait(server.epoll_fd, &event, 1, -1)) < 0) {
r = epoll_wait(server.epoll_fd, &event, 1, -1);
if (r < 0) {
if (errno == EINTR)
continue;
log_error("epoll_wait() failed: %m");
goto fail;
}
if (k <= 0)
r = -errno;
goto finish;
} else if (r == 0)
break;
if ((k = process_event(&server, &event)) < 0)
goto fail;
if (k == 0)
r = process_event(&server, &event);
if (r < 0)
goto finish;
else if (r == 0)
break;
}
r = EXIT_SUCCESS;
log_debug("systemd-kmsg-syslogd stopped as pid %lu", (unsigned long) getpid());
fail:
finish:
sd_notify(false,
"STATUS=Shutting down...");
server_done(&server);
return r;
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}

View File

@ -393,7 +393,7 @@ static void print_session_status_info(SessionStatusInfo *i) {
printf("\t Leader: %u", (unsigned) i->leader);
get_process_name(i->leader, &t);
get_process_comm(i->leader, &t);
if (t) {
printf(" (%s)", t);
free(t);

View File

@ -31,21 +31,12 @@
#include "macro.h"
#include "util.h"
#include "log.h"
static void make_v4_uuid(unsigned char *id) {
/* Stolen from generate_random_uuid() of drivers/char/random.c
* in the kernel sources */
/* Set UUID version to 4 --- truly random generation */
id[6] = (id[6] & 0x0F) | 0x40;
/* Set the UUID variant to DCE */
id[8] = (id[8] & 0x3F) | 0x80;
}
#include "sd-id128.h"
static int generate(char id[34]) {
int fd;
unsigned char buf[16], *p;
int fd, r;
unsigned char *p;
sd_id128_t buf;
char *q;
ssize_t k;
@ -68,26 +59,13 @@ static int generate(char id[34]) {
}
/* If that didn't work, generate a random machine id */
fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (fd < 0) {
log_error("Failed to open /dev/urandom: %m");
return -errno;
r = sd_id128_randomize(&buf);
if (r < 0) {
log_error("Failed to open /dev/urandom: %s", strerror(-r));
return r;
}
k = loop_read(fd, buf, sizeof(buf), false);
close_nointr_nofail(fd);
if (k != sizeof(buf)) {
log_error("Failed to read /dev/urandom: %s", strerror(k < 0 ? -k : EIO));
return k < 0 ? (int) k : -EIO;
}
/* Turn this into a valid v4 UUID, to be nice. Note that we
* only guarantee this for newly generated UUIDs, not for
* pre-existing ones.*/
make_v4_uuid(buf);
for (p = buf, q = id; p < buf + sizeof(buf); p++, q += 2) {
for (p = buf.bytes, q = id; p < buf.bytes + sizeof(buf); p++, q += 2) {
q[0] = hexchar(*p >> 4);
q[1] = hexchar(*p & 15);
}

View File

@ -2024,7 +2024,7 @@ static int manager_dispatch_sigchld(Manager *m) {
if (si.si_code == CLD_EXITED || si.si_code == CLD_KILLED || si.si_code == CLD_DUMPED) {
char *name = NULL;
get_process_name(si.si_pid, &name);
get_process_comm(si.si_pid, &name);
log_debug("Got SIGCHLD for process %lu (%s)", (unsigned long) si.si_pid, strna(name));
free(name);
}
@ -2109,7 +2109,7 @@ static int manager_process_signal_fd(Manager *m) {
if (sfsi.ssi_pid > 0) {
char *p = NULL;
get_process_name(sfsi.ssi_pid, &p);
get_process_comm(sfsi.ssi_pid, &p);
log_debug("Received SIG%s from PID %lu (%s).",
signal_to_string(sfsi.ssi_signo),

View File

@ -163,42 +163,24 @@ static int get_user_data(
const char *username = NULL;
struct passwd *pw = NULL;
uid_t uid;
int r;
bool have_loginuid = false;
char *s;
assert(handle);
assert(ret_username);
assert(ret_pw);
if (have_effective_cap(CAP_AUDIT_CONTROL) > 0) {
/* Only use audit login uid if we are executed with
* sufficient capabilities so that pam_loginuid could
* do its job. If we are lacking the CAP_AUDIT_CONTROL
* capabality we most likely are being run in a
* container and /proc/self/loginuid is useless since
* it probably contains a uid of the host system. */
if (read_one_line_file("/proc/self/loginuid", &s) >= 0) {
uid_t uid;
r = parse_uid(s, &uid);
free(s);
if (r >= 0 && uid != (uint32_t) -1) {
have_loginuid = true;
pw = pam_modutil_getpwuid(handle, uid);
}
}
}
if (!have_loginuid) {
if ((r = pam_get_user(handle, &username, NULL)) != PAM_SUCCESS) {
r = audit_loginuid_from_pid(0, &uid);
if (r >= 0)
pw = pam_modutil_getpwuid(handle, uid);
else {
r = pam_get_user(handle, &username, NULL);
if (r != PAM_SUCCESS) {
pam_syslog(handle, LOG_ERR, "Failed to get user name.");
return r;
}
if (!username || !*username) {
if (isempty(username)) {
pam_syslog(handle, LOG_ERR, "User name not valid.");
return PAM_AUTH_ERR;
}

210
src/sd-id128.c Normal file
View File

@ -0,0 +1,210 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "sd-id128.h"
#include "util.h"
#include "macro.h"
char *sd_id128_to_string(sd_id128_t id, char s[33]) {
unsigned n;
assert(s);
for (n = 0; n < 16; n++) {
s[n*2] = hexchar(id.bytes[n] >> 4);
s[n*2+1] = hexchar(id.bytes[n] & 0xF);
}
s[32] = 0;
return s;
}
int sd_id128_from_string(const char s[33], sd_id128_t *ret) {
unsigned n;
sd_id128_t t;
assert(s);
assert(ret);
for (n = 0; n < 16; n++) {
int a, b;
a = unhexchar(s[n*2]);
if (a < 0)
return -EINVAL;
b = unhexchar(s[n*2+1]);
if (b < 0)
return -EINVAL;
t.bytes[n] = (a << 4) | b;
}
if (s[32] != 0)
return -EINVAL;
*ret = t;
return 0;
}
sd_id128_t sd_id128_make_v4_uuid(sd_id128_t id) {
/* Stolen from generate_random_uuid() of drivers/char/random.c
* in the kernel sources */
/* Set UUID version to 4 --- truly random generation */
id.bytes[6] = (id.bytes[6] & 0x0F) | 0x40;
/* Set the UUID variant to DCE */
id.bytes[8] = (id.bytes[8] & 0x3F) | 0x80;
return id;
}
int sd_id128_get_machine(sd_id128_t *ret) {
static __thread sd_id128_t saved_machine_id;
static __thread bool saved_machine_id_valid = false;
int fd;
char buf[32];
ssize_t k;
unsigned j;
sd_id128_t t;
if (saved_machine_id_valid) {
*ret = saved_machine_id;
return 0;
}
fd = open("/etc/machine-id", O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (fd < 0)
return -errno;
k = loop_read(fd, buf, 32, false);
close_nointr_nofail(fd);
if (k < 0)
return (int) k;
if (k < 32)
return -EIO;
for (j = 0; j < 16; j++) {
int a, b;
a = unhexchar(buf[j*2]);
b = unhexchar(buf[j*2+1]);
if (a < 0 || b < 0)
return -EIO;
t.bytes[j] = a << 4 | b;
}
saved_machine_id = t;
saved_machine_id_valid = true;
*ret = t;
return 0;
}
int sd_id128_get_boot(sd_id128_t *ret) {
static __thread sd_id128_t saved_boot_id;
static __thread bool saved_boot_id_valid = false;
int fd;
char buf[36];
ssize_t k;
unsigned j;
sd_id128_t t;
char *p;
if (saved_boot_id_valid) {
*ret = saved_boot_id;
return 0;
}
fd = open("/proc/sys/kernel/random/boot_id", O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (fd < 0)
return -errno;
k = loop_read(fd, buf, 36, false);
close_nointr_nofail(fd);
if (k < 0)
return (int) k;
if (k < 36)
return -EIO;
for (j = 0, p = buf; j < 16; j++) {
int a, b;
if (*p == '-')
p++;
a = unhexchar(p[0]);
b = unhexchar(p[1]);
if (a < 0 || b < 0)
return -EIO;
t.bytes[j] = a << 4 | b;
p += 2;
}
saved_boot_id = t;
saved_boot_id_valid = true;
*ret = t;
return 0;
}
int sd_id128_randomize(sd_id128_t *ret) {
int fd;
ssize_t k;
sd_id128_t t;
assert(ret);
fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (fd < 0)
return -errno;
k = loop_read(fd, &t, 16, false);
close_nointr_nofail(fd);
if (k < 0)
return (int) k;
if (k < 16)
return -EIO;
/* Turn this into a valid v4 UUID, to be nice. Note that we
* only guarantee this for newly generated UUIDs, not for
* pre-existing ones.*/
*ret = sd_id128_make_v4_uuid(t);
return 0;
}

56
src/sd-id128.h Normal file
View File

@ -0,0 +1,56 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
#ifndef fooid128hfoo
#define fooid128hfoo
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <inttypes.h>
#include <stdbool.h>
#include <string.h>
typedef union sd_id128 sd_id128_t;
union sd_id128 {
uint8_t bytes[16];
uint64_t qwords[2];
};
char *sd_id128_to_string(sd_id128_t id, char s[33]);
int sd_id128_from_string(const char s[33], sd_id128_t *ret);
int sd_id128_randomize(sd_id128_t *ret);
sd_id128_t sd_id128_make_v4_uuid(sd_id128_t id);
int sd_id128_get_machine(sd_id128_t *ret);
int sd_id128_get_boot(sd_id128_t *ret);
#define SD_ID128_MAKE(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) \
((sd_id128_t) { .bytes = { 0x##v0, 0x##v1, 0x##v2, 0x##v3, 0x##v4, 0x##v5, 0x##v6, 0x##v7, \
0x##v8, 0x##v9, 0x##v10, 0x##v11, 0x##v12, 0x##v13, 0x##v14, 0x##v15 }})
static inline bool sd_id128_equal(sd_id128_t a, sd_id128_t b) {
return memcmp(&a, &b, 16) == 0;
}
#endif

View File

@ -649,7 +649,8 @@ int main(int argc, char *argv[]) {
umask(0022);
if ((n = sd_listen_fds(true)) < 0) {
n = sd_listen_fds(true);
if (n < 0) {
log_error("Failed to read listening file descriptors from environment: %s", strerror(-r));
return EXIT_FAILURE;
}

View File

@ -2182,7 +2182,7 @@ static void print_status_info(UnitStatusInfo *i) {
if (i->running) {
char *t = NULL;
get_process_name(i->main_pid, &t);
get_process_comm(i->main_pid, &t);
if (t) {
printf(" (%s)", t);
free(t);
@ -2216,7 +2216,7 @@ static void print_status_info(UnitStatusInfo *i) {
printf(" Control: %u", (unsigned) i->control_pid);
get_process_name(i->control_pid, &t);
get_process_comm(i->control_pid, &t);
if (t) {
printf(" (%s)", t);
free(t);

49
src/test-id128.c Normal file
View File

@ -0,0 +1,49 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <string.h>
#include "sd-id128.h"
#include "util.h"
#include "macro.h"
#define ID128_WALDI SD_ID128_MAKE(01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 10)
int main(int argc, char *argv[]) {
sd_id128_t id, id2;
char t[33];
assert_se(sd_id128_randomize(&id) == 0);
printf("random: %s\n", sd_id128_to_string(id, t));
assert_se(sd_id128_from_string(t, &id2) == 0);
assert_se(sd_id128_equal(id, id2));
assert_se(sd_id128_get_machine(&id) == 0);
printf("machine: %s\n", sd_id128_to_string(id, t));
assert_se(sd_id128_get_boot(&id) == 0);
printf("boot: %s\n", sd_id128_to_string(id, t));
printf("waldi: %s\n", sd_id128_to_string(ID128_WALDI, t));
return 0;
}

View File

@ -55,6 +55,7 @@
#include <linux/rtc.h>
#include <glob.h>
#include <grp.h>
#include <sys/mman.h>
#include "macro.h"
#include "util.h"
@ -73,7 +74,7 @@ size_t page_size(void) {
static __thread size_t pgsz = 0;
long r;
if (_likely_(pgsz))
if (_likely_(pgsz > 0))
return pgsz;
assert_se((r = sysconf(_SC_PAGESIZE)) > 0);
@ -993,46 +994,51 @@ char *truncate_nl(char *s) {
return s;
}
int get_process_name(pid_t pid, char **name) {
char *p;
int get_process_comm(pid_t pid, char **name) {
int r;
assert(pid >= 1);
assert(name);
if (asprintf(&p, "/proc/%lu/comm", (unsigned long) pid) < 0)
return -ENOMEM;
if (pid == 0)
r = read_one_line_file("/proc/self/comm", name);
else {
char *p;
if (asprintf(&p, "/proc/%lu/comm", (unsigned long) pid) < 0)
return -ENOMEM;
r = read_one_line_file(p, name);
free(p);
r = read_one_line_file(p, name);
free(p);
}
if (r < 0)
return r;
return 0;
return r;
}
int get_process_cmdline(pid_t pid, size_t max_length, char **line) {
char *p, *r, *k;
int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) {
char *r, *k;
int c;
bool space = false;
size_t left;
FILE *f;
assert(pid >= 1);
assert(max_length > 0);
assert(line);
if (asprintf(&p, "/proc/%lu/cmdline", (unsigned long) pid) < 0)
return -ENOMEM;
if (pid == 0)
f = fopen("/proc/self/cmdline", "re");
else {
char *p;
if (asprintf(&p, "/proc/%lu/cmdline", (unsigned long) pid) < 0)
return -ENOMEM;
f = fopen(p, "re");
free(p);
f = fopen(p, "re");
free(p);
}
if (!f)
return -errno;
if (!(r = new(char, max_length))) {
r = new(char, max_length);
if (!r) {
fclose(f);
return -ENOMEM;
}
@ -1076,13 +1082,17 @@ int get_process_cmdline(pid_t pid, size_t max_length, char **line) {
free(r);
if ((h = get_process_name(pid, &t)) < 0)
if (!comm_fallback)
return -ENOENT;
h = get_process_comm(pid, &t);
if (h < 0)
return h;
h = asprintf(&r, "[%s]", t);
r = join("[", t, "]", NULL);
free(t);
if (h < 0)
if (!r)
return -ENOMEM;
}
@ -1090,6 +1100,25 @@ int get_process_cmdline(pid_t pid, size_t max_length, char **line) {
return 0;
}
int get_process_exe(pid_t pid, char **name) {
int r;
assert(name);
if (pid == 0)
r = readlink_malloc("/proc/self/exe", name);
else {
char *p;
if (asprintf(&p, "/proc/%lu/exe", (unsigned long) pid) < 0)
return -ENOMEM;
r = readlink_malloc(p, name);
free(p);
}
return r;
}
char *strnappend(const char *s, const char *suffix, size_t b) {
size_t a;
char *r;
@ -4267,7 +4296,7 @@ const char *default_term_for_tty(const char *tty) {
return term;
}
bool dirent_is_file(struct dirent *de) {
bool dirent_is_file(const struct dirent *de) {
assert(de);
if (ignore_file(de->d_name))
@ -4281,6 +4310,15 @@ bool dirent_is_file(struct dirent *de) {
return true;
}
bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) {
assert(de);
if (!dirent_is_file(de))
return false;
return endswith(de->d_name, suffix);
}
void execute_directory(const char *directory, DIR *d, char *argv[]) {
DIR *_d = NULL;
struct dirent *de;
@ -4453,6 +4491,98 @@ void parse_syslog_priority(char **p, int *priority) {
*p += k;
}
void skip_syslog_pid(char **buf) {
char *p;
assert(buf);
assert(*buf);
p = *buf;
if (*p != '[')
return;
p++;
p += strspn(p, "0123456789");
if (*p != ']')
return;
p++;
*buf = p;
}
void skip_syslog_date(char **buf) {
enum {
LETTER,
SPACE,
NUMBER,
SPACE_OR_NUMBER,
COLON
} sequence[] = {
LETTER, LETTER, LETTER,
SPACE,
SPACE_OR_NUMBER, NUMBER,
SPACE,
SPACE_OR_NUMBER, NUMBER,
COLON,
SPACE_OR_NUMBER, NUMBER,
COLON,
SPACE_OR_NUMBER, NUMBER,
SPACE
};
char *p;
unsigned i;
assert(buf);
assert(*buf);
p = *buf;
for (i = 0; i < ELEMENTSOF(sequence); i++, p++) {
if (!*p)
return;
switch (sequence[i]) {
case SPACE:
if (*p != ' ')
return;
break;
case SPACE_OR_NUMBER:
if (*p == ' ')
break;
/* fall through */
case NUMBER:
if (*p < '0' || *p > '9')
return;
break;
case LETTER:
if (!(*p >= 'A' && *p <= 'Z') &&
!(*p >= 'a' && *p <= 'z'))
return;
break;
case COLON:
if (*p != ':')
return;
break;
}
}
*buf = p;
}
int have_effective_cap(int value) {
cap_t cap;
cap_flag_value_t fv;
@ -4672,21 +4802,6 @@ int vt_disallocate(const char *name) {
return 0;
}
static int file_is_conf(const struct dirent *d, const char *suffix) {
assert(d);
if (ignore_file(d->d_name))
return 0;
if (d->d_type != DT_REG &&
d->d_type != DT_LNK &&
d->d_type != DT_UNKNOWN)
return 0;
return endswith(d->d_name, suffix);
}
static int files_add(Hashmap *h, const char *path, const char *suffix) {
DIR *dir;
struct dirent buffer, *de;
@ -4712,7 +4827,7 @@ static int files_add(Hashmap *h, const char *path, const char *suffix) {
if (!de)
break;
if (!file_is_conf(de, suffix))
if (!dirent_is_file_with_suffix(de, suffix))
continue;
if (asprintf(&p, "%s/%s", path, de->d_name) < 0) {
@ -5063,21 +5178,27 @@ int symlink_or_copy_atomic(const char *from, const char *to) {
}
int audit_session_from_pid(pid_t pid, uint32_t *id) {
char *p, *s;
char *s;
uint32_t u;
int r;
assert(pid >= 1);
assert(id);
if (have_effective_cap(CAP_AUDIT_CONTROL) <= 0)
return -ENOENT;
if (asprintf(&p, "/proc/%lu/sessionid", (unsigned long) pid) < 0)
return -ENOMEM;
if (pid == 0)
r = read_one_line_file("/proc/self/sessionid", &s);
else {
char *p;
if (asprintf(&p, "/proc/%lu/sessionid", (unsigned long) pid) < 0)
return -ENOMEM;
r = read_one_line_file(p, &s);
free(p);
}
r = read_one_line_file(p, &s);
free(p);
if (r < 0)
return r;
@ -5094,6 +5215,51 @@ int audit_session_from_pid(pid_t pid, uint32_t *id) {
return 0;
}
int audit_loginuid_from_pid(pid_t pid, uid_t *uid) {
char *s;
uid_t u;
int r;
assert(uid);
/* Only use audit login uid if we are executed with sufficient
* capabilities so that pam_loginuid could do its job. If we
* are lacking the CAP_AUDIT_CONTROL capabality we most likely
* are being run in a container and /proc/self/loginuid is
* useless since it probably contains a uid of the host
* system. */
if (have_effective_cap(CAP_AUDIT_CONTROL) <= 0)
return -ENOENT;
if (pid == 0)
r = read_one_line_file("/proc/self/loginuid", &s);
else {
char *p;
if (asprintf(&p, "/proc/%lu/loginuid", (unsigned long) pid) < 0)
return -ENOMEM;
r = read_one_line_file(p, &s);
free(p);
}
if (r < 0)
return r;
r = parse_uid(s, &u);
free(s);
if (r < 0)
return r;
if (u == (uid_t) -1)
return -ENOENT;
*uid = (uid_t) u;
return 0;
}
bool display_is_local(const char *display) {
assert(display);
@ -5700,3 +5866,21 @@ int strdup_or_null(const char *a, char **b) {
*b = c;
return 0;
}
int prot_from_flags(int flags) {
switch (flags & O_ACCMODE) {
case O_RDONLY:
return PROT_READ;
case O_WRONLY:
return PROT_WRITE;
case O_RDWR:
return PROT_READ|PROT_WRITE;
default:
return -EINVAL;
}
}

View File

@ -248,8 +248,9 @@ int parent_of_path(const char *path, char **parent);
int rmdir_parents(const char *path, const char *stop);
int get_process_name(pid_t pid, char **name);
int get_process_cmdline(pid_t pid, size_t max_length, char **line);
int get_process_comm(pid_t pid, char **name);
int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line);
int get_process_exe(pid_t pid, char **name);
char hexchar(int x);
int unhexchar(char c);
@ -274,7 +275,9 @@ bool path_equal(const char *a, const char *b);
char *ascii_strlower(char *path);
bool dirent_is_file(struct dirent *de);
bool dirent_is_file(const struct dirent *de);
bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix);
bool ignore_file(const char *filename);
bool chars_intersect(const char *a, const char *b);
@ -415,6 +418,8 @@ bool nulstr_contains(const char*nulstr, const char *needle);
bool plymouth_running(void);
void parse_syslog_priority(char **p, int *priority);
void skip_syslog_pid(char **buf);
void skip_syslog_date(char **buf);
int have_effective_cap(int value);
@ -443,6 +448,7 @@ int hwclock_get_time(struct tm *tm);
int hwclock_set_time(const struct tm *tm);
int audit_session_from_pid(pid_t pid, uint32_t *id);
int audit_loginuid_from_pid(pid_t pid, uid_t *uid);
bool display_is_local(const char *display);
int socket_from_display(const char *display, char **path);
@ -506,4 +512,6 @@ extern char **saved_argv;
bool kexec_loaded(void);
int prot_from_flags(int flags);
#endif

1
tmpfiles.d/Makefile Symbolic link
View File

@ -0,0 +1 @@
../src/Makefile