rfkill: define main through macro

This commit is contained in:
Yu Watanabe 2018-11-23 05:55:10 +09:00
parent 5f5f0afc8b
commit 7a83c3aee0
1 changed files with 21 additions and 39 deletions

View File

@ -12,6 +12,7 @@
#include "fd-util.h" #include "fd-util.h"
#include "fileio.h" #include "fileio.h"
#include "io-util.h" #include "io-util.h"
#include "main-func.h"
#include "mkdir.h" #include "mkdir.h"
#include "parse-util.h" #include "parse-util.h"
#include "proc-cmdline.h" #include "proc-cmdline.h"
@ -345,57 +346,44 @@ static void context_save_and_clear(Context *c) {
safe_close(c->rfkill_fd); safe_close(c->rfkill_fd);
} }
int main(int argc, char *argv[]) { static int run(int argc, char *argv[]) {
_cleanup_(context_save_and_clear) Context c = { .rfkill_fd = -1 }; _cleanup_(context_save_and_clear) Context c = { .rfkill_fd = -1 };
bool ready = false; bool ready = false;
int r, n; int r, n;
if (argc > 1) { if (argc > 1)
log_error("This program requires no arguments."); return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program requires no arguments.");
return EXIT_FAILURE;
}
log_setup_service(); log_setup_service();
umask(0022); umask(0022);
r = mkdir_p("/var/lib/systemd/rfkill", 0755); r = mkdir_p("/var/lib/systemd/rfkill", 0755);
if (r < 0) { if (r < 0)
log_error_errno(r, "Failed to create rfkill directory: %m"); return log_error_errno(r, "Failed to create rfkill directory: %m");
goto finish;
}
n = sd_listen_fds(false); n = sd_listen_fds(false);
if (n < 0) { if (n < 0)
r = log_error_errno(n, "Failed to determine whether we got any file descriptors passed: %m"); return log_error_errno(n, "Failed to determine whether we got any file descriptors passed: %m");
goto finish; if (n > 1)
} return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Got too many file descriptors.");
if (n > 1) {
log_error("Got too many file descriptors.");
r = -EINVAL;
goto finish;
}
if (n == 0) { if (n == 0) {
c.rfkill_fd = open("/dev/rfkill", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); c.rfkill_fd = open("/dev/rfkill", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
if (c.rfkill_fd < 0) { if (c.rfkill_fd < 0) {
if (errno == ENOENT) { if (errno == ENOENT) {
log_debug_errno(errno, "Missing rfkill subsystem, or no device present, exiting."); log_debug_errno(errno, "Missing rfkill subsystem, or no device present, exiting.");
r = 0; return 0;
goto finish;
} }
r = log_error_errno(errno, "Failed to open /dev/rfkill: %m"); return log_error_errno(errno, "Failed to open /dev/rfkill: %m");
goto finish;
} }
} else { } else {
c.rfkill_fd = SD_LISTEN_FDS_START; c.rfkill_fd = SD_LISTEN_FDS_START;
if (r < 0) {
log_error_errno(r, "Failed to make /dev/rfkill socket non-blocking: %m");
goto finish;
}
r = fd_nonblock(c.rfkill_fd, 1); r = fd_nonblock(c.rfkill_fd, 1);
if (r < 0)
return log_error_errno(r, "Failed to make /dev/rfkill socket non-blocking: %m");
} }
for (;;) { for (;;) {
@ -421,10 +409,8 @@ int main(int argc, char *argv[]) {
r = fd_wait_for_event(c.rfkill_fd, POLLIN, EXIT_USEC); r = fd_wait_for_event(c.rfkill_fd, POLLIN, EXIT_USEC);
if (r == -EINTR) if (r == -EINTR)
continue; continue;
if (r < 0) { if (r < 0)
log_error_errno(r, "Failed to poll() on device: %m"); return log_error_errno(r, "Failed to poll() on device: %m");
goto finish;
}
if (r > 0) if (r > 0)
continue; continue;
@ -435,11 +421,8 @@ int main(int argc, char *argv[]) {
log_error_errno(errno, "Failed to read from /dev/rfkill: %m"); log_error_errno(errno, "Failed to read from /dev/rfkill: %m");
} }
if (l != RFKILL_EVENT_SIZE_V1) { if (l != RFKILL_EVENT_SIZE_V1)
log_error("Read event structure of invalid size."); return log_error_errno(SYNTHETIC_ERRNO(EIO), "Read event structure of invalid size.");
r = -EIO;
goto finish;
}
type = rfkill_type_to_string(event.type); type = rfkill_type_to_string(event.type);
if (!type) { if (!type) {
@ -470,8 +453,7 @@ int main(int argc, char *argv[]) {
} }
} }
r = 0; return 0;
finish:
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
} }
DEFINE_MAIN_FUNCTION(run);