chiark / gitweb /
pam_systemd: dup the fd received from logind
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 6 Nov 2013 22:24:16 +0000 (17:24 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 7 Nov 2013 00:50:18 +0000 (19:50 -0500)
Otherwise sd_bus_message cleanup would close it.

src/libsystemd-bus/test-bus-chat.c
src/login/logind-session-dbus.c
src/login/pam-module.c

index efc19c6..a29fbdf 100644 (file)
@@ -228,6 +228,8 @@ static int server(sd_bus *bus) {
                                 goto fail;
                         }
 
+                        log_info("Received fd=%d", fd);
+
                         if (write(fd, &x, 1) < 0) {
                                 log_error("Failed to write to fd: %m");
                                 close_nointr_nofail(fd);
@@ -307,6 +309,8 @@ static void* client1(void*p) {
                 goto finish;
         }
 
+        log_info("Sending fd=%d", pp[1]);
+
         r = sd_bus_call_method(
                         bus,
                         "org.freedesktop.systemd.test",
index a559e60..167d2ab 100644 (file)
@@ -656,6 +656,15 @@ int session_send_create_reply(Session *s, sd_bus_error *error) {
         if (!p)
                 return -ENOMEM;
 
+        log_debug("Sending reply about created session: "
+                  "id=%s object_path=%s runtime_path=%s session_fd=%d seat=%s vtnr=%u",
+                  s->id,
+                  p,
+                  s->user->runtime_path,
+                  fifo_fd,
+                  s->seat ? s->seat->id : "",
+                  (uint32_t) s->vtnr);
+
         return sd_bus_reply_method_return(
                         s->manager->bus, c,
                         "soshsub",
index 0be39de..58f9742 100644 (file)
@@ -206,14 +206,14 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         if (parse_argv(handle,
                        argc, argv,
                        &class_pam,
-                       &debug) < 0) {
-                r = PAM_SESSION_ERR;
-                goto finish;
-        }
+                       &debug) < 0)
+                return PAM_SESSION_ERR;
 
         r = get_user_data(handle, &username, &pw);
-        if (r != PAM_SUCCESS)
-                goto finish;
+        if (r != PAM_SUCCESS) {
+                pam_syslog(handle, LOG_ERR, "Failed to get user data.");
+                return r;
+        }
 
         /* Make sure we don't enter a loop by talking to
          * systemd-logind when it is actually waiting for the
@@ -321,6 +321,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                 !streq(remote_host, "localhost.localdomain");
 
         /* Talk to logind over the message bus */
+
         r = sd_bus_open_system(&bus);
         if (r < 0) {
                 pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", strerror(-r));
@@ -373,8 +374,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                                 &existing);
         if (r < 0) {
                 pam_syslog(handle, LOG_ERR, "Failed to parse message: %s", strerror(-r));
-                r = PAM_SESSION_ERR;
-                goto finish;
+                return PAM_SESSION_ERR;
         }
 
         if (debug)
@@ -385,20 +385,20 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         r = pam_misc_setenv(handle, "XDG_SESSION_ID", id, 0);
         if (r != PAM_SUCCESS) {
                 pam_syslog(handle, LOG_ERR, "Failed to set session id.");
-                goto finish;
+                return r;
         }
 
         r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", runtime_path, 0);
         if (r != PAM_SUCCESS) {
                 pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
-                goto finish;
+                return r;
         }
 
         if (!isempty(seat)) {
                 r = pam_misc_setenv(handle, "XDG_SEAT", seat, 0);
                 if (r != PAM_SUCCESS) {
                         pam_syslog(handle, LOG_ERR, "Failed to set seat.");
-                        goto finish;
+                        return r;
                 }
         }
 
@@ -410,31 +410,32 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                 r = pam_misc_setenv(handle, "XDG_VTNR", buf, 0);
                 if (r != PAM_SUCCESS) {
                         pam_syslog(handle, LOG_ERR, "Failed to set virtual terminal number.");
-                        goto finish;
+                        return r;
                 }
         }
 
         r = pam_set_data(handle, "systemd.existing", INT_TO_PTR(!!existing), NULL);
         if (r != PAM_SUCCESS) {
                 pam_syslog(handle, LOG_ERR, "Failed to install existing flag.");
-                goto finish;
+                return r;
         }
 
         if (session_fd >= 0) {
+                session_fd = dup(session_fd);
+                if (session_fd < 0) {
+                        pam_syslog(handle, LOG_ERR, "Failed to dup session fd: %m");
+                        return PAM_SESSION_ERR;
+                }
+
                 r = pam_set_data(handle, "systemd.session-fd", INT_TO_PTR(session_fd+1), NULL);
                 if (r != PAM_SUCCESS) {
                         pam_syslog(handle, LOG_ERR, "Failed to install session fd.");
-                        goto finish;
+                        close_nointr_nofail(session_fd);
+                        return r;
                 }
         }
 
         return PAM_SUCCESS;
-
-finish:
-        if (session_fd >= 0)
-                close_nointr_nofail(session_fd);
-
-        return r;
 }
 
 _public_ PAM_EXTERN int pam_sm_close_session(