chiark / gitweb /
util: replace close_nointr_nofail() by a more useful safe_close()
[elogind.git] / src / tty-ask-password-agent / tty-ask-password-agent.c
index 0dec0629c0012daa327549ad4b7192bfc707b9bd..1d067af2295e6d3348a7aa1e4977c8a32f099ebb 100644 (file)
@@ -40,6 +40,7 @@
 #include "socket-util.h"
 #include "ask-password-api.h"
 #include "strv.h"
 #include "socket-util.h"
 #include "ask-password-api.h"
 #include "strv.h"
+#include "build.h"
 
 static enum {
         ACTION_LIST,
 
 static enum {
         ACTION_LIST,
@@ -59,11 +60,11 @@ static int ask_password_plymouth(
                 char ***_passphrases) {
 
         int fd = -1, notify = -1;
                 char ***_passphrases) {
 
         int fd = -1, notify = -1;
-        union sockaddr_union sa;
+        union sockaddr_union sa = {};
         char *packet = NULL;
         ssize_t k;
         int r, n;
         char *packet = NULL;
         ssize_t k;
         int r, n;
-        struct pollfd pollfd[2];
+        struct pollfd pollfd[2] = {};
         char buffer[LINE_MAX];
         size_t p = 0;
         enum {
         char buffer[LINE_MAX];
         size_t p = 0;
         enum {
@@ -90,7 +91,6 @@ static int ask_password_plymouth(
                 goto finish;
         }
 
                 goto finish;
         }
 
-        zero(sa);
         sa.sa.sa_family = AF_UNIX;
         strncpy(sa.un.sun_path+1, "/org/freedesktop/plymouthd", sizeof(sa.un.sun_path)-1);
         if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
         sa.sa.sa_family = AF_UNIX;
         strncpy(sa.un.sun_path+1, "/org/freedesktop/plymouthd", sizeof(sa.un.sun_path)-1);
         if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
@@ -115,7 +115,6 @@ static int ask_password_plymouth(
                 goto finish;
         }
 
                 goto finish;
         }
 
-        zero(pollfd);
         pollfd[POLL_SOCKET].fd = fd;
         pollfd[POLL_SOCKET].events = POLLIN;
         pollfd[POLL_INOTIFY].fd = notify;
         pollfd[POLL_SOCKET].fd = fd;
         pollfd[POLL_SOCKET].events = POLLIN;
         pollfd[POLL_INOTIFY].fd = notify;
@@ -235,11 +234,8 @@ static int ask_password_plymouth(
         r = 0;
 
 finish:
         r = 0;
 
 finish:
-        if (notify >= 0)
-                close_nointr_nofail(notify);
-
-        if (fd >= 0)
-                close_nointr_nofail(fd);
+        safe_close(notify);
+        safe_close(fd);
 
         free(packet);
 
 
         free(packet);
 
@@ -276,7 +272,7 @@ static int parse_password(const char *filename, char **wall) {
                 return -errno;
         }
 
                 return -errno;
         }
 
-        r = config_parse(filename, f, NULL, config_item_table_lookup, (void*) items, true, NULL);
+        r = config_parse(NULL, filename, f, NULL, config_item_table_lookup, (void*) items, true, false, NULL);
         if (r < 0) {
                 log_error("Failed to parse password file %s: %s", filename, strerror(-r));
                 goto finish;
         if (r < 0) {
                 log_error("Failed to parse password file %s: %s", filename, strerror(-r));
                 goto finish;
@@ -295,9 +291,7 @@ static int parse_password(const char *filename, char **wall) {
                 }
         }
 
                 }
         }
 
-        if (pid > 0 &&
-            kill(pid, 0) < 0 &&
-            errno == ESRCH) {
+        if (pid > 0 && !pid_is_alive(pid)) {
                 r = 0;
                 goto finish;
         }
                 r = 0;
                 goto finish;
         }
@@ -314,8 +308,7 @@ static int parse_password(const char *filename, char **wall) {
                              *wall ? "\r\n\r\n" : "",
                              message,
                              pid) < 0) {
                              *wall ? "\r\n\r\n" : "",
                              message,
                              pid) < 0) {
-                        log_error("Out of memory");
-                        r = -ENOMEM;
+                        r = log_oom();
                         goto finish;
                 }
 
                         goto finish;
                 }
 
@@ -325,7 +318,7 @@ static int parse_password(const char *filename, char **wall) {
                 union {
                         struct sockaddr sa;
                         struct sockaddr_un un;
                 union {
                         struct sockaddr sa;
                         struct sockaddr_un un;
-                } sa;
+                } sa = {};
                 size_t packet_length = 0;
 
                 assert(arg_action == ACTION_QUERY ||
                 size_t packet_length = 0;
 
                 assert(arg_action == ACTION_QUERY ||
@@ -341,7 +334,7 @@ static int parse_password(const char *filename, char **wall) {
                 }
 
                 if (arg_plymouth) {
                 }
 
                 if (arg_plymouth) {
-                        char **passwords = NULL;
+                        _cleanup_strv_free_ char **passwords = NULL;
 
                         if ((r = ask_password_plymouth(message, not_after, filename, accept_cached, &passwords)) >= 0) {
                                 char **p;
 
                         if ((r = ask_password_plymouth(message, not_after, filename, accept_cached, &passwords)) >= 0) {
                                 char **p;
@@ -365,7 +358,7 @@ static int parse_password(const char *filename, char **wall) {
 
                 } else {
                         int tty_fd = -1;
 
                 } else {
                         int tty_fd = -1;
-                        char *password;
+                        char *password = NULL;
 
                         if (arg_console)
                                 if ((tty_fd = acquire_terminal("/dev/console", false, false, false, (usec_t) -1)) < 0) {
 
                         if (arg_console)
                                 if ((tty_fd = acquire_terminal("/dev/console", false, false, false, (usec_t) -1)) < 0) {
@@ -376,7 +369,7 @@ static int parse_password(const char *filename, char **wall) {
                         r = ask_password_tty(message, not_after, filename, &password);
 
                         if (arg_console) {
                         r = ask_password_tty(message, not_after, filename, &password);
 
                         if (arg_console) {
-                                close_nointr_nofail(tty_fd);
+                                safe_close(tty_fd);
                                 release_terminal();
                         }
 
                                 release_terminal();
                         }
 
@@ -410,7 +403,6 @@ static int parse_password(const char *filename, char **wall) {
                         goto finish;
                 }
 
                         goto finish;
                 }
 
-                zero(sa);
                 sa.un.sun_family = AF_UNIX;
                 strncpy(sa.un.sun_path, socket_name, sizeof(sa.un.sun_path));
 
                 sa.un.sun_family = AF_UNIX;
                 strncpy(sa.un.sun_path, socket_name, sizeof(sa.un.sun_path));
 
@@ -424,8 +416,7 @@ static int parse_password(const char *filename, char **wall) {
 finish:
         fclose(f);
 
 finish:
         fclose(f);
 
-        if (socket_fd >= 0)
-                close_nointr_nofail(socket_fd);
+        safe_close(socket_fd);
 
         free(packet);
         free(socket_name);
 
         free(packet);
         free(socket_name);
@@ -497,7 +488,7 @@ static bool wall_tty_match(const char *path) {
                 return true;
 
         /* What, we managed to open the pipe? Then this tty is filtered. */
                 return true;
 
         /* What, we managed to open the pipe? Then this tty is filtered. */
-        close_nointr_nofail(fd);
+        safe_close(fd);
         return false;
 }
 
         return false;
 }
 
@@ -532,8 +523,7 @@ static int show_passwords(void) {
                         continue;
 
                 if (!(p = strappend("/run/systemd/ask-password/", de->d_name))) {
                         continue;
 
                 if (!(p = strappend("/run/systemd/ask-password/", de->d_name))) {
-                        log_error("Out of memory");
-                        r = -ENOMEM;
+                        r = log_oom();
                         goto finish;
                 }
 
                         goto finish;
                 }
 
@@ -544,7 +534,7 @@ static int show_passwords(void) {
                 free(p);
 
                 if (wall) {
                 free(p);
 
                 if (wall) {
-                        utmp_wall(wall, wall_tty_match);
+                        utmp_wall(wall, NULL, wall_tty_match);
                         free(wall);
                 }
         }
                         free(wall);
                 }
         }
@@ -564,7 +554,7 @@ static int watch_passwords(void) {
         };
 
         int notify = -1, signal_fd = -1, tty_block_fd = -1;
         };
 
         int notify = -1, signal_fd = -1, tty_block_fd = -1;
-        struct pollfd pollfd[_FD_MAX];
+        struct pollfd pollfd[_FD_MAX] = {};
         sigset_t mask;
         int r;
 
         sigset_t mask;
         int r;
 
@@ -592,7 +582,6 @@ static int watch_passwords(void) {
                 goto finish;
         }
 
                 goto finish;
         }
 
-        zero(pollfd);
         pollfd[FD_INOTIFY].fd = notify;
         pollfd[FD_INOTIFY].events = POLLIN;
         pollfd[FD_SIGNAL].fd = signal_fd;
         pollfd[FD_INOTIFY].fd = notify;
         pollfd[FD_INOTIFY].events = POLLIN;
         pollfd[FD_SIGNAL].fd = signal_fd;
@@ -621,14 +610,9 @@ static int watch_passwords(void) {
         r = 0;
 
 finish:
         r = 0;
 
 finish:
-        if (notify >= 0)
-                close_nointr_nofail(notify);
-
-        if (signal_fd >= 0)
-                close_nointr_nofail(signal_fd);
-
-        if (tty_block_fd >= 0)
-                close_nointr_nofail(tty_block_fd);
+        safe_close(notify);
+        safe_close(signal_fd);
+        safe_close(tty_block_fd);
 
         return r;
 }
 
         return r;
 }
@@ -638,6 +622,7 @@ static int help(void) {
         printf("%s [OPTIONS...]\n\n"
                "Process system password requests.\n\n"
                "  -h --help     Show this help\n"
         printf("%s [OPTIONS...]\n\n"
                "Process system password requests.\n\n"
                "  -h --help     Show this help\n"
+               "     --version  Show package version\n"
                "     --list     Show pending password requests\n"
                "     --query    Process pending password requests\n"
                "     --watch    Continuously process password requests\n"
                "     --list     Show pending password requests\n"
                "     --query    Process pending password requests\n"
                "     --watch    Continuously process password requests\n"
@@ -657,18 +642,20 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_WATCH,
                 ARG_WALL,
                 ARG_PLYMOUTH,
                 ARG_WATCH,
                 ARG_WALL,
                 ARG_PLYMOUTH,
-                ARG_CONSOLE
+                ARG_CONSOLE,
+                ARG_VERSION
         };
 
         static const struct option options[] = {
                 { "help",     no_argument, NULL, 'h'          },
         };
 
         static const struct option options[] = {
                 { "help",     no_argument, NULL, 'h'          },
+                { "version",  no_argument, NULL, ARG_VERSION  },
                 { "list",     no_argument, NULL, ARG_LIST     },
                 { "query",    no_argument, NULL, ARG_QUERY    },
                 { "watch",    no_argument, NULL, ARG_WATCH    },
                 { "wall",     no_argument, NULL, ARG_WALL     },
                 { "plymouth", no_argument, NULL, ARG_PLYMOUTH },
                 { "console",  no_argument, NULL, ARG_CONSOLE  },
                 { "list",     no_argument, NULL, ARG_LIST     },
                 { "query",    no_argument, NULL, ARG_QUERY    },
                 { "watch",    no_argument, NULL, ARG_WATCH    },
                 { "wall",     no_argument, NULL, ARG_WALL     },
                 { "plymouth", no_argument, NULL, ARG_PLYMOUTH },
                 { "console",  no_argument, NULL, ARG_CONSOLE  },
-                { NULL,    0,           NULL, 0               }
+                {}
         };
 
         int c;
         };
 
         int c;
@@ -681,7 +668,11 @@ static int parse_argv(int argc, char *argv[]) {
                 switch (c) {
 
                 case 'h':
                 switch (c) {
 
                 case 'h':
-                        help();
+                        return help();
+
+                case ARG_VERSION:
+                        puts(PACKAGE_STRING);
+                        puts(SYSTEMD_FEATURES);
                         return 0;
 
                 case ARG_LIST:
                         return 0;
 
                 case ARG_LIST:
@@ -712,8 +703,7 @@ static int parse_argv(int argc, char *argv[]) {
                         return -EINVAL;
 
                 default:
                         return -EINVAL;
 
                 default:
-                        log_error("Unknown option code %c", c);
-                        return -EINVAL;
+                        assert_not_reached("Unhandled option");
                 }
         }
 
                 }
         }
 
@@ -728,6 +718,7 @@ static int parse_argv(int argc, char *argv[]) {
 int main(int argc, char *argv[]) {
         int r;
 
 int main(int argc, char *argv[]) {
         int r;
 
+        log_set_target(LOG_TARGET_AUTO);
         log_parse_environment();
         log_open();
 
         log_parse_environment();
         log_open();