Skip to content

Commit

Permalink
properly handle return codes
Browse files Browse the repository at this point in the history
Signed-off-by: John Crispin <blogic@openwrt.org>
  • Loading branch information
John Crispin committed Mar 28, 2015
1 parent 74d8354 commit 91da63d
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 30 deletions.
3 changes: 2 additions & 1 deletion initd/early.c
Expand Up @@ -77,7 +77,8 @@ early_mounts(void)
mkdir("/tmp/run", 0777);
mkdir("/tmp/lock", 0777);
mkdir("/tmp/state", 0777);
symlink("/tmp", "/var");
if (symlink("/tmp", "/var"))
ERROR("failed to symlink /tmp -> /var\n");
}

static void
Expand Down
4 changes: 1 addition & 3 deletions initd/mkdev.c
Expand Up @@ -121,7 +121,5 @@ int mkdev(const char *name, int _mode)
n_patterns = 1;
find_devs(true);
find_devs(false);
chdir("/");

return 0;
return chdir("/");
}
3 changes: 2 additions & 1 deletion initd/preinit.c
Expand Up @@ -38,7 +38,8 @@ check_dbglvl(void)

if (!fp)
return;
fscanf(fp, "%d", &lvl);
if (fscanf(fp, "%d", &lvl) == EOF)
ERROR("failed to read debug level\n");
fclose(fp);
unlink("/tmp/debug_level");

Expand Down
11 changes: 6 additions & 5 deletions inittab.c
Expand Up @@ -70,9 +70,11 @@ static int dev_open(const char *dev)
int fd = -1;

if (dev) {
chdir("/dev");
fd = open( dev, O_RDWR);
chdir("/");
if (chdir("/dev"))
ERROR("failed to change dir to /dev\n");
fd = open(dev, O_RDWR);
if (chdir("/"))
ERROR("failed to change dir to /\n");
}

return fd;
Expand All @@ -83,9 +85,8 @@ static int dev_exist(const char *dev)
int res;

res = dev_open(dev);
if (res != -1) {
if (res != -1)
close(res);
}

return (res != -1);
}
Expand Down
16 changes: 12 additions & 4 deletions jail/jail.c
Expand Up @@ -313,12 +313,16 @@ static int spawn_child(void *arg)
sysfs = 1;
break;
case 'n':
sethostname(optarg, strlen(optarg));
if (sethostname(optarg, strlen(optarg)))
ERROR("failed to sethostname: %s\n", strerror(errno));
break;
}
}

asprintf(&mpoint, "%s/old", path);
if (asprintf(&mpoint, "%s/old", path) < 0) {
ERROR("failed to alloc pivot path: %s\n", strerror(errno));
return -1;
}
mkdir_p(mpoint, 0755);
if (pivot_root(path, mpoint) == -1) {
ERROR("pivot_root failed:%s\n", strerror(errno));
Expand Down Expand Up @@ -370,13 +374,17 @@ static void spawn_namespace(const char *path, int argc, char **argv)
char *dir = get_current_dir_name();

uloop_init();
chdir(path);
if (chdir(path)) {
ERROR("failed to chdir() into the jail\n");
return;
}
namespace_process.pid = clone(spawn_child,
child_stack + STACK_SIZE,
CLONE_NEWUTS | CLONE_NEWPID | CLONE_NEWNS | SIGCHLD, argv);

if (namespace_process.pid != -1) {
chdir(dir);
if (chdir(dir))
ERROR("failed to chdir() out of the jail\n");
free(dir);
uloop_process_add(&namespace_process);
uloop_run();
Expand Down
13 changes: 10 additions & 3 deletions plug/hotplug.c
Expand Up @@ -198,7 +198,10 @@ static void handle_firmware(struct blob_attr *msg, struct blob_attr *data)
ERROR("Failed to open %s\n", loadpath);
exit(-1);
}
write(load, "1", 1);
if (write(load, "1", 1) == -1) {
ERROR("Failed to write to %s\n", loadpath);
exit(-1);
}
close(load);

snprintf(syspath, sizeof(syspath), "/sys/%s/data", dev);
Expand All @@ -214,15 +217,19 @@ static void handle_firmware(struct blob_attr *msg, struct blob_attr *data)
if (len <= 0)
break;

write(fw, buf, len);
if (write(fw, buf, len) == -1) {
ERROR("failed to write firmware file %s/%s to %s\n", dir, file, dev);
break;
}
}

if (src >= 0)
close(src);
close(fw);

load = open(loadpath, O_WRONLY);
write(load, "0", 1);
if (write(load, "0", 1) == -1)
ERROR("failed to write to %s\n", loadpath);
close(load);

DEBUG(2, "Done loading %s\n", path);
Expand Down
6 changes: 4 additions & 2 deletions service/instance.c
Expand Up @@ -283,8 +283,10 @@ instance_run(struct service_instance *in, int _stdout, int _stderr)
}

if (in->uid || in->gid) {
setuid(in->uid);
setgid(in->gid);
if (setuid(in->uid) || setgid(in->gid)) {
ERROR("failed to set uid:%d, gid:%d\n", in->uid, in->gid);
exit(127);
}
}
execvp(argv[0], argv);
exit(127);
Expand Down
22 changes: 14 additions & 8 deletions state.c
Expand Up @@ -43,12 +43,14 @@ static int reboot_event;

static void set_stdio(const char* tty)
{
chdir("/dev");
freopen(tty, "r", stdin);
freopen(tty, "w", stdout);
freopen(tty, "w", stderr);
chdir("/");
fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) | O_NONBLOCK);
if (chdir("/dev") ||
!freopen(tty, "r", stdin) ||
!freopen(tty, "w", stdout) ||
!freopen(tty, "w", stderr) ||
chdir("/"))
ERROR("failed to set stdio\n");
else
fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) | O_NONBLOCK);
}

static void set_console(void)
Expand All @@ -70,7 +72,10 @@ static void set_console(void)
i++;
}

chdir("/dev");
if (chdir("/dev")) {
ERROR("failed to change dir to /dev\n");
return;
}
while (tty!=NULL) {
f = open(tty, O_RDONLY);
if (f >= 0) {
Expand All @@ -81,7 +86,8 @@ static void set_console(void)
tty=try[i];
i++;
}
chdir("/");
if (chdir("/"))
ERROR("failed to change dir to /\n");

if (tty != NULL)
set_stdio(tty);
Expand Down
3 changes: 2 additions & 1 deletion trace/trace.c
Expand Up @@ -214,7 +214,8 @@ int main(int argc, char **argv, char **envp)
uloop_done();

if (!json)
asprintf(&json, "/tmp/%s.%u.json", basename(*argv), child);
if (asprintf(&json, "/tmp/%s.%u.json", basename(*argv), child) < 0)
ERROR("failed to allocate output path: %s\n", strerror(errno));

print_syscalls(policy, json);

Expand Down
7 changes: 5 additions & 2 deletions upgraded/upgraded.c
Expand Up @@ -18,6 +18,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include <libubox/uloop.h>

Expand Down Expand Up @@ -55,12 +56,14 @@ int main(int argc, char **argv)
{
pid_t p = getpid();

chdir("/tmp");

if (p != 1) {
fprintf(stderr, "this tool needs to run as pid 1\n");
return -1;
}
if (chdir("/tmp") == -1) {
fprintf(stderr, "failed to chdir to /tmp: %s\n", strerror(errno));
return -1;
}
if (argc != 2) {
fprintf(stderr, "sysupgrade stage 2 failed, no folder specified\n");
return -1;
Expand Down

0 comments on commit 91da63d

Please sign in to comment.