[systemd-commits] 3 commits - .gitignore libsystemd-daemon.pc.in libsystemd-login.pc.in Makefile.am src/sd-daemon.c src/sd-login.c src/sd-login.h src/test-login.c src/uaccess.c systemd.pc.in TODO

Lennart Poettering lennart at kemper.freedesktop.org
Thu Jul 14 14:06:38 PDT 2011


 .gitignore              |    3 
 Makefile.am             |   80 ++++++++++-
 TODO                    |    2 
 libsystemd-daemon.pc.in |   18 ++
 libsystemd-login.pc.in  |   18 ++
 src/sd-daemon.c         |   32 ++--
 src/sd-login.c          |  323 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/sd-login.h          |   51 +++++++
 src/test-login.c        |   65 +++++++++
 src/uaccess.c           |    6 
 systemd.pc.in           |    2 
 11 files changed, 575 insertions(+), 25 deletions(-)

New commits:
commit 114a50f898a89bd7784c215ac5df95ec8c45a905
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Jul 14 23:06:31 2011 +0200

    sd-daemon: turn sd-daemon.c into a shared library

diff --git a/.gitignore b/.gitignore
index cd25404..1c07f12 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
+libsystemd-daemon.pc
 libsystemd-login.pc
 test-login
 systemd-loginctl
diff --git a/Makefile.am b/Makefile.am
index 445cc1e..767baf7 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -23,6 +23,10 @@ LIBSYSTEMD_LOGIN_CURRENT=0
 LIBSYSTEMD_LOGIN_REVISION=0
 LIBSYSTEMD_LOGIN_AGE=0
 
+LIBSYSTEMD_DAEMON_CURRENT=0
+LIBSYSTEMD_DAEMON_REVISION=0
+LIBSYSTEMD_DAEMON_AGE=0
+
 # Dirs of external packages
 dbuspolicydir=@dbuspolicydir@
 dbussessionservicedir=@dbussessionservicedir@
@@ -189,6 +193,7 @@ systemgenerator_PROGRAMS += \
 endif
 
 lib_LTLIBRARIES = \
+	libsystemd-daemon.la \
         libsystemd-login.la
 
 pkginclude_HEADERS = \
@@ -422,6 +427,7 @@ EXTRA_DIST = \
 	units/quotacheck.service.in \
         units/user at .service.in \
 	systemd.pc.in \
+        libsystemd-daemon.pc.in \
         libsystemd-login.pc.in \
 	introspect.awk \
         src/73-seat-late.rules.in
@@ -486,6 +492,7 @@ dist_doc_DATA = \
 
 pkgconfigdata_DATA = \
 	systemd.pc \
+        libsystemd-daemon.pc \
         libsystemd-login.pc
 
 # Passed through intltool only
@@ -511,8 +518,7 @@ EXTRA_DIST += \
 
 noinst_LTLIBRARIES = \
 	libsystemd-basic.la \
-	libsystemd-core.la \
-	libsystemd-daemon.la
+	libsystemd-core.la
 
 libsystemd_basic_la_SOURCES = \
 	src/util.c \
@@ -601,9 +607,6 @@ libsystemd_core_la_LIBADD = \
 	$(PAM_LIBS) \
 	$(AUDIT_LIBS)
 
-libsystemd_daemon_la_SOURCES = \
-	src/sd-daemon.c
-
 # This is needed because automake is buggy in how it generates the
 # rules for C programs, but not Vala programs.  We therefore can't
 # list the .h files as dependencies if we want make dist to work.
@@ -1364,18 +1367,30 @@ pam_systemd_la_LIBADD = \
 	$(PAM_LIBS) \
         $(DBUS_LIBS)
 
-libsystemd_login_la_CFLAGS = \
+libsystemd_daemon_la_SOURCES = \
+	src/sd-daemon.c
+
+libsystemd_daemon_la_CFLAGS = \
         $(AM_CFLAGS) \
-	-fvisibility=hidden
+	-fvisibility=hidden \
+        -DSD_EXPORT_SYMBOLS
 
-libsystemd_login_la_LDFLAGS = \
+libsystemd_daemon_la_LDFLAGS = \
         -shared \
-        -version-info $(LIBSYSTEMD_LOGIN_CURRENT):$(LIBSYSTEMD_LOGIN_REVISION):$(LIBSYSTEMD_LOGIN_AGE)
+        -version-info $(LIBSYSTEMD_DAEMON_CURRENT):$(LIBSYSTEMD_DAEMON_REVISION):$(LIBSYSTEMD_DAEMON_AGE)
 
 libsystemd_login_la_SOURCES = \
         src/sd-login.c \
         src/cgroup-util.c
 
+libsystemd_login_la_CFLAGS = \
+        $(AM_CFLAGS) \
+	-fvisibility=hidden
+
+libsystemd_login_la_LDFLAGS = \
+        -shared \
+        -version-info $(LIBSYSTEMD_LOGIN_CURRENT):$(LIBSYSTEMD_LOGIN_REVISION):$(LIBSYSTEMD_LOGIN_AGE)
+
 libsystemd_login_la_LIBADD = \
 	libsystemd-basic.la
 
@@ -1512,22 +1527,22 @@ endif
 DBUS_PREPROCESS = $(CPP) -P $(DBUS_CFLAGS) -imacros dbus/dbus-protocol.h
 
 org.freedesktop.systemd1.%.xml: systemd
-	$(AM_V_GEN)$(OBJCOPY) -O binary -j introspect.$* $< $@.tmp && \
+	$(AM_V_GEN)$(LIBTOOL) --mode=execute $(OBJCOPY) -O binary -j introspect.$* $< $@.tmp && \
 		$(STRINGS) $@.tmp | $(AWK) -f $(srcdir)/introspect.awk | \
 		$(DBUS_PREPROCESS) -o $@ - && rm $@.tmp
 
 org.freedesktop.hostname1.xml: systemd-hostnamed
-	$(AM_V_GEN)$(OBJCOPY) -O binary -j introspect.hostname1 $< $@.tmp && \
+	$(AM_V_GEN)$(LIBTOOL) --mode=execute $(OBJCOPY) -O binary -j introspect.hostname1 $< $@.tmp && \
 		$(STRINGS) $@.tmp | $(AWK) -f $(srcdir)/introspect.awk | \
 		$(DBUS_PREPROCESS) -o $@ - && rm $@.tmp
 
 org.freedesktop.locale1.xml: systemd-localed
-	$(AM_V_GEN)$(OBJCOPY) -O binary -j introspect.locale1 $< $@.tmp && \
+	$(AM_V_GEN)$(LIBTOOL) --mode=execute $(OBJCOPY) -O binary -j introspect.locale1 $< $@.tmp && \
 		$(STRINGS) $@.tmp | $(AWK) -f $(srcdir)/introspect.awk | \
 		$(DBUS_PREPROCESS) -o $@ - && rm $@.tmp
 
 org.freedesktop.timedate1.xml: systemd-timedated
-	$(AM_V_GEN)$(OBJCOPY) -O binary -j introspect.timedate1 $< $@.tmp && \
+	$(AM_V_GEN)$(LIBTOOL) --mode=execute $(OBJCOPY) -O binary -j introspect.timedate1 $< $@.tmp && \
 		$(STRINGS) $@.tmp | $(AWK) -f $(srcdir)/introspect.awk | \
 		$(DBUS_PREPROCESS) -o $@ - && rm $@.tmp
 
diff --git a/libsystemd-daemon.pc.in b/libsystemd-daemon.pc.in
new file mode 100644
index 0000000..2b6b9d6
--- /dev/null
+++ b/libsystemd-daemon.pc.in
@@ -0,0 +1,18 @@
+#  This file is part of systemd.
+#
+#  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.
+
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: systemd
+Description: systemd Daemon Utility Library
+URL: @PACKAGE_URL@
+Version: @PACKAGE_VERSION@
+Libs: -L${libdir} -lsystemd-daemon
+Cflags: -I${includedir}
diff --git a/src/sd-daemon.c b/src/sd-daemon.c
index a2ec74c..e68b708 100644
--- a/src/sd-daemon.c
+++ b/src/sd-daemon.c
@@ -49,13 +49,19 @@
 
 #include "sd-daemon.h"
 
-#if (__GNUC__ >= 4) && !defined(SD_EXPORT_SYMBOLS)
-#define _sd_hidden_ __attribute__ ((visibility("hidden")))
+#if (__GNUC__ >= 4)
+#ifdef SD_EXPORT_SYMBOLS
+/* Export symbols */
+#define _sd_export_ __attribute__ ((visibility("default")))
 #else
-#define _sd_hidden_
+/* Don't export the symbols */
+#define _sd_export_ __attribute__ ((visibility("hidden")))
+#endif
+#else
+#define _sd_export_
 #endif
 
-_sd_hidden_ int sd_listen_fds(int unset_environment) {
+_sd_export_ int sd_listen_fds(int unset_environment) {
 
 #if defined(DISABLE_SYSTEMD) || !defined(__linux__)
         return 0;
@@ -136,7 +142,7 @@ finish:
 #endif
 }
 
-_sd_hidden_ int sd_is_fifo(int fd, const char *path) {
+_sd_export_ int sd_is_fifo(int fd, const char *path) {
         struct stat st_fd;
 
         if (fd < 0)
@@ -169,7 +175,7 @@ _sd_hidden_ int sd_is_fifo(int fd, const char *path) {
         return 1;
 }
 
-_sd_hidden_ int sd_is_special(int fd, const char *path) {
+_sd_export_ int sd_is_special(int fd, const char *path) {
         struct stat st_fd;
 
         if (fd < 0)
@@ -256,7 +262,7 @@ union sockaddr_union {
         struct sockaddr_storage storage;
 };
 
-_sd_hidden_ int sd_is_socket(int fd, int family, int type, int listening) {
+_sd_export_ int sd_is_socket(int fd, int family, int type, int listening) {
         int r;
 
         if (family < 0)
@@ -284,7 +290,7 @@ _sd_hidden_ int sd_is_socket(int fd, int family, int type, int listening) {
         return 1;
 }
 
-_sd_hidden_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
+_sd_export_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
         union sockaddr_union sockaddr;
         socklen_t l;
         int r;
@@ -329,7 +335,7 @@ _sd_hidden_ int sd_is_socket_inet(int fd, int family, int type, int listening, u
         return 1;
 }
 
-_sd_hidden_ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
+_sd_export_ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
         union sockaddr_union sockaddr;
         socklen_t l;
         int r;
@@ -372,7 +378,7 @@ _sd_hidden_ int sd_is_socket_unix(int fd, int type, int listening, const char *p
         return 1;
 }
 
-_sd_hidden_ int sd_is_mq(int fd, const char *path) {
+_sd_export_ int sd_is_mq(int fd, const char *path) {
 #if !defined(__linux__)
         return 0;
 #else
@@ -409,7 +415,7 @@ _sd_hidden_ int sd_is_mq(int fd, const char *path) {
 #endif
 }
 
-_sd_hidden_ int sd_notify(int unset_environment, const char *state) {
+_sd_export_ int sd_notify(int unset_environment, const char *state) {
 #if defined(DISABLE_SYSTEMD) || !defined(__linux__) || !defined(SOCK_CLOEXEC)
         return 0;
 #else
@@ -477,7 +483,7 @@ finish:
 #endif
 }
 
-_sd_hidden_ int sd_notifyf(int unset_environment, const char *format, ...) {
+_sd_export_ int sd_notifyf(int unset_environment, const char *format, ...) {
 #if defined(DISABLE_SYSTEMD) || !defined(__linux__)
         return 0;
 #else
@@ -499,7 +505,7 @@ _sd_hidden_ int sd_notifyf(int unset_environment, const char *format, ...) {
 #endif
 }
 
-_sd_hidden_ int sd_booted(void) {
+_sd_export_ int sd_booted(void) {
 #if defined(DISABLE_SYSTEMD) || !defined(__linux__)
         return 0;
 #else

commit d40c98162feaae6e43fa51d702da4153e3ec19dc
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Jul 14 22:51:19 2011 +0200

    uaccess: become a nop when not run on a systemd system

diff --git a/src/uaccess.c b/src/uaccess.c
index 435471e..a9713a5 100644
--- a/src/uaccess.c
+++ b/src/uaccess.c
@@ -25,6 +25,7 @@
 #include "logind-acl.h"
 #include "util.h"
 #include "log.h"
+#include "sd-daemon.h"
 
 int main(int argc, char *argv[]) {
         int r;
@@ -43,6 +44,11 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
+        /* Make sure we don't muck around with ACLs the system is not
+         * running systemd. */
+        if (!sd_booted())
+                return 0;
+
         path = argv[1];
         seat = argc < 3 || isempty(argv[2]) ? "seat0" : argv[2];
 

commit 74b91131ed09850ed487a2f7849147ff6f80194d
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Jul 14 22:50:01 2011 +0200

    logind: introduce libsystemd-login.so as fast path to access logind data

diff --git a/.gitignore b/.gitignore
index b108fd9..cd25404 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
+libsystemd-login.pc
+test-login
 systemd-loginctl
 systemd-localed
 systemd-timedated
diff --git a/Makefile.am b/Makefile.am
index 53167ff..445cc1e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -19,6 +19,10 @@ ACLOCAL_AMFLAGS = -I m4
 
 SUBDIRS = po
 
+LIBSYSTEMD_LOGIN_CURRENT=0
+LIBSYSTEMD_LOGIN_REVISION=0
+LIBSYSTEMD_LOGIN_AGE=0
+
 # Dirs of external packages
 dbuspolicydir=@dbuspolicydir@
 dbussessionservicedir=@dbussessionservicedir@
@@ -35,6 +39,7 @@ pkgsysconfdir=$(sysconfdir)/systemd
 userunitdir=$(prefix)/lib/systemd/user
 tmpfilesdir=$(prefix)/lib/tmpfiles.d
 usergeneratordir=$(pkglibexecdir)/user-generators
+pkgincludedir=$(includedir)/systemd
 
 # And these are the special ones for /
 rootdir=@rootdir@
@@ -183,6 +188,12 @@ systemgenerator_PROGRAMS += \
 	systemd-cryptsetup-generator
 endif
 
+lib_LTLIBRARIES = \
+        libsystemd-login.la
+
+pkginclude_HEADERS = \
+        src/sd-login.h
+
 noinst_PROGRAMS = \
 	test-engine \
 	test-job-type \
@@ -192,7 +203,8 @@ noinst_PROGRAMS = \
 	test-daemon \
 	test-cgroup \
 	test-env-replace \
-	test-strv
+	test-strv \
+        test-login
 
 if HAVE_PAM
 pamlib_LTLIBRARIES = \
@@ -410,6 +422,7 @@ EXTRA_DIST = \
 	units/quotacheck.service.in \
         units/user at .service.in \
 	systemd.pc.in \
+        libsystemd-login.pc.in \
 	introspect.awk \
         src/73-seat-late.rules.in
 
@@ -472,7 +485,8 @@ dist_doc_DATA = \
 	src/sd-readahead.c
 
 pkgconfigdata_DATA = \
-	systemd.pc
+	systemd.pc \
+        libsystemd-login.pc
 
 # Passed through intltool only
 polkitpolicy_in_files = \
@@ -801,6 +815,16 @@ test_strv_CFLAGS = \
 test_strv_LDADD = \
 	libsystemd-basic.la
 
+test_login_SOURCES = \
+	src/test-login.c
+
+test_login_CFLAGS = \
+	$(AM_CFLAGS)
+
+test_login_LDADD = \
+	libsystemd-basic.la \
+        libsystemd-login.la
+
 systemd_logger_SOURCES = \
 	src/logger.c \
 	src/tcpwrap.c
@@ -946,6 +970,7 @@ systemd_uaccess_CFLAGS = \
 
 systemd_uaccess_LDADD = \
 	libsystemd-basic.la \
+	libsystemd-daemon.la \
         $(UDEV_LIBS) \
         $(ACL_LIBS)
 
@@ -1339,6 +1364,21 @@ pam_systemd_la_LIBADD = \
 	$(PAM_LIBS) \
         $(DBUS_LIBS)
 
+libsystemd_login_la_CFLAGS = \
+        $(AM_CFLAGS) \
+	-fvisibility=hidden
+
+libsystemd_login_la_LDFLAGS = \
+        -shared \
+        -version-info $(LIBSYSTEMD_LOGIN_CURRENT):$(LIBSYSTEMD_LOGIN_REVISION):$(LIBSYSTEMD_LOGIN_AGE)
+
+libsystemd_login_la_SOURCES = \
+        src/sd-login.c \
+        src/cgroup-util.c
+
+libsystemd_login_la_LIBADD = \
+	libsystemd-basic.la
+
 SED_PROCESS = \
 	$(AM_V_GEN)$(MKDIR_P) $(dir $@) && \
 	$(SED)  -e 's, at rootlibexecdir\@,$(rootlibexecdir),g' \
@@ -1355,6 +1395,9 @@ SED_PROCESS = \
 		-e 's, at PACKAGE_NAME\@,$(PACKAGE_NAME),g' \
 		-e 's, at PACKAGE_URL\@,$(PACKAGE_URL),g' \
 		-e 's, at prefix\@,$(prefix),g' \
+		-e 's, at exec_prefix\@,$(exec_prefix),g' \
+		-e 's, at libdir\@,$(libdir),g' \
+		-e 's, at includedir\@,$(includedir),g' \
 		< $< > $@ || rm $@
 
 units/%: units/%.in Makefile
diff --git a/TODO b/TODO
index 8a807b4..1577565 100644
--- a/TODO
+++ b/TODO
@@ -20,6 +20,8 @@ F15 External:
 
 Features:
 
+* logind: ensure ACLs are updated on login and logout
+
 * warn if the user stops a service but not its associated socket
 
 * ensure we always set the facility when logging to kmsg
diff --git a/libsystemd-login.pc.in b/libsystemd-login.pc.in
new file mode 100644
index 0000000..cd36a9c
--- /dev/null
+++ b/libsystemd-login.pc.in
@@ -0,0 +1,18 @@
+#  This file is part of systemd.
+#
+#  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.
+
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: systemd
+Description: systemd Login Utility Library
+URL: @PACKAGE_URL@
+Version: @PACKAGE_VERSION@
+Libs: -L${libdir} -lsystemd-login
+Cflags: -I${includedir}
diff --git a/src/sd-login.c b/src/sd-login.c
new file mode 100644
index 0000000..3ae850d
--- /dev/null
+++ b/src/sd-login.c
@@ -0,0 +1,323 @@
+/*-*- 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 <unistd.h>
+#include <string.h>
+#include <errno.h>
+
+#include "util.h"
+#include "cgroup-util.h"
+#include "macro.h"
+#include "sd-login.h"
+
+_public_ int sd_pid_get_session(pid_t pid, char **session) {
+        int r;
+        char *cg_process, *cg_init, *p;
+
+        if (pid == 0)
+                pid = getpid();
+
+        if (pid <= 0)
+                return -EINVAL;
+
+        if (!session)
+                return -EINVAL;
+
+        r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, pid, &cg_process);
+        if (r < 0)
+                return r;
+
+        r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &cg_init);
+        if (r < 0) {
+                free(cg_process);
+                return r;
+        }
+
+        if (endswith(cg_init, "/system"))
+                cg_init[strlen(cg_init)-7] = 0;
+        else if (streq(cg_init, "/"))
+                cg_init[0] = 0;
+
+        if (startswith(cg_process, cg_init))
+                p = cg_process + strlen(cg_init);
+        else
+                p = cg_process;
+
+        free(cg_init);
+
+        if (!startswith(p, "/user/")) {
+                free(cg_process);
+                return -ENOENT;
+        }
+
+        p += 6;
+        if (startswith(p, "shared/") || streq(p, "shared")) {
+                free(cg_process);
+                return -ENOENT;
+        }
+
+        p = strchr(p, '/');
+        if (!p) {
+                free(cg_process);
+                return -ENOENT;
+        }
+
+        p++;
+        p = strndup(p, strcspn(p, "/"));
+        free(cg_process);
+
+        if (!p)
+                return -ENOMEM;
+
+        *session = p;
+        return 0;
+}
+
+_public_ int sd_uid_get_state(uid_t uid, char**state) {
+        char *p, *s = NULL;
+        int r;
+
+        if (!state)
+                return -EINVAL;
+
+        if (asprintf(&p, "/run/systemd/users/%lu", (unsigned long) uid) < 0)
+                return -ENOMEM;
+
+        r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
+        free(p);
+
+        if (r == -ENOENT) {
+                free(s);
+                s = strdup("offline");
+                if (!s)
+                        return -ENOMEM;
+
+                *state = s;
+                return 0;
+        } else if (r < 0) {
+                free(s);
+                return r;
+        } else if (!s)
+                return -EIO;
+
+        *state = s;
+        return 0;
+}
+
+static int uid_is_on_seat_internal(uid_t uid, const char *seat, const char *variable) {
+        char *p, *w, *t, *state, *s = NULL;
+        size_t l;
+        int r;
+
+        if (!seat)
+                return -EINVAL;
+
+        p = strappend("/run/systemd/seats/", seat);
+        if (!p)
+                return -ENOMEM;
+
+        r = parse_env_file(p, NEWLINE, "UIDS", &s, NULL);
+        free(p);
+
+        if (r < 0) {
+                free(s);
+                return r;
+        }
+
+        if (!s)
+                return -EIO;
+
+        if (asprintf(&t, "%lu", (unsigned long) uid) < 0) {
+                free(s);
+                return -ENOMEM;
+        }
+
+        FOREACH_WORD(w, l, s, state) {
+                if (strncmp(t, w, l) == 0) {
+                        free(s);
+                        free(t);
+
+                        return 1;
+                }
+        }
+
+        free(s);
+        free(t);
+
+        return 0;
+}
+
+_public_ int sd_uid_is_on_seat(uid_t uid, const char *seat) {
+        return uid_is_on_seat_internal(uid, seat, "UIDS");
+}
+
+_public_ int sd_uid_is_active_on_seat(uid_t uid, const char *seat) {
+        return uid_is_on_seat_internal(uid, seat, "ACTIVE_UID");
+}
+
+_public_ int sd_session_is_active(const char *session) {
+        int r;
+        char *p, *s = NULL;
+
+        if (!session)
+                return -EINVAL;
+
+        p = strappend("/run/systemd/sessions/", session);
+        if (!p)
+                return -ENOMEM;
+
+        r = parse_env_file(p, NEWLINE, "ACTIVE", &s, NULL);
+        free(p);
+
+        if (r < 0) {
+                free(s);
+                return r;
+        }
+
+        if (!s)
+                return -EIO;
+
+        r = parse_boolean(s);
+        free(s);
+
+        return r;
+}
+
+_public_ int sd_session_get_uid(const char *session, uid_t *uid) {
+        int r;
+        char *p, *s = NULL;
+        unsigned long ul;
+
+        if (!session)
+                return -EINVAL;
+        if (!uid)
+                return -EINVAL;
+
+        p = strappend("/run/systemd/sessions/", session);
+        if (!p)
+                return -ENOMEM;
+
+        r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
+        free(p);
+
+        if (r < 0) {
+                free(s);
+                return r;
+        }
+
+        if (!s)
+                return -EIO;
+
+        r = safe_atolu(s, &ul);
+        free(s);
+
+        if (r < 0)
+                return r;
+
+        *uid = (uid_t) ul;
+        return 0;
+}
+
+_public_ int sd_session_get_seat(const char *session, char **seat) {
+        char *p, *s = NULL;
+        int r;
+
+        if (!session)
+                return -EINVAL;
+        if (!seat)
+                return -EINVAL;
+
+        p = strappend("/run/systemd/sessions/", session);
+        if (!p)
+                return -ENOMEM;
+
+        r = parse_env_file(p, NEWLINE, "SEAT", &s, NULL);
+        free(p);
+
+        if (r < 0) {
+                free(s);
+                return r;
+        }
+
+        if (isempty(s))
+                return -ENOENT;
+
+        *seat = s;
+        return 0;
+}
+
+_public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
+        char *p, *s = NULL, *t = NULL;
+        int r;
+
+        if (!seat)
+                return -EINVAL;
+        if (!session && !uid)
+                return -EINVAL;
+
+        p = strappend("/run/systemd/seats/", seat);
+        if (!p)
+                return -ENOMEM;
+
+        r = parse_env_file(p, NEWLINE,
+                           "ACTIVE", &s,
+                           "ACTIVE_UID", &t,
+                           NULL);
+        free(p);
+
+        if (r < 0) {
+                free(s);
+                free(t);
+                return r;
+        }
+
+        if (session && !s)  {
+                free(t);
+                return -EIO;
+        }
+
+        if (uid && !t) {
+                free(s);
+                return -EIO;
+        }
+
+        if (uid && t) {
+                unsigned long ul;
+
+                r = safe_atolu(t, &ul);
+                if (r < 0) {
+                        free(t);
+                        free(s);
+                        return r;
+                }
+
+                *uid = (uid_t) ul;
+        }
+
+        free(t);
+
+        if (session && s)
+                *session = s;
+        else
+                free(s);
+
+        return 0;
+}
diff --git a/src/sd-login.h b/src/sd-login.h
new file mode 100644
index 0000000..f23ac80
--- /dev/null
+++ b/src/sd-login.h
@@ -0,0 +1,51 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#ifndef foosdloginhfoo
+#define foosdloginhfoo
+
+/***
+  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/types.h>
+
+/* Get session from PID */
+int sd_pid_get_session(pid_t pid, char **session);
+
+/* Get state from uid. Possible states: offline, lingering, online, active */
+int sd_uid_get_state(uid_t uid, char**state);
+
+/* Return 1 if uid has session on seat */
+int sd_uid_is_on_seat(uid_t uid, const char *seat);
+
+/* Return 1 if uid has active session on seat */
+int sd_uid_is_active_on_seat(uid_t uid, const char *seat);
+
+/* Return 1 if the session is a active */
+int sd_session_is_active(const char *session);
+
+/* Determine user id of session */
+int sd_session_get_uid(const char *session, uid_t *uid);
+
+/* Determine seat of session */
+int sd_session_get_seat(const char *session, char **seat);
+
+/* Return active session and user of seat */
+int sd_seat_get_active(const char *seat, char **session, uid_t *uid);
+
+#endif
diff --git a/src/test-login.c b/src/test-login.c
new file mode 100644
index 0000000..97313fc
--- /dev/null
+++ b/src/test-login.c
@@ -0,0 +1,65 @@
+/*-*- 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 "sd-login.h"
+#include "util.h"
+
+int main(int argc, char* argv[]) {
+    int r, k;
+    uid_t u, u2;
+        char *seat;
+        char *session;
+        char *state;
+        char *session2;
+
+        assert_se(sd_pid_get_session(0, &session) == 0);
+        printf("session = %s\n", session);
+
+        r = sd_session_is_active(session);
+        assert_se(r >= 0);
+        printf("active = %s\n", yes_no(r));
+
+        assert_se(sd_session_get_uid(session, &u) >= 0);
+        printf("uid = %lu\n", (unsigned long) u);
+
+        assert_se(sd_session_get_seat(session, &seat) >= 0);
+        printf("seat = %s\n", seat);
+
+        assert_se(sd_uid_get_state(u, &state) >= 0);
+        printf("state = %s\n", state);
+
+        assert_se(sd_uid_is_on_seat(u, seat) > 0);
+
+        k = sd_uid_is_active_on_seat(u, seat);
+        assert_se(k >= 0);
+        assert_se(!!r == !!r);
+
+        assert_se(sd_seat_get_active(seat, &session2, &u2) >= 0);
+        printf("session2 = %s\n", session2);
+        printf("uid2 = %lu\n", (unsigned long) u2);
+
+        free(session);
+        free(state);
+        free(session2);
+        free(seat);
+
+        return 0;
+}
diff --git a/systemd.pc.in b/systemd.pc.in
index 2e2217e..29376e5 100644
--- a/systemd.pc.in
+++ b/systemd.pc.in
@@ -6,7 +6,7 @@
 #  (at your option) any later version.
 
 prefix=@prefix@
-exec_prefix=${prefix}
+exec_prefix=@exec_prefix@
 systemdsystemunitdir=@systemunitdir@
 systemduserunitdir=@userunitdir@
 systemdsystemconfdir=@pkgsysconfdir@/system



More information about the systemd-commits mailing list