Modernization

Use _cleanup_ and wrap lines to ~80 chars and such.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2013-03-24 19:45:16 -04:00
parent 3c8bed4ee0
commit e62d8c3944
14 changed files with 115 additions and 116 deletions

View File

@ -161,11 +161,12 @@ int bus_execute_append_cpu_sched_priority(DBusMessageIter *i, const char *proper
n = c->cpu_sched_priority;
else {
struct sched_param p;
n = 0;
zero(p);
if (sched_getparam(0, &p) >= 0)
n = p.sched_priority;
else
n = 0;
}
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n))

View File

@ -1215,8 +1215,12 @@ int exec_spawn(ExecCommand *command,
zero(param);
param.sched_priority = context->cpu_sched_priority;
if (sched_setscheduler(0, context->cpu_sched_policy |
(context->cpu_sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0), &param) < 0) {
r = sched_setscheduler(0,
context->cpu_sched_policy |
(context->cpu_sched_reset_on_fork ?
SCHED_RESET_ON_FORK : 0),
&param);
if (r < 0) {
err = -errno;
r = EXIT_SETSCHEDULER;
goto fail_child;
@ -1437,7 +1441,8 @@ int exec_spawn(ExecCommand *command,
}
}
if (!(our_env = new0(char*, 7))) {
our_env = new0(char*, 7);
if (!our_env) {
err = -ENOMEM;
r = EXIT_MEMORY;
goto fail_child;
@ -1477,20 +1482,21 @@ int exec_spawn(ExecCommand *command,
assert(n_env <= 7);
if (!(final_env = strv_env_merge(
5,
environment,
our_env,
context->environment,
files_env,
pam_env,
NULL))) {
final_env = strv_env_merge(5,
environment,
our_env,
context->environment,
files_env,
pam_env,
NULL);
if (!final_env) {
err = -ENOMEM;
r = EXIT_MEMORY;
goto fail_child;
}
if (!(final_argv = replace_env_argv(argv, final_env))) {
final_argv = replace_env_argv(argv, final_env);
if (!final_argv) {
err = -ENOMEM;
r = EXIT_MEMORY;
goto fail_child;
@ -1519,10 +1525,10 @@ int exec_spawn(ExecCommand *command,
}
log_struct_unit(LOG_DEBUG,
unit_id,
"MESSAGE=Forked %s as %lu",
command->path, (unsigned long) pid,
NULL);
unit_id,
"MESSAGE=Forked %s as %lu",
command->path, (unsigned long) pid,
NULL);
/* We add the new process to the cgroup both in the child (so
* that we can be sure that no user code is ever executed

View File

@ -229,7 +229,8 @@ static int read_response(int fd, unsigned requests_max) {
}
static int check_loopback(void) {
int r, fd;
int r;
int _cleanup_close_ fd;
union {
struct sockaddr sa;
struct sockaddr_in in;
@ -251,8 +252,6 @@ static int check_loopback(void) {
else
r = errno == EADDRNOTAVAIL ? 0 : -errno;
close_nointr_nofail(fd);
return r;
}
@ -263,7 +262,7 @@ int loopback_setup(void) {
struct sockaddr_nl nl;
} sa;
unsigned requests = 0, i;
int fd;
int _cleanup_close_ fd = -1;
bool eperm = false;
errno = 0;
@ -279,16 +278,16 @@ int loopback_setup(void) {
sa.nl.nl_family = AF_NETLINK;
if (bind(fd, &sa.sa, sizeof(sa)) < 0) {
r = -errno;
goto finish;
goto error;
}
r = add_adresses(fd, if_loopback, &requests);
if (r < 0)
goto finish;
goto error;
r = start_interface(fd, if_loopback, &requests);
if (r < 0)
goto finish;
goto error;
for (i = 0; i < requests; i++) {
r = read_response(fd, requests);
@ -296,22 +295,17 @@ int loopback_setup(void) {
if (r == -EPERM)
eperm = true;
else if (r < 0)
goto finish;
goto error;
}
if (eperm && check_loopback() < 0) {
r = -EPERM;
goto finish;
goto error;
}
r = 0;
finish:
if (r < 0)
log_warning("Failed to configure loopback device: %s", strerror(-r));
if (fd >= 0)
close_nointr_nofail(fd);
return 0;
error:
log_warning("Failed to configure loopback device: %s", strerror(-r));
return r;
}

View File

@ -116,8 +116,10 @@ _noreturn_ static void crash(int sig) {
sa.sa_flags = SA_NOCLDSTOP|SA_RESTART;
assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
if ((pid = fork()) < 0)
log_error("Caught <%s>, cannot fork for core dump: %s", signal_to_string(sig), strerror(errno));
pid = fork();
if (pid < 0)
log_error("Caught <%s>, cannot fork for core dump: %s",
signal_to_string(sig), strerror(errno));
else if (pid == 0) {
struct rlimit rl;
@ -147,12 +149,17 @@ _noreturn_ static void crash(int sig) {
int r;
/* Order things nicely. */
if ((r = wait_for_terminate(pid, &status)) < 0)
log_error("Caught <%s>, waitpid() failed: %s", signal_to_string(sig), strerror(-r));
r = wait_for_terminate(pid, &status);
if (r < 0)
log_error("Caught <%s>, waitpid() failed: %s",
signal_to_string(sig), strerror(-r));
else if (status.si_code != CLD_DUMPED)
log_error("Caught <%s>, core dump failed.", signal_to_string(sig));
log_error("Caught <%s>, core dump failed.",
signal_to_string(sig));
else
log_error("Caught <%s>, dumped core as pid %lu.", signal_to_string(sig), (unsigned long) pid);
log_error("Caught <%s>, dumped core as pid %lu.",
signal_to_string(sig),
(unsigned long) pid);
}
}
@ -183,7 +190,8 @@ _noreturn_ static void crash(int sig) {
_exit(1);
}
log_info("Successfully spawned crash shell as pid %lu.", (unsigned long) pid);
log_info("Successfully spawned crash shell as pid %lu.",
(unsigned long) pid);
}
log_info("Freezing execution.");

View File

@ -95,7 +95,7 @@ static int manager_setup_notify(Manager *m) {
struct sockaddr_un un;
} sa;
struct epoll_event ev;
int one = 1;
int one = 1, r;
assert(m);
@ -116,12 +116,15 @@ static int manager_setup_notify(Manager *m) {
sa.un.sun_path[0] = 0;
if (bind(m->notify_watch.fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
r = bind(m->notify_watch.fd, &sa.sa,
offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1));
if (r < 0) {
log_error("bind() failed: %m");
return -errno;
}
if (setsockopt(m->notify_watch.fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) {
r = setsockopt(m->notify_watch.fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
if (r < 0) {
log_error("SO_PASSCRED failed: %m");
return -errno;
}
@ -130,7 +133,8 @@ static int manager_setup_notify(Manager *m) {
ev.events = EPOLLIN;
ev.data.ptr = &m->notify_watch;
if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->notify_watch.fd, &ev) < 0) {
r = epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->notify_watch.fd, &ev);
if (r < 0) {
log_error("Failed to add notification socket fd to epoll: %m");
return -errno;
}
@ -1188,7 +1192,7 @@ static int manager_process_notify_fd(Manager *m) {
uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
} control;
Unit *u;
char **tags;
char _cleanup_strv_free_ **tags = NULL;
zero(iovec);
iovec.iov_base = buf;
@ -1226,7 +1230,8 @@ static int manager_process_notify_fd(Manager *m) {
if (!u) {
u = cgroup_unit_by_pid(m, ucred->pid);
if (!u) {
log_warning("Cannot find unit for notify message of PID %lu.", (unsigned long) ucred->pid);
log_warning("Cannot find unit for notify message of PID %lu.",
(unsigned long) ucred->pid);
continue;
}
}
@ -1241,8 +1246,6 @@ static int manager_process_notify_fd(Manager *m) {
if (UNIT_VTABLE(u)->notify_message)
UNIT_VTABLE(u)->notify_message(u, ucred->pid, tags);
strv_free(tags);
}
return 0;
@ -1898,7 +1901,8 @@ void manager_send_unit_plymouth(Manager *m, Unit *u) {
/* We set SOCK_NONBLOCK here so that we rather drop the
* message then wait for plymouth */
if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
if (fd < 0) {
log_error("socket() failed: %m");
return;
}

View File

@ -1710,7 +1710,8 @@ static int mount_enumerate(Manager *m) {
assert(m);
if (!m->proc_self_mountinfo) {
if (!(m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re")))
m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
if (!m->proc_self_mountinfo)
return -errno;
m->mount_watch.type = WATCH_MOUNT;
@ -1724,7 +1725,8 @@ static int mount_enumerate(Manager *m) {
return -errno;
}
if ((r = mount_load_proc_self_mountinfo(m, false)) < 0)
r = mount_load_proc_self_mountinfo(m, false);
if (r < 0)
goto fail;
return 0;

View File

@ -379,12 +379,14 @@ static int delete_loopback(const char *device) {
}
static int delete_dm(dev_t devnum) {
int fd, r;
int _cleanup_close_ fd = -1;
int r;
struct dm_ioctl dm;
assert(major(devnum) != 0);
if ((fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC)) < 0)
fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC);
if (fd < 0)
return -errno;
zero(dm);
@ -396,8 +398,6 @@ static int delete_dm(dev_t devnum) {
dm.dev = devnum;
r = ioctl(fd, DM_DEV_REMOVE, &dm);
close_nointr_nofail(fd);
return r >= 0 ? 0 : -errno;
}

View File

@ -284,13 +284,16 @@ static int collect(const char *root) {
goto finish;
}
if (!(files = hashmap_new(string_hash_func, string_compare_func))) {
files = hashmap_new(string_hash_func, string_compare_func);
if (!files) {
log_error("Failed to allocate set.");
r = -ENOMEM;
goto finish;
}
if ((fanotify_fd = fanotify_init(FAN_CLOEXEC|FAN_NONBLOCK, O_RDONLY|O_LARGEFILE|O_CLOEXEC|O_NOATIME)) < 0) {
fanotify_fd = fanotify_init(FAN_CLOEXEC|FAN_NONBLOCK,
O_RDONLY|O_LARGEFILE|O_CLOEXEC|O_NOATIME);
if (fanotify_fd < 0) {
log_error("Failed to create fanotify object: %m");
r = -errno;
goto finish;
@ -302,7 +305,8 @@ static int collect(const char *root) {
goto finish;
}
if ((inotify_fd = open_inotify()) < 0) {
inotify_fd = open_inotify();
if (inotify_fd < 0) {
r = inotify_fd;
goto finish;
}

View File

@ -255,14 +255,16 @@ static int create_socket(char **name) {
assert(name);
if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
if (fd < 0) {
log_error("socket() failed: %m");
return -errno;
}
zero(sa);
sa.un.sun_family = AF_UNIX;
snprintf(sa.un.sun_path, sizeof(sa.un.sun_path)-1, "/run/systemd/ask-password/sck.%llu", random_ull());
snprintf(sa.un.sun_path, sizeof(sa.un.sun_path)-1,
"/run/systemd/ask-password/sck.%llu", random_ull());
u = umask(0177);
r = bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path));
@ -280,7 +282,8 @@ static int create_socket(char **name) {
goto fail;
}
if (!(c = strdup(sa.un.sun_path))) {
c = strdup(sa.un.sun_path);
if (!c) {
r = log_oom();
goto fail;
}

View File

@ -44,7 +44,7 @@ typedef struct EpollData {
} EpollData;
static dbus_bool_t add_watch(DBusWatch *watch, void *data) {
EpollData *e;
EpollData _cleanup_free_ *e = NULL;
struct epoll_event ev;
assert(watch);
@ -63,10 +63,8 @@ static dbus_bool_t add_watch(DBusWatch *watch, void *data) {
if (epoll_ctl(PTR_TO_INT(data), EPOLL_CTL_ADD, e->fd, &ev) < 0) {
if (errno != EEXIST) {
free(e);
if (errno != EEXIST)
return FALSE;
}
/* Hmm, bloody D-Bus creates multiple watches on the
* same fd. epoll() does not like that. As a dirty
@ -74,14 +72,11 @@ static dbus_bool_t add_watch(DBusWatch *watch, void *data) {
* one we can safely add to the epoll(). */
e->fd = dup(e->fd);
if (e->fd < 0) {
free(e);
if (e->fd < 0)
return FALSE;
}
if (epoll_ctl(PTR_TO_INT(data), EPOLL_CTL_ADD, e->fd, &ev) < 0) {
close_nointr_nofail(e->fd);
free(e);
return FALSE;
}
@ -89,12 +84,13 @@ static dbus_bool_t add_watch(DBusWatch *watch, void *data) {
}
dbus_watch_set_data(watch, e, NULL);
e = NULL; /* prevent freeing */
return TRUE;
}
static void remove_watch(DBusWatch *watch, void *data) {
EpollData *e;
EpollData _cleanup_free_ *e = NULL;
assert(watch);
@ -106,8 +102,6 @@ static void remove_watch(DBusWatch *watch, void *data) {
if (e->fd_is_dupped)
close_nointr_nofail(e->fd);
free(e);
}
static void toggle_watch(DBusWatch *watch, void *data) {
@ -186,7 +180,7 @@ fail:
}
static void remove_timeout(DBusTimeout *timeout, void *data) {
EpollData *e;
EpollData _cleanup_free_ *e = NULL;
assert(timeout);
@ -196,7 +190,6 @@ static void remove_timeout(DBusTimeout *timeout, void *data) {
assert_se(epoll_ctl(PTR_TO_INT(data), EPOLL_CTL_DEL, e->fd, NULL) >= 0);
close_nointr_nofail(e->fd);
free(e);
}
static void toggle_timeout(DBusTimeout *timeout, void *data) {

View File

@ -1873,18 +1873,18 @@ int flush_fd(int fd) {
ssize_t l;
int r;
if ((r = poll(&pollfd, 1, 0)) < 0) {
r = poll(&pollfd, 1, 0);
if (r < 0) {
if (errno == EINTR)
continue;
return -errno;
}
if (r == 0)
} else if (r == 0)
return 0;
if ((l = read(fd, buf, sizeof(buf))) < 0) {
l = read(fd, buf, sizeof(buf));
if (l < 0) {
if (errno == EINTR)
continue;
@ -1893,9 +1893,7 @@ int flush_fd(int fd) {
return 0;
return -errno;
}
if (l <= 0)
} else if (l == 0)
return 0;
}
}
@ -2068,10 +2066,12 @@ fail:
}
int release_terminal(void) {
int r = 0, fd;
int r = 0;
struct sigaction sa_old, sa_new;
int _cleanup_close_ fd;
if ((fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY|O_CLOEXEC)) < 0)
fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY|O_CLOEXEC);
if (fd < 0)
return -errno;
/* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
@ -2087,7 +2087,6 @@ int release_terminal(void) {
assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0);
close_nointr_nofail(fd);
return r;
}

View File

@ -292,7 +292,7 @@ int utmp_put_runlevel(int runlevel, int previous) {
#define TIMEOUT_MSEC 50
static int write_to_terminal(const char *tty, const char *message) {
int fd, r;
int _cleanup_close_ fd = -1;
const char *p;
size_t left;
usec_t end;
@ -300,14 +300,10 @@ static int write_to_terminal(const char *tty, const char *message) {
assert(tty);
assert(message);
if ((fd = open(tty, O_WRONLY|O_NDELAY|O_NOCTTY|O_CLOEXEC)) < 0)
fd = open(tty, O_WRONLY|O_NDELAY|O_NOCTTY|O_CLOEXEC);
if (fd < 0 || !isatty(fd))
return -errno;
if (!isatty(fd)) {
r = -errno;
goto finish;
}
p = message;
left = strlen(message);
@ -321,30 +317,26 @@ static int write_to_terminal(const char *tty, const char *message) {
t = now(CLOCK_MONOTONIC);
if (t >= end) {
r = -ETIME;
goto finish;
}
if (t >= end)
return -ETIME;
zero(pollfd);
pollfd.fd = fd;
pollfd.events = POLLOUT;
if ((k = poll(&pollfd, 1, (end - t) / USEC_PER_MSEC)) < 0)
k = poll(&pollfd, 1, (end - t) / USEC_PER_MSEC);
if (k < 0)
return -errno;
if (k <= 0) {
r = -ETIME;
goto finish;
}
if ((n = write(fd, p, left)) < 0) {
if (k == 0)
return -ETIME;
n = write(fd, p, left);
if (n < 0) {
if (errno == EAGAIN)
continue;
r = -errno;
goto finish;
return -errno;
}
assert((size_t) n <= left);
@ -353,12 +345,7 @@ static int write_to_terminal(const char *tty, const char *message) {
left -= n;
}
r = 0;
finish:
close_nointr_nofail(fd);
return r;
return 0;
}
int utmp_wall(const char *message, bool (*match_tty)(const char *tty)) {

View File

@ -2980,7 +2980,7 @@ static int print_property(const char *name, DBusMessageIter *iter) {
}
static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
_cleanup_free_ DBusMessage *reply = NULL;
DBusMessage _cleanup_free_ *reply = NULL;
const char *interface = "";
int r;
DBusMessageIter iter, sub, sub2, sub3;

View File

@ -191,8 +191,8 @@ static void font_copy_to_all_vcs(int fd) {
for (i = 1; i <= 15; i++) {
char vcname[16];
int vcfd;
struct console_font_op cfo;
int _cleanup_close_ vcfd = -1;
if (i == vcs.v_active)
continue;
@ -212,8 +212,6 @@ static void font_copy_to_all_vcs(int fd) {
cfo.op = KD_FONT_OP_COPY;
cfo.height = vcs.v_active-1; /* tty1 == index 0 */
ioctl(vcfd, KDFONTOP, &cfo);
close_nointr_nofail(vcfd);
}
}