chiark / gitweb /
login: fix pos-array allocation
[elogind.git] / src / login / inhibit.c
index 208a0ce..48c2ec4 100644 (file)
@@ -27,7 +27,6 @@
 #include <fcntl.h>
 
 #include "sd-bus.h"
-
 #include "bus-util.h"
 #include "bus-error.h"
 #include "util.h"
@@ -63,7 +62,7 @@ static int inhibit(sd_bus *bus, sd_bus_error *error) {
 
         r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
         if (r < 0)
-                return -EIO;
+                return r;
 
         r = dup(fd);
         if (r < 0)
@@ -93,12 +92,11 @@ static int print_inhibitors(sd_bus *bus, sd_bus_error *error) {
 
         r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
         if (r < 0)
-                return -EIO;
+                return bus_log_parse_error(r);
 
         while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
                 _cleanup_free_ char *comm = NULL, *u = NULL;
 
-
                 get_process_comm(pid, &comm);
                 u = uid_to_name(uid);
 
@@ -114,11 +112,11 @@ static int print_inhibitors(sd_bus *bus, sd_bus_error *error) {
                 n++;
         }
         if (r < 0)
-                return -EIO;
+                return bus_log_parse_error(r);
 
         r = sd_bus_message_exit_container(reply);
         if (r < 0)
-                return -EIO;
+                return bus_log_parse_error(r);
 
         printf("%u inhibitors listed.\n", n);
         return 0;
@@ -162,7 +160,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "why",          required_argument, NULL, ARG_WHY          },
                 { "mode",         required_argument, NULL, ARG_MODE         },
                 { "list",         no_argument,       NULL, ARG_LIST         },
-                { NULL,           0,                 NULL, 0                }
+                {}
         };
 
         int c;
@@ -175,8 +173,7 @@ static int parse_argv(int argc, char *argv[]) {
                 switch (c) {
 
                 case 'h':
-                        help();
-                        return 0;
+                        return help();
 
                 case ARG_VERSION:
                         puts(PACKAGE_STRING);
@@ -203,9 +200,11 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_action = ACTION_LIST;
                         break;
 
-                default:
-                        log_error("Unknown option code %c", c);
+                case '?':
                         return -EINVAL;
+
+                default:
+                        assert_not_reached("Unhandled option");
                 }
         }
 
@@ -221,19 +220,20 @@ static int parse_argv(int argc, char *argv[]) {
 }
 
 int main(int argc, char *argv[]) {
-        int r;
-        _cleanup_bus_unref_ sd_bus *bus = NULL;
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
-        int fd = -1;
+        _cleanup_bus_unref_ sd_bus *bus = NULL;
+        int r;
 
         log_parse_environment();
         log_open();
 
         r = parse_argv(argc, argv);
-        if (r <= 0)
+        if (r < 0)
                 return EXIT_FAILURE;
+        if (r == 0)
+                return EXIT_SUCCESS;
 
-        r = sd_bus_open_system(&bus);
+        r = sd_bus_default_system(&bus);
         if (r < 0) {
                 log_error("Failed to connect to bus: %s", strerror(-r));
                 return EXIT_FAILURE;
@@ -248,15 +248,14 @@ int main(int argc, char *argv[]) {
                 }
 
         } else {
-                char *w = NULL;
+                _cleanup_close_ int fd = -1;
+                _cleanup_free_ char *w = NULL;
                 pid_t pid;
 
                 if (!arg_who)
                         arg_who = w = strv_join(argv + optind, " ");
 
                 fd = inhibit(bus, &error);
-                free(w);
-
                 if (fd < 0) {
                         log_error("Failed to inhibit: %s", bus_error_message(&error, -r));
                         return EXIT_FAILURE;
@@ -279,11 +278,7 @@ int main(int argc, char *argv[]) {
                 }
 
                 r = wait_for_terminate_and_warn(argv[optind], pid);
-                close(fd);
-                if (r < 0)
-                        return EXIT_FAILURE;
-                else
-                        return r;
+                return r < 0 ? EXIT_FAILURE : r;
         }
 
         return 0;