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
n_patterns = 1;
find_devs(true);
find_devs(false);
- chdir("/");
-
- return 0;
+ return chdir("/");
}
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");
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;
int res;
res = dev_open(dev);
- if (res != -1) {
+ if (res != -1)
close(res);
- }
return (res != -1);
}
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));
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();
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);
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(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);
}
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);
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)
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) {
tty=try[i];
i++;
}
- chdir("/");
+ if (chdir("/"))
+ ERROR("failed to change dir to /\n");
if (tty != NULL)
set_stdio(tty);
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);
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <errno.h>
#include <libubox/uloop.h>
{
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;