[systemd-commits] 8 commits - fixme .gitignore Makefile.am src/cgroup.c src/cgroup.h src/conf-parser.c src/conf-parser.h src/dbus-service.c src/initctl.c src/install.c src/kmod-setup.c src/load-fragment.c src/logger.c src/main.c src/manager.c src/manager.h src/mount.c src/path-lookup.c src/path-lookup.h src/sd-daemon.c src/sd-daemon.h src/service.c src/service.h src/socket.c src/strv.c src/strv.h src/systemctl.c src/test-daemon.c src/unit.h src/util.c src/util.h
Lennart Poettering
lennart at kemper.freedesktop.org
Tue Jun 15 20:10:37 PDT 2010
.gitignore | 2
Makefile.am | 69 +++---
fixme | 9
src/cgroup.c | 38 ++-
src/cgroup.h | 5
src/conf-parser.c | 26 +-
src/conf-parser.h | 3
src/dbus-service.c | 4
src/initctl.c | 7
src/install.c | 593 ++++++++++++++++++++++++++++++++++++++++++++++++++++
src/kmod-setup.c | 3
src/load-fragment.c | 24 +-
src/logger.c | 7
src/main.c | 4
src/manager.c | 161 ++++++++++++++
src/manager.h | 3
src/mount.c | 8
src/path-lookup.c | 34 ++
src/path-lookup.h | 2
src/sd-daemon.c | 113 +++++++++
src/sd-daemon.h | 59 +++++
src/service.c | 81 ++++++-
src/service.h | 3
src/socket.c | 8
src/strv.c | 13 +
src/strv.h | 2
src/systemctl.c | 1
src/test-daemon.c | 37 +++
src/unit.h | 3
src/util.c | 40 +++
src/util.h | 3
31 files changed, 1264 insertions(+), 101 deletions(-)
New commits:
commit 8c47c7325fa1ab72febf807f8831ff24c75fbf45
Author: Lennart Poettering <lennart at poettering.net>
Date: Wed Jun 16 05:10:31 2010 +0200
notify: add minimal readiness/status protocol for spawned daemons
diff --git a/.gitignore b/.gitignore
index ecc9f70..0ad1446 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
+test-daemon
systemd-install
org.freedesktop.systemd1.*.xml
test-ns
diff --git a/Makefile.am b/Makefile.am
index d875786..e50ae82 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -67,7 +67,8 @@ noinst_PROGRAMS = \
test-engine \
test-job-type \
test-ns \
- test-loopback
+ test-loopback \
+ test-daemon
dist_dbuspolicy_DATA = \
src/org.freedesktop.systemd1.conf
@@ -316,8 +317,10 @@ test_loopback_SOURCES = \
src/test-loopback.c \
src/loopback-setup.c
-test_loopback_CFLAGS = $(systemd_CFLAGS)
-test_loopback_LDADD = $(systemd_LDADD)
+test_daemon_SOURCES = \
+ $(BASIC_SOURCES) \
+ src/test-daemon.c \
+ src/sd-daemon.c
systemd_logger_SOURCES = \
$(BASIC_SOURCES) \
diff --git a/fixme b/fixme
index d1718ce..e39e725 100644
--- a/fixme
+++ b/fixme
@@ -1,4 +1,4 @@
-* timer
+* calendar time support in timer
* enforce max number of concurrent connection limit in sockets.
@@ -49,8 +49,6 @@
- bluetoothd (/var/run/sdp! @/org/bluez/audio!)
- distccd
-* regnerate unit/sysv search paths on daemon reload
-
* write utmp record a la upstart for processes
* run PAM session stuff
diff --git a/src/cgroup.c b/src/cgroup.c
index 330014d..108c4fc 100644
--- a/src/cgroup.c
+++ b/src/cgroup.c
@@ -535,6 +535,37 @@ int cgroup_notify_empty(Manager *m, const char *group) {
return 0;
}
+Unit* cgroup_unit_by_pid(Manager *m, pid_t pid) {
+ CGroupBonding *l, *b;
+ char *group = NULL;
+ int r;
+
+ assert(m);
+
+ if (pid <= 1)
+ return NULL;
+
+ if ((r = cgroup_get_current_controller_path(pid, m->cgroup_controller, &group)))
+ return NULL;
+
+ l = hashmap_get(m->cgroup_bondings, group);
+ free(group);
+
+ if (!l)
+ return NULL;
+
+ LIST_FOREACH(by_path, b, l) {
+
+ if (!b->unit)
+ continue;
+
+ if (b->only_us)
+ return b->unit;
+ }
+
+ return NULL;
+}
+
CGroupBonding *cgroup_bonding_find_list(CGroupBonding *first, const char *controller) {
CGroupBonding *b;
diff --git a/src/cgroup.h b/src/cgroup.h
index 26fac0a..67c7cc3 100644
--- a/src/cgroup.h
+++ b/src/cgroup.h
@@ -76,4 +76,6 @@ int manager_shutdown_cgroup(Manager *m, bool delete);
int cgroup_notify_empty(Manager *m, const char *group);
+Unit* cgroup_unit_by_pid(Manager *m, pid_t pid);
+
#endif
diff --git a/src/dbus-service.c b/src/dbus-service.c
index 6286172..f70a772 100644
--- a/src/dbus-service.c
+++ b/src/dbus-service.c
@@ -41,7 +41,8 @@
" <property name=\"ControlPID\" type=\"u\" access=\"read\"/>\n" \
" <property name=\"SysVPath\" type=\"s\" access=\"read\"/>\n" \
" <property name=\"BusName\" type=\"s\" access=\"read\"/>\n" \
- " </interface>\n"
+ " <property name=\"StatusText\" type=\"s\" access=\"read\"/>\n" \
+ " </interface>\n"
#define INTROSPECTION \
DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
@@ -76,6 +77,7 @@ DBusHandlerResult bus_service_message_handler(Unit *u, DBusMessage *message) {
{ "org.freedesktop.systemd1.Service", "ControlPID", bus_property_append_pid, "u", &u->service.control_pid },
{ "org.freedesktop.systemd1.Service", "SysVPath", bus_property_append_string, "s", u->service.sysv_path },
{ "org.freedesktop.systemd1.Service", "BusName", bus_property_append_string, "s", u->service.bus_name },
+ { "org.freedesktop.systemd1.Service", "StatusText", bus_property_append_string, "s", u->service.status_text },
{ NULL, NULL, NULL, NULL, NULL }
};
diff --git a/src/initctl.c b/src/initctl.c
index 34c3883..56ed5cd 100644
--- a/src/initctl.c
+++ b/src/initctl.c
@@ -354,6 +354,10 @@ int main(int argc, char *argv[]) {
if (server_init(&server, (unsigned) n) < 0)
return 2;
+ sd_notify(false,
+ "READY=1\n"
+ "STATUS=Processing requests...");
+
for (;;) {
struct epoll_event event;
int k;
@@ -378,6 +382,9 @@ int main(int argc, char *argv[]) {
r = 0;
fail:
+ sd_notify(false,
+ "STATUS=Shutting down...");
+
server_done(&server);
log_info("systemd-initctl stopped as pid %llu", (unsigned long long) getpid());
diff --git a/src/logger.c b/src/logger.c
index 5c7e4ee..48eee6c 100644
--- a/src/logger.c
+++ b/src/logger.c
@@ -547,6 +547,10 @@ int main(int argc, char *argv[]) {
if (server_init(&server, (unsigned) n) < 0)
return 3;
+ sd_notify(false,
+ "READY=1\n"
+ "STATUS=Processing requests...");
+
for (;;) {
struct epoll_event event;
int k;
@@ -571,6 +575,9 @@ int main(int argc, char *argv[]) {
r = 0;
fail:
+ sd_notify(false,
+ "STATUS=Shutting down...");
+
server_done(&server);
log_info("systemd-logger stopped as pid %llu", (unsigned long long) getpid());
diff --git a/src/manager.c b/src/manager.c
index 4dcdf2e..97d05b5 100644
--- a/src/manager.c
+++ b/src/manager.c
@@ -60,6 +60,67 @@
/* As soon as 5s passed since a unit was added to our GC queue, make sure to run a gc sweep */
#define GC_QUEUE_USEC_MAX (10*USEC_PER_SEC)
+/* Where clients shall send notification messages to */
+#define NOTIFY_SOCKET "/org/freedesktop/systemd1/notify"
+
+static int manager_setup_notify(Manager *m) {
+ union {
+ struct sockaddr sa;
+ struct sockaddr_un un;
+ } sa;
+ struct epoll_event ev;
+ char *ne[2], **t;
+ int one = 1;
+
+ assert(m);
+
+ m->notify_watch.type = WATCH_NOTIFY;
+ if ((m->notify_watch.fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
+ log_error("Failed to allocate notification socket: %m");
+ return -errno;
+ }
+
+ zero(sa);
+ sa.sa.sa_family = AF_UNIX;
+
+ if (m->running_as == MANAGER_SESSION)
+ snprintf(sa.un.sun_path+1, sizeof(sa.un.sun_path)-1, NOTIFY_SOCKET "/%llu", random_ull());
+ else
+ strncpy(sa.un.sun_path+1, NOTIFY_SOCKET, sizeof(sa.un.sun_path)-1);
+
+ if (bind(m->notify_watch.fd, &sa.sa, sizeof(sa)) < 0) {
+ log_error("bind() failed: %m");
+ return -errno;
+ }
+
+ if (setsockopt(m->notify_watch.fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) {
+ log_error("SO_PASSCRED failed: %m");
+ return -errno;
+ }
+
+ zero(ev);
+ ev.events = EPOLLIN;
+ ev.data.ptr = &m->notify_watch;
+
+ if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->notify_watch.fd, &ev) < 0)
+ return -errno;
+
+ if (asprintf(&ne[0], "NOTIFY_SOCKET=@%s", sa.un.sun_path+1) < 0)
+ return -ENOMEM;
+
+ ne[1] = NULL;
+ t = strv_env_merge(m->environment, ne, NULL);
+ free(ne[0]);
+
+ if (!t)
+ return -ENOMEM;
+
+ strv_free(m->environment);
+ m->environment = t;
+
+ return 0;
+}
+
static int enable_special_signals(Manager *m) {
char fd;
@@ -177,6 +238,9 @@ int manager_new(ManagerRunningAs running_as, bool confirm_spawn, Manager **_m) {
if ((r = manager_setup_cgroup(m)) < 0)
goto fail;
+ if ((r = manager_setup_notify(m)) < 0)
+ goto fail;
+
/* Try to connect to the busses, if possible. */
if ((r = bus_init_system(m)) < 0 ||
(r = bus_init_api(m)) < 0)
@@ -364,6 +428,8 @@ void manager_free(Manager *m) {
close_nointr_nofail(m->epoll_fd);
if (m->signal_watch.fd >= 0)
close_nointr_nofail(m->signal_watch.fd);
+ if (m->notify_watch.fd >= 0)
+ close_nointr_nofail(m->notify_watch.fd);
lookup_paths_free(&m->lookup_paths);
strv_free(m->environment);
@@ -1521,12 +1587,82 @@ unsigned manager_dispatch_dbus_queue(Manager *m) {
return n;
}
+static int manager_process_notify_fd(Manager *m) {
+ ssize_t n;
+
+ assert(m);
+
+ for (;;) {
+ char buf[4096];
+ struct msghdr msghdr;
+ struct iovec iovec;
+ struct ucred *ucred;
+ union {
+ struct cmsghdr cmsghdr;
+ uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
+ } control;
+ Unit *u;
+ char **tags;
+
+ 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);
+
+ if ((n = recvmsg(m->notify_watch.fd, &msghdr, MSG_DONTWAIT)) <= 0) {
+ if (n >= 0)
+ return -EIO;
+
+ if (errno == EAGAIN)
+ break;
+
+ return -errno;
+ }
+
+ if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
+ control.cmsghdr.cmsg_level != SOL_SOCKET ||
+ control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
+ control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
+ log_warning("Received notify message without credentials. Ignoring.");
+ continue;
+ }
+
+ ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
+
+ if (!(u = hashmap_get(m->watch_pids, UINT32_TO_PTR(ucred->pid))))
+ if (!(u = cgroup_unit_by_pid(m, ucred->pid))) {
+ log_warning("Cannot find unit for notify message of PID %lu.", (unsigned long) ucred->pid);
+ continue;
+ }
+
+ char_array_0(buf);
+ if (!(tags = strv_split(buf, "\n\r")))
+ return -ENOMEM;
+
+ log_debug("Got notification message for unit %s", u->meta.id);
+
+ if (UNIT_VTABLE(u)->notify_message)
+ UNIT_VTABLE(u)->notify_message(u, tags);
+
+ strv_free(tags);
+ }
+
+ return 0;
+}
+
static int manager_dispatch_sigchld(Manager *m) {
assert(m);
for (;;) {
siginfo_t si;
Unit *u;
+ int r;
zero(si);
@@ -1555,6 +1691,17 @@ static int manager_dispatch_sigchld(Manager *m) {
free(name);
}
+ /* Let's flush any message the dying child might still
+ * have queued for us. This ensures that the process
+ * still exists in /proc so that we can figure out
+ * which cgroup and hence unit it belongs to. */
+ if ((r = manager_process_notify_fd(m)) < 0)
+ return r;
+
+ /* And now figure out the unit this belongs to */
+ if (!(u = hashmap_get(m->watch_pids, UINT32_TO_PTR(si.si_pid))))
+ u = cgroup_unit_by_pid(m, si.si_pid);
+
/* And now, we actually reap the zombie. */
if (waitid(P_PID, si.si_pid, &si, WEXITED) < 0) {
if (errno == EINTR)
@@ -1572,11 +1719,12 @@ static int manager_dispatch_sigchld(Manager *m) {
si.si_status,
strna(si.si_code == CLD_EXITED ? exit_status_to_string(si.si_status) : strsignal(si.si_status)));
- if (!(u = hashmap_remove(m->watch_pids, UINT32_TO_PTR(si.si_pid))))
+ if (!u)
continue;
log_debug("Child %llu belongs to %s", (long long unsigned) si.si_pid, u->meta.id);
+ hashmap_remove(m->watch_pids, UINT32_TO_PTR(si.si_pid));
UNIT_VTABLE(u)->sigchld_event(u, si.si_pid, si.si_code, si.si_status);
}
@@ -1738,6 +1886,17 @@ static int process_event(Manager *m, struct epoll_event *ev) {
break;
+ case WATCH_NOTIFY:
+
+ /* An incoming daemon notification event? */
+ if (ev->events != EPOLLIN)
+ return -EINVAL;
+
+ if ((r = manager_process_notify_fd(m)) < 0)
+ return r;
+
+ break;
+
case WATCH_FD:
/* Some fd event, to be dispatched to the units */
diff --git a/src/manager.h b/src/manager.h
index d78bcf5..070a27f 100644
--- a/src/manager.h
+++ b/src/manager.h
@@ -56,6 +56,7 @@ typedef enum ManagerRunningAs {
enum WatchType {
WATCH_INVALID,
WATCH_SIGNAL,
+ WATCH_NOTIFY,
WATCH_FD,
WATCH_TIMER,
WATCH_MOUNT,
@@ -171,6 +172,7 @@ struct Manager {
Hashmap *watch_pids; /* pid => Unit object n:1 */
+ Watch notify_watch;
Watch signal_watch;
int epoll_fd;
@@ -215,7 +217,6 @@ struct Manager {
char *cgroup_hierarchy;
usec_t gc_queue_timestamp;
-
int gc_marker;
unsigned n_in_gc_queue;
diff --git a/src/mount.c b/src/mount.c
index 5577f16..a8f3d7b 100644
--- a/src/mount.c
+++ b/src/mount.c
@@ -921,12 +921,14 @@ static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
assert(m);
assert(pid >= 0);
- success = is_clean_exit(code, status);
- m->failure = m->failure || !success;
+ if (pid != m->control_pid)
+ return;
- assert(m->control_pid == pid);
m->control_pid = 0;
+ success = is_clean_exit(code, status);
+ m->failure = m->failure || !success;
+
if (m->control_command) {
exec_status_fill(&m->control_command->exec_status, pid, code, status);
m->control_command = NULL;
diff --git a/src/sd-daemon.c b/src/sd-daemon.c
index 29bd204..0dad73f 100644
--- a/src/sd-daemon.c
+++ b/src/sd-daemon.c
@@ -24,6 +24,10 @@
SOFTWARE.
***/
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
@@ -34,12 +38,14 @@
#include <errno.h>
#include <unistd.h>
#include <string.h>
+#include <stdarg.h>
+#include <stdio.h>
#include "sd-daemon.h"
int sd_listen_fds(int unset_environment) {
-#ifdef DISABLE_SYSTEMD
+#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
return 0;
#else
int r, fd;
@@ -317,3 +323,108 @@ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t
return 1;
}
+
+int sd_notify(int unset_environment, const char *state) {
+#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
+ return 0;
+#else
+ int fd = -1, r;
+ struct msghdr msghdr;
+ struct iovec iovec;
+ union sockaddr_union sockaddr;
+ struct ucred *ucred;
+ union {
+ struct cmsghdr cmsghdr;
+ uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
+ } control;
+ const char *e;
+
+ if (!state) {
+ r = -EINVAL;
+ goto finish;
+ }
+
+ if (!(e = getenv("NOTIFY_SOCKET"))) {
+ r = 0;
+ goto finish;
+ }
+
+ /* Must be an abstract socket, or an absolute path */
+ if ((e[0] != '@' && e[0] != '/') || e[1] == 0) {
+ r = -EINVAL;
+ goto finish;
+ }
+
+ if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
+ r = -errno;
+ goto finish;
+ }
+
+ memset(&sockaddr, 0, sizeof(sockaddr));
+ sockaddr.sa.sa_family = AF_UNIX;
+ strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
+
+ if (sockaddr.un.sun_path[0] == '@')
+ sockaddr.un.sun_path[0] = 0;
+
+ memset(&iovec, 0, sizeof(iovec));
+ iovec.iov_base = (char*) state;
+ iovec.iov_len = strlen(state);
+
+ memset(&control, 0, sizeof(control));
+ control.cmsghdr.cmsg_level = SOL_SOCKET;
+ control.cmsghdr.cmsg_type = SCM_CREDENTIALS;
+ control.cmsghdr.cmsg_len = CMSG_LEN(sizeof(struct ucred));
+
+ ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
+ ucred->pid = getpid();
+ ucred->uid = getuid();
+ ucred->gid = getgid();
+
+ memset(&msghdr, 0, sizeof(msghdr));
+ msghdr.msg_name = &sockaddr;
+ msghdr.msg_namelen = sizeof(struct sockaddr_un);
+ msghdr.msg_iov = &iovec;
+ msghdr.msg_iovlen = 1;
+ msghdr.msg_control = &control;
+ msghdr.msg_controllen = control.cmsghdr.cmsg_len;
+
+ if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
+ r = -errno;
+ goto finish;
+ }
+
+ r = 0;
+
+finish:
+ if (unset_environment)
+ unsetenv("NOTIFY_SOCKET");
+
+ if (fd >= 0)
+ close(fd);
+
+ return r;
+#endif
+}
+
+int sd_notifyf(int unset_environment, const char *format, ...) {
+#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
+ return 0;
+#else
+ va_list ap;
+ char *p = NULL;
+ int r;
+
+ va_start(ap, format);
+ r = vasprintf(&p, format, ap);
+ va_end(ap);
+
+ if (r < 0 || !p)
+ return -ENOMEM;
+
+ r = sd_notify(unset_environment, p);
+ free(p);
+
+ return r;
+#endif
+}
diff --git a/src/sd-daemon.h b/src/sd-daemon.h
index 0d8de45..0277b0f 100644
--- a/src/sd-daemon.h
+++ b/src/sd-daemon.h
@@ -27,8 +27,13 @@
SOFTWARE.
***/
+#include <sys/types.h>
#include <inttypes.h>
+#ifdef __cplusplus
+extern "C" {
+#endif
+
/* Reference implementation of a few systemd related interfaces for
* writing daemons. These interfaces are trivial to implement. To
* simplify porting we provide this reference
@@ -111,4 +116,58 @@ int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port
* errno style error code on failure. */
int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length);
+/* Informs systemd about changed daemon state. This takes a numeber of
+ * newline seperated environment-style variable assignments in a
+ * string. The following strings are known:
+ *
+ * READY=1 Tells systemd that daemon startup is finished (only
+ * relevant for services of Type=notify). The passed
+ * argument is a boolean "1" or "0". Since there is
+ * little value in signalling non-readiness the only
+ * value daemons should send is "READY=1".
+ *
+ * STATUS=... Passes a status string back to systemd that
+ * describes the daemon state. This is free-from and
+ * can be used for various purposes: general state
+ * feedback, fsck-like programs could pass completion
+ * percentages and failing programs could pass a human
+ * readable error message. Example: "STATUS=Completed
+ * 66% of file system check..."
+ *
+ * ERRNO=... If a daemon fails, the errno-style error code,
+ * formatted as string. Example: "ERRNO=2" for ENOENT.
+ *
+ * BUSERROR=... If a daemon fails, the D-Bus error-style error
+ * code. Example: "BUSERROR=org.freedesktop.DBus.Error.TimedOut"
+ *
+ * MAINPID=... The main pid of a daemon, in case systemd did not
+ * fork off the process itself. Example: "MAINPID=4711"
+ *
+ * See sd_notifyf() for more complete examples.
+ */
+int sd_notify(int unset_environment, const char *state);
+
+/* Similar to sd_send_state() but takes a format string.
+ *
+ * Example 1: A daemon could send the following after initialization:
+ *
+ * sd_notifyf(0, "READY=1\n"
+ * "STATUS=Processing requests...\n"
+ * "MAINPID=%lu",
+ * (unsigned long) getpid());
+ *
+ * Example 2: A daemon could send the following shortly before
+ * exiting, on failure:
+ *
+ * sd_notifyf(0, "STATUS=Failed to start up: %s\n"
+ * "ERRNO=%i",
+ * strerror(errno),
+ * errno);
+ */
+int sd_notifyf(int unset_environment, const char *format, ...);
+
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/src/service.c b/src/service.c
index 547a555..6e35a4d 100644
--- a/src/service.c
+++ b/src/service.c
@@ -149,6 +149,9 @@ static void service_done(Unit *u) {
free(s->sysv_runlevels);
s->sysv_runlevels = NULL;
+ free(s->status_text);
+ s->status_text = NULL;
+
exec_context_done(&s->exec_context);
exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
s->control_command = NULL;
@@ -907,6 +910,10 @@ static void service_dump(Unit *u, FILE *f, const char *prefix) {
fprintf(f, "%sSysVRunLevels: %s\n",
prefix, s->sysv_runlevels);
+ if (s->status_text)
+ fprintf(f, "%sStatus Text: %s\n",
+ prefix, s->status_text);
+
free(p2);
}
@@ -1120,7 +1127,9 @@ static int service_coldplug(Unit *u) {
if ((s->deserialized_state == SERVICE_START &&
(s->type == SERVICE_FORKING ||
- s->type == SERVICE_DBUS)) ||
+ s->type == SERVICE_DBUS ||
+ s->type == SERVICE_FINISH ||
+ s->type == SERVICE_NOTIFY)) ||
s->deserialized_state == SERVICE_START_POST ||
s->deserialized_state == SERVICE_RUNNING ||
s->deserialized_state == SERVICE_RELOAD ||
@@ -1541,7 +1550,7 @@ static void service_enter_start(Service *s) {
if ((r = service_spawn(s,
s->exec_command[SERVICE_EXEC_START],
- s->type == SERVICE_FORKING || s->type == SERVICE_DBUS,
+ s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
true,
true,
true,
@@ -1569,13 +1578,15 @@ static void service_enter_start(Service *s) {
service_set_state(s, SERVICE_START);
} else if (s->type == SERVICE_FINISH ||
- s->type == SERVICE_DBUS) {
+ s->type == SERVICE_DBUS ||
+ s->type == SERVICE_NOTIFY) {
/* For finishing services we wait until the start
* process exited, too, but it is our main process. */
/* For D-Bus services we know the main pid right away,
- * but wait for the bus name to appear on the bus. */
+ * but wait for the bus name to appear on the
+ * bus. Notify services are similar. */
s->main_pid = pid;
s->main_pid_known = true;
@@ -1946,7 +1957,7 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
exec_status_fill(&s->main_exec_status, pid, code, status);
s->main_pid = 0;
- if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) {
+ if (s->type != SERVICE_FORKING) {
assert(s->exec_command[SERVICE_EXEC_START]);
s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
}
@@ -1974,7 +1985,7 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
break;
} else {
- assert(s->type == SERVICE_DBUS);
+ assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
/* Fall through */
}
@@ -2101,8 +2112,7 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
assert_not_reached("Uh, control process died at wrong time.");
}
}
- } else
- assert_not_reached("Got SIGCHLD for unkown PID");
+ }
}
static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
@@ -2195,6 +2205,57 @@ static void service_cgroup_notify_event(Unit *u) {
}
}
+static void service_notify_message(Unit *u, char **tags) {
+ Service *s = SERVICE(u);
+ const char *e;
+
+ assert(u);
+
+ log_debug("%s: Got message", u->meta.id);
+
+ /* Interpret MAINPID= */
+ if ((e = strv_find_prefix(tags, "MAINPID=")) &&
+ (s->state == SERVICE_START ||
+ s->state == SERVICE_START_POST ||
+ s->state == SERVICE_RUNNING ||
+ s->state == SERVICE_RELOAD)) {
+ unsigned long pid;
+
+ if (safe_atolu(e + 8, &pid) < 0 ||
+ (unsigned long) (pid_t) pid != pid ||
+ pid <= 1)
+ log_warning("Failed to parse %s", e);
+ else {
+ log_debug("%s: got %s", u->meta.id, e);
+ s->main_pid = (pid_t) pid;
+ }
+ }
+
+ /* Interpret READY= */
+ if (s->type == SERVICE_NOTIFY &&
+ s->state == SERVICE_START &&
+ strv_find(tags, "READY=1")) {
+ log_debug("%s: got READY=1", u->meta.id);
+
+ service_enter_start_post(s);
+ }
+
+ /* Interpret STATUS= */
+ if ((e = strv_find_prefix(tags, "STATUS="))) {
+ char *t;
+
+ if (!(t = strdup(e+7))) {
+ log_error("Failed to allocate string.");
+ return;
+ }
+
+ log_debug("%s: got %s", u->meta.id, e);
+
+ free(s->status_text);
+ s->status_text = t;
+ }
+}
+
static int service_enumerate(Manager *m) {
char **p;
unsigned i;
@@ -2456,7 +2517,8 @@ static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
[SERVICE_FORKING] = "forking",
[SERVICE_SIMPLE] = "simple",
[SERVICE_FINISH] = "finish",
- [SERVICE_DBUS] = "dbus"
+ [SERVICE_DBUS] = "dbus",
+ [SERVICE_NOTIFY] = "notify"
};
DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
@@ -2502,6 +2564,7 @@ const UnitVTable service_vtable = {
.timer_event = service_timer_event,
.cgroup_notify_empty = service_cgroup_notify_event,
+ .notify_message = service_notify_message,
.bus_name_owner_change = service_bus_name_owner_change,
.bus_query_pid_done = service_bus_query_pid_done,
diff --git a/src/service.h b/src/service.h
index 5242de5..d644e72 100644
--- a/src/service.h
+++ b/src/service.h
@@ -60,6 +60,7 @@ typedef enum ServiceType {
SERVICE_FORKING, /* forks by itself (i.e. traditional daemons) */
SERVICE_FINISH, /* we fork and wait until the program finishes (i.e. programs like fsck which run and need to finish before we continue) */
SERVICE_DBUS, /* we fork and wait until a specific D-Bus name appears on the bus */
+ SERVICE_NOTIFY, /* we fork and wait until a daemon sends us a ready message with sd_notify() */
_SERVICE_TYPE_MAX,
_SERVICE_TYPE_INVALID = -1
} ServiceType;
@@ -121,6 +122,8 @@ struct Service {
char *bus_name;
+ char *status_text;
+
RateLimit ratelimit;
int socket_fd;
diff --git a/src/socket.c b/src/socket.c
index 19f1d22..66131f8 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -1228,12 +1228,14 @@ static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) {
assert(s);
assert(pid >= 0);
- success = is_clean_exit(code, status);
- s->failure = s->failure || !success;
+ if (pid != s->control_pid)
+ return;
- assert(s->control_pid == pid);
s->control_pid = 0;
+ success = is_clean_exit(code, status);
+ s->failure = s->failure || !success;
+
if (s->control_command)
exec_status_fill(&s->control_command->exec_status, pid, code, status);
diff --git a/src/test-daemon.c b/src/test-daemon.c
new file mode 100644
index 0000000..8911b68
--- /dev/null
+++ b/src/test-daemon.c
@@ -0,0 +1,37 @@
+/*-*- Mode: C; c-basic-offset: 8 -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 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 <unistd.h>
+
+#include "sd-daemon.h"
+
+int main(int argc, char*argv[]) {
+
+ sd_notify(0, "STATUS=Starting up");
+ sleep(5);
+ sd_notify(0,
+ "STATUS=Running\n"
+ "READY=1");
+ sleep(10);
+ sd_notify(0, "STATUS=Quitting");
+
+ return 0;
+}
diff --git a/src/unit.h b/src/unit.h
index 5e61f7c..1f8874f 100644
--- a/src/unit.h
+++ b/src/unit.h
@@ -285,6 +285,9 @@ struct UnitVTable {
* ran empty */
void (*cgroup_notify_empty)(Unit *u);
+ /* Called whenever a process of this unit sends us a message */
+ void (*notify_message)(Unit *u, char **tags);
+
/* Called whenever a name thus Unit registered for comes or
* goes away. */
void (*bus_name_owner_change)(Unit *u, const char *name, const char *old_owner, const char *new_owner);
commit 17586c16bac1d5ecf7d60ef57d18e82e36c288c1
Author: Lennart Poettering <lennart at poettering.net>
Date: Wed Jun 16 05:07:57 2010 +0200
kmod: autoload unix.ko if it isn't around
diff --git a/src/kmod-setup.c b/src/kmod-setup.c
index fa856c9..e3925c5 100644
--- a/src/kmod-setup.c
+++ b/src/kmod-setup.c
@@ -31,7 +31,8 @@
static const char * const kmod_table[] = {
"autofs4", "/sys/class/misc/autofs",
- "ipv6", "/sys/module/ipv6"
+ "ipv6", "/sys/module/ipv6",
+ "unix", "/proc/net/unix"
};
int kmod_setup(void) {
commit 5515116b61d23c5ddb1c5ac74adfaf564b28be76
Author: Lennart Poettering <lennart at poettering.net>
Date: Wed Jun 16 05:07:30 2010 +0200
cgroup: drop inherit flag, this mus be fixed in the kernel
diff --git a/src/cgroup.c b/src/cgroup.c
index fb4047f..330014d 100644
--- a/src/cgroup.c
+++ b/src/cgroup.c
@@ -70,12 +70,7 @@ int cgroup_bonding_realize(CGroupBonding *b) {
goto fail;
}
- if (b->inherit)
- r = cgroup_create_cgroup_from_parent(b->cgroup, true);
- else
- r = cgroup_create_cgroup(b->cgroup, true);
-
- if (r != 0) {
+ if ((r = cgroup_create_cgroup(b->cgroup, true)) != 0) {
r = translate_error(r, errno);
goto fail;
}
diff --git a/src/cgroup.h b/src/cgroup.h
index d27c063..26fac0a 100644
--- a/src/cgroup.h
+++ b/src/cgroup.h
@@ -48,9 +48,6 @@ struct CGroupBonding {
/* When our tasks are the only ones in this group */
bool only_us:1;
-
- /* Inherit parameters from parent group */
- bool inherit:1;
};
int cgroup_bonding_realize(CGroupBonding *b);
commit 41192f6e8af0004c5d736df31c25b849e34e7ac6
Author: Lennart Poettering <lennart at poettering.net>
Date: Wed Jun 16 05:06:55 2010 +0200
main: fix help regarding --unit/systemd.unit=
diff --git a/src/main.c b/src/main.c
index d241144..9cdbf2e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -284,7 +284,7 @@ static int parse_proc_cmdline_word(const char *word) {
log_warning("Unknown kernel switch %s. Ignoring.", word);
log_info("Supported kernel switches:");
- log_info("systemd.default_unit=UNIT Default unit to start");
+ log_info("systemd.unit=UNIT Default unit to start");
log_info("systemd.log_target=console|kmsg|syslog Log target");
log_info("systemd.log_level=LEVEL Log level");
log_info("systemd.dump_core=0|1 Dump core on crash");
@@ -496,7 +496,7 @@ static int help(void) {
printf("%s [options]\n\n"
" -h --help Show this help\n"
- " --default=UNIT Set default unit\n"
+ " --unit=UNIT Set default unit\n"
" --log-level=LEVEL Set log level\n"
" --log-target=TARGET Set log target (console, syslog, kmsg, syslog-or-kmsg)\n"
" --running-as=AS Set running as (init, system, session)\n"
commit a4bfb3990be698b1f5c691a73823c040ffe919d5
Author: Lennart Poettering <lennart at poettering.net>
Date: Wed Jun 16 05:06:02 2010 +0200
strv: introduce strv_find_prefix()
diff --git a/src/strv.c b/src/strv.c
index a749096..2ebd0ee 100644
--- a/src/strv.c
+++ b/src/strv.c
@@ -31,7 +31,6 @@
char *strv_find(char **l, const char *name) {
char **i;
- assert(l);
assert(name);
STRV_FOREACH(i, l)
@@ -41,6 +40,18 @@ char *strv_find(char **l, const char *name) {
return NULL;
}
+char *strv_find_prefix(char **l, const char *name) {
+ char **i;
+
+ assert(name);
+
+ STRV_FOREACH(i, l)
+ if (startswith(*i, name))
+ return *i;
+
+ return NULL;
+}
+
void strv_free(char **l) {
char **k;
diff --git a/src/strv.h b/src/strv.h
index 11d2ea1..2d24b4e 100644
--- a/src/strv.h
+++ b/src/strv.h
@@ -28,6 +28,8 @@
#include "macro.h"
char *strv_find(char **l, const char *name);
+char *strv_find_prefix(char **l, const char *name);
+
void strv_free(char **l);
char **strv_copy(char **l) _malloc_;
unsigned strv_length(char **l);
commit d3782d60cd47f57f48a9229bdd3badbd2f4bae44
Author: Lennart Poettering <lennart at poettering.net>
Date: Wed Jun 16 05:05:36 2010 +0200
util: introduce random_ull()
diff --git a/src/util.c b/src/util.c
index 2bc90da..7140903 100644
--- a/src/util.c
+++ b/src/util.c
@@ -2141,6 +2141,26 @@ int dir_is_empty(const char *path) {
return r;
}
+unsigned long long random_ull(void) {
+ int fd;
+ uint64_t ull;
+ ssize_t r;
+
+ if ((fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0)
+ goto fallback;
+
+ r = loop_read(fd, &ull, sizeof(ull));
+ close_nointr_nofail(fd);
+
+ if (r != sizeof(ull))
+ goto fallback;
+
+ return ull;
+
+fallback:
+ return random() * RAND_MAX + random();
+}
+
static const char *const ioprio_class_table[] = {
[IOPRIO_CLASS_NONE] = "none",
[IOPRIO_CLASS_RT] = "realtime",
diff --git a/src/util.h b/src/util.h
index cacc396..0b397ac 100644
--- a/src/util.h
+++ b/src/util.h
@@ -196,6 +196,8 @@ int make_stdio(int fd);
bool is_clean_exit(int code, int status);
+unsigned long long random_ull(void);
+
#define DEFINE_STRING_TABLE_LOOKUP(name,type) \
const char *name##_to_string(type i) { \
if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
commit 10e87ee7f66b59a504c0ed2025463ba5faa69923
Author: Lennart Poettering <lennart at poettering.net>
Date: Wed Jun 16 01:58:50 2010 +0200
install: add systemd-install tool for managing alias/wants symlinks
diff --git a/.gitignore b/.gitignore
index de52b6f..ecc9f70 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
+systemd-install
org.freedesktop.systemd1.*.xml
test-ns
test-loopback
diff --git a/Makefile.am b/Makefile.am
index 20fd06f..d875786 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -50,8 +50,11 @@ rootbin_PROGRAMS = \
systemd \
systemctl
-if HAVE_GTK
bin_PROGRAMS = \
+ systemd-install
+
+if HAVE_GTK
+bin_PROGRAMS += \
systemadm
endif
@@ -275,8 +278,8 @@ systemd_SOURCES = \
$(COMMON_SOURCES) \
src/main.c
-systemd_CPPFLAGS = \
- $(AM_CPPFLAGS) \
+systemd_CFLAGS = \
+ $(AM_CFLAGS) \
$(DBUS_CFLAGS) \
$(UDEV_CFLAGS) \
$(CGROUP_CFLAGS)
@@ -290,14 +293,14 @@ test_engine_SOURCES = \
$(COMMON_SOURCES) \
src/test-engine.c
-test_engine_CPPFLAGS = $(systemd_CPPFLAGS)
+test_engine_CFLAGS = $(systemd_CFLAGS)
test_engine_LDADD = $(systemd_LDADD)
test_job_type_SOURCES = \
$(COMMON_SOURCES) \
src/test-job-type.c
-test_job_type_CPPFLAGS = $(systemd_CPPFLAGS)
+test_job_type_CFLAGS = $(systemd_CFLAGS)
test_job_type_LDADD = $(systemd_LDADD)
test_ns_SOURCES = \
@@ -305,7 +308,7 @@ test_ns_SOURCES = \
src/test-ns.c \
src/namespace.c
-test_ns_CPPFLAGS = $(systemd_CPPFLAGS)
+test_ns_CFLAGS = $(systemd_CFLAGS)
test_ns_LDADD = $(systemd_LDADD)
test_loopback_SOURCES = \
@@ -313,7 +316,7 @@ test_loopback_SOURCES = \
src/test-loopback.c \
src/loopback-setup.c
-test_loopback_CPPFLAGS = $(systemd_CPPFLAGS)
+test_loopback_CFLAGS = $(systemd_CFLAGS)
test_loopback_LDADD = $(systemd_LDADD)
systemd_logger_SOURCES = \
@@ -326,8 +329,8 @@ systemd_initctl_SOURCES = \
src/initctl.c \
src/sd-daemon.c
-systemd_initctl_CPPFLAGS = \
- $(AM_CPPFLAGS) \
+systemd_initctl_CFLAGS = \
+ $(AM_CFLAGS) \
$(DBUS_CFLAGS)
systemd_initctl_LDADD = \
@@ -337,8 +340,8 @@ systemd_cgroups_agent_SOURCES = \
$(BASIC_SOURCES) \
src/cgroups-agent.c
-systemd_cgroups_agent_CPPFLAGS = \
- $(AM_CPPFLAGS) \
+systemd_cgroups_agent_CFLAGS = \
+ $(AM_CFLAGS) \
$(DBUS_CFLAGS)
systemd_cgroups_agent_LDADD = \
@@ -348,29 +351,36 @@ systemctl_SOURCES = \
src/systemctl.c \
$(BASIC_SOURCES)
-systemctl_CPPFLAGS = $(AM_CPPFLAGS) $(DBUS_CFLAGS)
+systemctl_CFLAGS = $(AM_CFLAGS) $(DBUS_CFLAGS)
systemctl_LDADD = $(DBUS_LIBS)
-VALAFLAGS = \
- -g \
- --save-temps \
- --pkg=dbus-glib-1 \
- --pkg=posix
-
-if HAVE_GTK
-VALAFLAGS += \
- --pkg=gtk+-2.0
-endif
+systemd_install_SOURCES = \
+ src/install.c \
+ src/path-lookup.c \
+ $(BASIC_SOURCES)
-VALA_CFLAGS = \
- -Wno-unused-variable \
- -Wno-unused-function
+# We don't really link here against D-Bus, however we indirectly include D-Bus header files
+systemd_install_CFLAGS = $(AM_CFLAGS) $(DBUS_CFLAGS)
systemadm_SOURCES = \
src/systemadm.vala \
src/systemd-interfaces.vala
-systemadm_CPPFLAGS = $(AM_CPPFLAGS) $(DBUSGLIB_CFLAGS) $(GTK_CFLAGS) $(VALA_CFLAGS)
+systemadm_CFLAGS = \
+ $(AM_CFLAGS) \
+ $(DBUSGLIB_CFLAGS) \
+ $(GTK_CFLAGS) \
+ -Wno-unused-variable \
+ -Wno-unused-function \
+ -Wno-shadow \
+ -Wno-format-nonliteral
+
+systemadm_VALAFLAGS = \
+ --pkg=dbus-glib-1 \
+ --pkg=posix \
+ --pkg=gtk+-2.0 \
+ -g
+
systemadm_LDADD = $(DBUSGLIB_LIBS) $(GTK_LIBS)
SED_PROCESS = \
diff --git a/fixme b/fixme
index 57682f0..d1718ce 100644
--- a/fixme
+++ b/fixme
@@ -8,11 +8,6 @@
* implicitly import "defaults" settings file into all types
-* write .service file install tool
- [Install]
- WantedBy=graphical.target
- Names=prefdm.service
-
* service startup should be delayed if the matching socket is being started
* add #ifdefs for non-redhat builds in sysv parser
diff --git a/src/conf-parser.c b/src/conf-parser.c
index 20f7641..a6d4d92 100644
--- a/src/conf-parser.c
+++ b/src/conf-parser.c
@@ -40,6 +40,7 @@ static int next_assignment(
unsigned line,
const char *section,
const ConfigItem *t,
+ bool relaxed,
const char *lvalue,
const char *rvalue,
void *userdata) {
@@ -60,18 +61,21 @@ static int next_assignment(
if (t->section && !streq(section, t->section))
continue;
+ if (!t->parse)
+ return 0;
+
return t->parse(filename, line, section, lvalue, rvalue, t->data, userdata);
}
/* Warn about unknown non-extension fields. */
- if (!startswith(lvalue, "X-"))
+ if (!relaxed && !startswith(lvalue, "X-"))
log_info("[%s:%u] Unknown lvalue '%s' in section '%s'. Ignoring.", filename, line, lvalue, strna(section));
return 0;
}
/* Parse a variable assignment line */
-static int parse_line(const char *filename, unsigned line, char **section, const char* const * sections, const ConfigItem *t, char *l, void *userdata) {
+static int parse_line(const char *filename, unsigned line, char **section, const char* const * sections, const ConfigItem *t, bool relaxed, char *l, void *userdata) {
char *e;
l = strstrip(l);
@@ -89,7 +93,7 @@ static int parse_line(const char *filename, unsigned line, char **section, const
if (!(fn = file_in_same_dir(filename, strstrip(l+9))))
return -ENOMEM;
- r = config_parse(fn, NULL, sections, t, userdata);
+ r = config_parse(fn, NULL, sections, t, relaxed, userdata);
free(fn);
return r;
@@ -110,11 +114,8 @@ static int parse_line(const char *filename, unsigned line, char **section, const
if (!(n = strndup(l+1, k-2)))
return -ENOMEM;
- if (sections && !strv_contains((char**) sections, n)) {
- log_error("[%s:%u] Unknown section '%s'.", filename, line, n);
- free(n);
- return -EBADMSG;
- }
+ if (!relaxed && sections && !strv_contains((char**) sections, n))
+ log_info("[%s:%u] Unknown section '%s'. Ignoring.", filename, line, n);
free(*section);
*section = n;
@@ -122,6 +123,9 @@ static int parse_line(const char *filename, unsigned line, char **section, const
return 0;
}
+ if (sections && !strv_contains((char**) sections, *section))
+ return 0;
+
if (!(e = strchr(l, '='))) {
log_error("[%s:%u] Missing '='.", filename, line);
return -EBADMSG;
@@ -130,11 +134,11 @@ static int parse_line(const char *filename, unsigned line, char **section, const
*e = 0;
e++;
- return next_assignment(filename, line, *section, t, strstrip(l), strstrip(e), userdata);
+ return next_assignment(filename, line, *section, t, relaxed, strstrip(l), strstrip(e), userdata);
}
/* Go through the file and parse each line */
-int config_parse(const char *filename, FILE *f, const char* const * sections, const ConfigItem *t, void *userdata) {
+int config_parse(const char *filename, FILE *f, const char* const * sections, const ConfigItem *t, bool relaxed, void *userdata) {
unsigned line = 0;
char *section = NULL;
int r;
@@ -165,7 +169,7 @@ int config_parse(const char *filename, FILE *f, const char* const * sections, co
goto finish;
}
- if ((r = parse_line(filename, ++line, §ion, sections, t, l, userdata)) < 0)
+ if ((r = parse_line(filename, ++line, §ion, sections, t, relaxed, l, userdata)) < 0)
goto finish;
}
diff --git a/src/conf-parser.h b/src/conf-parser.h
index bea2a8e..b3b2915 100644
--- a/src/conf-parser.h
+++ b/src/conf-parser.h
@@ -23,6 +23,7 @@
***/
#include <stdio.h>
+#include <stdbool.h>
/* An abstract parser for simple, line based, shallow configuration
* files consisting of variable assignments only. */
@@ -40,7 +41,7 @@ typedef struct ConfigItem {
/* The configuration file parsing routine. Expects a table of
* config_items in *t that is terminated by an item where lvalue is
* NULL */
-int config_parse(const char *filename, FILE *f, const char* const * sections, const ConfigItem *t, void *userdata);
+int config_parse(const char *filename, FILE *f, const char* const *sections, const ConfigItem *t, bool relaxed, void *userdata);
/* Generic parsers */
int config_parse_int(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata);
diff --git a/src/install.c b/src/install.c
new file mode 100644
index 0000000..e3db637
--- /dev/null
+++ b/src/install.c
@@ -0,0 +1,593 @@
+/*-*- Mode: C; c-basic-offset: 8 -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 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/stat.h>
+#include <stdio.h>
+#include <getopt.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include "log.h"
+#include "path-lookup.h"
+#include "util.h"
+#include "macro.h"
+#include "strv.h"
+#include "conf-parser.h"
+
+static bool arg_force = false;
+
+static enum {
+ WHERE_SYSTEM,
+ WHERE_SESSION,
+ WHERE_GLOBAL,
+} arg_where = WHERE_SYSTEM;
+
+static enum {
+ ACTION_INVALID,
+ ACTION_ENABLE,
+ ACTION_DISABLE,
+ ACTION_TEST
+} arg_action = ACTION_INVALID;
+
+typedef struct {
+ char *name;
+ char *path;
+
+ char **aliases;
+ char **wanted_by;
+} InstallInfo;
+
+Hashmap *will_install = NULL, *have_installed = NULL;
+
+static int help(void) {
+
+ printf("%s [options]\n\n"
+ " -h --help Show this help\n"
+ " --force Override existing links\n"
+ " --system Install into system\n"
+ " --session Install into session\n"
+ " --global Install into all sessions\n"
+ "Commands:\n"
+ " enable [NAME...] Enable one or more units\n"
+ " disable [NAME...] Disable one or more units\n"
+ " test [NAME...] Test whether any of the specified units are enabled\n",
+ __progname);
+
+ return 0;
+}
+
+static int parse_argv(int argc, char *argv[]) {
+
+ enum {
+ ARG_SESSION = 0x100,
+ ARG_SYSTEM,
+ ARG_GLOBAL,
+ ARG_FORCE
+ };
+
+ static const struct option options[] = {
+ { "help", no_argument, NULL, 'h' },
+ { "session", no_argument, NULL, ARG_SESSION },
+ { "system", no_argument, NULL, ARG_SYSTEM },
+ { "global", no_argument, NULL, ARG_GLOBAL },
+ { "force", no_argument, NULL, ARG_FORCE },
+ { NULL, 0, NULL, 0 }
+ };
+
+ int c;
+
+ assert(argc >= 1);
+ assert(argv);
+
+ while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
+
+ switch (c) {
+
+ case 'h':
+ help();
+ return 0;
+
+ case ARG_SESSION:
+ arg_where = WHERE_SESSION;
+ break;
+
+ case ARG_SYSTEM:
+ arg_where = WHERE_SYSTEM;
+ break;
+
+ case ARG_GLOBAL:
+ arg_where = WHERE_GLOBAL;
+ break;
+
+ case ARG_FORCE:
+ arg_force = true;
+ break;
+
+ case '?':
+ return -EINVAL;
+
+ default:
+ log_error("Unknown option code %c", c);
+ return -EINVAL;
+ }
+ }
+
+ if (optind >= argc) {
+ log_error("Missing verb.");
+ return -EINVAL;
+ }
+
+ if (streq(argv[optind], "enable"))
+ arg_action = ACTION_ENABLE;
+ else if (streq(argv[optind], "disable"))
+ arg_action = ACTION_DISABLE;
+ else if (streq(argv[optind], "test"))
+ arg_action = ACTION_TEST;
+ else {
+ log_error("Unknown verb %s", argv[optind]);
+ return -EINVAL;
+ }
+
+ optind++;
+
+ if (optind >= argc) {
+ log_error("Missing unit name.");
+ return -EINVAL;
+ }
+
+ return 1;
+}
+
+static void install_info_free(InstallInfo *i) {
+ assert(i);
+
+ free(i->name);
+ free(i->path);
+ strv_free(i->aliases);
+ strv_free(i->wanted_by);
+ free(i);
+}
+
+static void install_info_hashmap_free(Hashmap *m) {
+ InstallInfo *i;
+
+ while ((i = hashmap_steal_first(m)))
+ install_info_free(i);
+
+ hashmap_free(m);
+}
+
+static bool unit_name_valid(const char *name) {
+
+ /* This is a minimal version of unit_name_valid() from
+ * unit-name.c */
+
+ if (strchr(name, '/'))
+ return false;
+
+ if (!*name)
+ return false;
+
+ if (ignore_file(name))
+ return false;
+
+ return true;
+}
+
+static int install_info_add(const char *name) {
+ InstallInfo *i;
+ int r;
+
+ if (!unit_name_valid(name))
+ return -EINVAL;
+
+ if (hashmap_get(have_installed, name) ||
+ hashmap_get(will_install, name))
+ return 0;
+
+ if (!(i = new0(InstallInfo, 1))) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ if (!(i->name = strdup(name))) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ if ((r = hashmap_put(will_install, i->name, i)) < 0)
+ goto fail;
+
+ return 0;
+
+fail:
+ if (i)
+ install_info_free(i);
+
+ return r;
+}
+
+static int config_parse_also(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ char *w;
+ size_t l;
+ char *state;
+
+ assert(filename);
+ assert(lvalue);
+ assert(rvalue);
+
+ FOREACH_WORD_QUOTED(w, l, rvalue, state) {
+ char *n;
+ int r;
+
+ if (!(n = strndup(w, l)))
+ return -ENOMEM;
+
+ r = install_info_add(n);
+ free(n);
+
+ if (r < 0)
+ return r;
+ }
+
+ return 0;
+}
+
+static int create_symlink(const char *old_path, const char *new_path) {
+ int r;
+
+ assert(old_path);
+ assert(new_path);
+
+ if (arg_action == ACTION_ENABLE) {
+ char *dest;
+
+ mkdir_parents(new_path, 0755);
+
+ if (symlink(old_path, new_path) >= 0)
+ return 0;
+
+ if (errno != EEXIST) {
+ log_error("Cannot link %s to %s: %m", old_path, new_path);
+ return -errno;
+ }
+
+ if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
+
+ if (errno == EINVAL) {
+ log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path, new_path);
+ return -EEXIST;
+ }
+
+ log_error("readlink() failed: %s", strerror(-r));
+ return r;
+ }
+
+ if (streq(dest, old_path)) {
+ free(dest);
+ return 0;
+ }
+
+ if (!arg_force) {
+ log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path, new_path, dest);
+ free(dest);
+ return -EEXIST;
+ }
+
+ free(dest);
+ unlink(new_path);
+
+ if (symlink(old_path, new_path) >= 0)
+ return 0;
+
+ log_error("Cannot link %s to %s: %m", old_path, new_path);
+ return -errno;
+
+ } else if (arg_action == ACTION_DISABLE) {
+ char *dest;
+
+ if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
+ if (errno == ENOENT)
+ return 0;
+
+ if (errno == EINVAL) {
+ log_warning("File %s not a symlink, ignoring.", old_path);
+ return 0;
+ }
+
+ log_error("readlink() failed: %s", strerror(-r));
+ return r;
+ }
+
+ if (!streq(dest, old_path)) {
+ log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path, old_path, dest);
+ free(dest);
+ return 0;
+ }
+
+ free(dest);
+ if (unlink(new_path) >= 0)
+ return 0;
+
+ log_error("Cannot unlink %s: %m", new_path);
+ return -errno;
+
+ } else if (arg_action == ACTION_TEST) {
+ char *dest;
+
+ if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
+
+ if (errno == ENOENT || errno == EINVAL)
+ return 0;
+
+ log_error("readlink() failed: %s", strerror(-r));
+ return r;
+ }
+
+ if (streq(dest, old_path)) {
+ free(dest);
+ return 1;
+ }
+
+ return 0;
+ }
+
+ assert_not_reached("Unknown action.");
+}
+
+static int install_info_symlink_alias(InstallInfo *i, const char *config_path) {
+ char **s;
+ char *alias_path = NULL;
+ int r;
+
+ assert(i);
+
+ STRV_FOREACH(s, i->aliases) {
+
+ if (!unit_name_valid(*s)) {
+ log_error("Invalid name %s.", *s);
+ r = -EINVAL;
+ goto finish;
+ }
+
+ free(alias_path);
+ if (!(alias_path = path_make_absolute(*s, config_path))) {
+ log_error("Out of memory");
+ r = -ENOMEM;
+ goto finish;
+ }
+
+ if ((r = create_symlink(i->path, alias_path)) != 0)
+ goto finish;
+ }
+
+ r = 0;
+
+finish:
+ free(alias_path);
+
+ return r;
+}
+
+static int install_info_symlink_wants(InstallInfo *i, const char *config_path) {
+ char **s;
+ char *alias_path = NULL;
+ int r;
+
+ assert(i);
+
+ STRV_FOREACH(s, i->wanted_by) {
+ if (!unit_name_valid(*s)) {
+ log_error("Invalid name %s.", *s);
+ r = -EINVAL;
+ goto finish;
+ }
+
+ free(alias_path);
+ alias_path = NULL;
+
+ if (asprintf(&alias_path, "%s/%s.wants/%s", config_path, *s, i->name) < 0) {
+ log_error("Out of memory");
+ r = -ENOMEM;
+ goto finish;
+ }
+
+ if ((r = create_symlink(i->path, alias_path)) != 0)
+ goto finish;
+
+ if (arg_action == ACTION_DISABLE) {
+ char *t;
+
+ /* Try to remove .wants dir if we don't need it anymore */
+ if (asprintf(&t, "%s/%s.wants", config_path, *s) >= 0) {
+ rmdir(t);
+ free(t);
+ }
+ }
+ }
+
+ r = 0;
+
+finish:
+ free(alias_path);
+
+ return r;
+}
+
+static int install_info_apply(LookupPaths *paths, InstallInfo *i, const char *config_path) {
+
+ const ConfigItem items[] = {
+ { "Alias", config_parse_strv, &i->aliases, "Install" },
+ { "WantedBy", config_parse_strv, &i->wanted_by, "Install" },
+ { "Also", config_parse_also, NULL, "Install" },
+
+ { NULL, NULL, NULL, NULL }
+ };
+
+ char **p;
+ char *filename = NULL;
+ FILE *f = NULL;
+ int r;
+
+ assert(paths);
+ assert(i);
+
+ STRV_FOREACH(p, paths->unit_path) {
+
+ if (!(filename = path_make_absolute(i->name, *p))) {
+ log_error("Out of memory");
+ return -ENOMEM;
+ }
+
+ if ((f = fopen(filename, "re")))
+ break;
+
+ free(filename);
+ filename = NULL;
+
+ if (errno != ENOENT) {
+ log_error("Failed to open %s: %m", filename);
+ return -errno;
+ }
+ }
+
+ if (!f) {
+ log_error("Couldn't find %s.", i->name);
+ return -ENOENT;
+ }
+
+ i->path = filename;
+
+ if ((r = config_parse(filename, f, NULL, items, true, i)) < 0) {
+ fclose(f);
+ return r;
+ }
+
+ fclose(f);
+
+ if ((r = install_info_symlink_alias(i, config_path)) != 0)
+ return r;
+
+ if ((r = install_info_symlink_wants(i, config_path)) != 0)
+ return r;
+
+ return 0;
+}
+
+static char *get_config_path(void) {
+
+ switch (arg_where) {
+
+ case WHERE_SYSTEM:
+ return strdup(SYSTEM_CONFIG_UNIT_PATH);
+
+ case WHERE_GLOBAL:
+ return strdup(SESSION_CONFIG_UNIT_PATH);
+
+ case WHERE_SESSION: {
+ char *p;
+
+ if (session_config_home(&p) < 0)
+ return NULL;
+
+ return p;
+ }
+
+ default:
+ assert_not_reached("Unknown config path.");
+ }
+}
+
+int main(int argc, char *argv[]) {
+ int r, retval = 1, j;
+ LookupPaths paths;
+ InstallInfo *i;
+ char *config_path = NULL;
+
+ zero(paths);
+
+ log_set_target(LOG_TARGET_CONSOLE);
+ log_set_max_level(LOG_INFO);
+ log_parse_environment();
+
+ if ((r = parse_argv(argc, argv)) < 0)
+ goto finish;
+ else if (r == 0) {
+ retval = 0;
+ goto finish;
+ }
+
+ if ((r = lookup_paths_init(&paths, arg_where == WHERE_SYSTEM ? MANAGER_INIT : MANAGER_SESSION)) < 0) {
+ log_error("Failed to determine lookup paths: %s", strerror(-r));
+ goto finish;
+ }
+
+ if (!(config_path = get_config_path())) {
+ log_error("Failed to determine config path");
+ goto finish;
+ }
+
+ will_install = hashmap_new(string_hash_func, string_compare_func);
+ have_installed = hashmap_new(string_hash_func, string_compare_func);
+
+ if (!will_install || !have_installed) {
+ log_error("Failed to allocate unit sets.");
+ goto finish;
+ }
+
+ for (j = optind; j < argc; j++)
+ if ((r = install_info_add(argv[j])) < 0)
+ goto finish;
+
+ while ((i = hashmap_first(will_install))) {
+ assert_se(hashmap_move_one(have_installed, will_install, i->name) == 0);
+
+ if ((r = install_info_apply(&paths, i, config_path)) != 0) {
+
+ if (r < 0)
+ goto finish;
+
+ /* In test mode and found something */
+ retval = 0;
+ goto finish;
+ }
+ }
+
+ retval = arg_action == ACTION_TEST ? 1 : 0;
+
+finish:
+ install_info_hashmap_free(will_install);
+ install_info_hashmap_free(have_installed);
+
+ lookup_paths_free(&paths);
+
+ free(config_path);
+
+ return retval;
+}
diff --git a/src/load-fragment.c b/src/load-fragment.c
index 793c952..cf1434e 100644
--- a/src/load-fragment.c
+++ b/src/load-fragment.c
@@ -1471,12 +1471,17 @@ static int load_from_path(Unit *u, const char *path) {
{ "DirectoryNotEmpty", config_parse_path_spec, &u->path, "Path" },
{ "Unit", config_parse_path_unit, &u->path, "Path" },
+ /* The [Install] section is ignored here. */
+ { "Alias", NULL, NULL, "Install" },
+ { "WantedBy", NULL, NULL, "Install" },
+ { "Also", NULL, NULL, "Install" },
+
{ NULL, NULL, NULL, NULL }
};
#undef EXEC_CONTEXT_CONFIG_ITEMS
- const char *sections[3];
+ const char *sections[4];
int r;
Set *symlink_names;
FILE *f = NULL;
@@ -1494,7 +1499,8 @@ static int load_from_path(Unit *u, const char *path) {
sections[0] = "Unit";
sections[1] = section_table[u->meta.type];
- sections[2] = NULL;
+ sections[2] = "Install";
+ sections[3] = NULL;
if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
return -ENOMEM;
@@ -1563,7 +1569,7 @@ static int load_from_path(Unit *u, const char *path) {
}
/* Now, parse the file contents */
- if ((r = config_parse(filename, f, sections, items, u)) < 0)
+ if ((r = config_parse(filename, f, sections, items, false, u)) < 0)
goto finish;
free(u->meta.fragment_path);
diff --git a/src/path-lookup.c b/src/path-lookup.c
index 1c4b5dc..7a5b9b8 100644
--- a/src/path-lookup.c
+++ b/src/path-lookup.c
@@ -30,6 +30,28 @@
#include "path-lookup.h"
+int session_config_home(char **config_home) {
+ const char *e;
+
+ if ((e = getenv("XDG_CONFIG_HOME"))) {
+ if (asprintf(config_home, "%s/systemd/session", e) < 0)
+ return -ENOMEM;
+
+ return 1;
+ } else {
+ const char *home;
+
+ if ((home = getenv("HOME"))) {
+ if (asprintf(config_home, "%s/.config/systemd/session", home) < 0)
+ return -ENOMEM;
+
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
static char** session_dirs(void) {
const char *home, *e;
char *config_home = NULL, *data_home = NULL;
@@ -45,16 +67,10 @@ static char** session_dirs(void) {
* as data, and allow overriding as configuration.
*/
- home = getenv("HOME");
-
- if ((e = getenv("XDG_CONFIG_HOME"))) {
- if (asprintf(&config_home, "%s/systemd/session", e) < 0)
- goto fail;
+ if (session_config_home(&config_home) < 0)
+ goto fail;
- } else if (home) {
- if (asprintf(&config_home, "%s/.config/systemd/session", home) < 0)
- goto fail;
- }
+ home = getenv("HOME");
if ((e = getenv("XDG_CONFIG_DIRS")))
if (!(config_dirs = strv_split(e, ":")))
diff --git a/src/path-lookup.h b/src/path-lookup.h
index a04d5a0..e5a1092 100644
--- a/src/path-lookup.h
+++ b/src/path-lookup.h
@@ -30,6 +30,8 @@ typedef struct LookupPaths {
#include "manager.h"
+int session_config_home(char **config_home);
+
int lookup_paths_init(LookupPaths *p, ManagerRunningAs running_as);
void lookup_paths_free(LookupPaths *p);
diff --git a/src/systemctl.c b/src/systemctl.c
index 4afe53e..663cb83 100644
--- a/src/systemctl.c
+++ b/src/systemctl.c
@@ -1437,6 +1437,7 @@ int main(int argc, char*argv[]) {
dbus_error_init(&error);
log_set_target(LOG_TARGET_CONSOLE);
+ log_set_max_level(LOG_INFO);
log_parse_environment();
if ((r = parse_argv(argc, argv)) < 0)
commit 2c7108c40abfb1f175391aa59cf1b07ab203e690
Author: Lennart Poettering <lennart at poettering.net>
Date: Wed Jun 16 01:56:00 2010 +0200
util: introduce readlink_and_make_absolute()
diff --git a/src/load-fragment.c b/src/load-fragment.c
index 79b0893..793c952 100644
--- a/src/load-fragment.c
+++ b/src/load-fragment.c
@@ -1160,7 +1160,7 @@ static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
* reached by a symlink. The old string will be freed. */
for (;;) {
- char *target, *k, *name;
+ char *target, *name;
if (c++ >= FOLLOW_MAX)
return -ELOOP;
@@ -1189,17 +1189,11 @@ static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
return -errno;
/* Hmm, so this is a symlink. Let's read the name, and follow it manually */
- if ((r = readlink_malloc(*filename, &target)) < 0)
+ if ((r = readlink_and_make_absolute(*filename, &target)) < 0)
return r;
- k = file_in_same_dir(*filename, target);
- free(target);
-
- if (!k)
- return -ENOMEM;
-
free(*filename);
- *filename = k;
+ *filename = target;
}
if (!(f = fdopen(fd, "r"))) {
diff --git a/src/util.c b/src/util.c
index 7664df5..2bc90da 100644
--- a/src/util.c
+++ b/src/util.c
@@ -582,6 +582,26 @@ int readlink_malloc(const char *p, char **r) {
}
}
+int readlink_and_make_absolute(const char *p, char **r) {
+ char *target, *k;
+ int j;
+
+ assert(p);
+ assert(r);
+
+ if ((j = readlink_malloc(p, &target)) < 0)
+ return j;
+
+ k = file_in_same_dir(p, target);
+ free(target);
+
+ if (!k)
+ return -ENOMEM;
+
+ *r = k;
+ return 0;
+}
+
char *file_name_from_path(const char *p) {
char *r;
diff --git a/src/util.h b/src/util.h
index efc993c..cacc396 100644
--- a/src/util.h
+++ b/src/util.h
@@ -139,6 +139,7 @@ int read_one_line_file(const char *fn, char **line);
char *strappend(const char *s, const char *suffix);
int readlink_malloc(const char *p, char **r);
+int readlink_and_make_absolute(const char *p, char **r);
char *file_name_from_path(const char *p);
bool is_path(const char *p);
More information about the systemd-commits
mailing list