Skip to content

Commit 02a97e8

Browse files
committed
Fix various problems with unchecked return values from syscalls.
Signed-off-by: Joachim Nilsson <[email protected]>
1 parent def759f commit 02a97e8

File tree

10 files changed

+25
-16
lines changed

10 files changed

+25
-16
lines changed

helpers.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,11 @@ void set_hostname(char **hostname)
733733
if (fp) {
734734
struct stat st;
735735

736-
fstat(fileno(fp), &st);
736+
if (fstat(fileno(fp), &st)) {
737+
fclose(fp);
738+
return;
739+
}
740+
737741
*hostname = realloc(*hostname, st.st_size);
738742
if (!*hostname) {
739743
fclose(fp);

helpers.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
#ifndef makedir
4848
# define makedir(x, p) do { if (mkdir(x, p) && errno != EEXIST) _pe("Failed creating directory %s", x); } while (0)
4949
#endif
50+
#ifndef makefifo
51+
# define makefifo(x, p) do { if (mkfifo(x, p) && errno != EEXIST) _pe("Failed creating FIFO %s", x); } while (0)
52+
#endif
53+
#ifndef erase
54+
# define erase(x) do { if (remove(x) && errno != ENOENT) _pe("Failed removing %s", x); } while (0)
55+
#endif
5056
#ifndef chardev
5157
# define chardev(x,m,maj,min) mknod((x), S_IFCHR|(m), makedev((maj),(min)))
5258
#endif

libite/copyfile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ int main(void)
344344
if (fexist(files[i + 2]))
345345
fprintf(stderr, "OK => %s\n", files[i + 2]);
346346

347-
remove(files[i + 2]);
347+
erase(files[i + 2]);
348348
i += 3;
349349
}
350350

@@ -358,7 +358,7 @@ int main(void)
358358
if (fexist(files[i + 2]))
359359
fprintf(stderr, "OK => %s\n", files[i + 2]);
360360

361-
remove(files[i + 2]);
361+
erase(files[i + 2]);
362362
i += 3;
363363
}
364364

libite/dir.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,13 @@ int simulate_files(int creat)
142142
if (creat)
143143
touch(files[i]);
144144
else
145-
remove(files[i]);
145+
erase(files[i]);
146146
}
147147

148148
if (creat)
149149
symlink("config2.cfg", STARTUP_CONFIG);
150150
else
151-
remove(STARTUP_CONFIG);
151+
erase(STARTUP_CONFIG);
152152

153153
return 0;
154154
}

plugins/bootmisc.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ static void setup(void *UNUSED(arg))
5757
_d("Setting up necessary UTMP files ...");
5858
touch("/var/run/utmp");
5959
chown("/var/run/utmp", 0, getgroup("utmp"));
60-
chmod("/var/run/utmp", 0664);
6160

6261
#ifdef RUNLEVEL
6362
memset(&entry, 0, sizeof(struct utmp));
@@ -71,16 +70,16 @@ static void setup(void *UNUSED(arg))
7170
#ifdef TOUCH_ETC_NETWORK_RUN_IFSTATE
7271
touch("/etc/network/run/ifstate");
7372
#else
74-
remove("/etc/network/run/ifstate");
73+
erase("/etc/network/run/ifstate");
7574
#endif
7675

7776
_d("Setting up misc files ...");
7877
makedir("/var/run/lldpd", 0755); /* Needed by lldpd */
7978
makedir("/var/run/pluto", 0755); /* Needed by Openswan */
8079
makedir("/var/run/quagga", 0755); /* Needed by Quagga */
8180
makedir("/var/log/quagga", 0755); /* Needed by Quagga */
82-
makedir("/var/run/sshd", 01755); /* OpenSSH */
83-
mkfifo("/dev/xconsole", 0640); /* sysklogd */
81+
makedir("/var/run/sshd", 01755); /* OpenSSH */
82+
makefifo("/dev/xconsole", 0640); /* sysklogd */
8483
chown("/dev/xconsole", 0, getgroup("tty"));
8584
}
8685

plugins/dbus.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static void setup(void *UNUSED(arg))
3232
makedir("/var/run/dbus", 0755);
3333
makedir("/var/lock/subsys/messagebus", 0755);
3434
run("dbus-uuidgen --ensure");
35-
remove("/var/run/dbus/pid");
35+
erase("/var/run/dbus/pid");
3636
run_interactive("dbus-daemon --system", "Starting D-Bus");
3737
#endif
3838
}

plugins/initctl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ static void parse(void *UNUSED(arg), int fd, int UNUSED(events))
124124
PLUGIN_INIT(plugin_init)
125125
{
126126
_d("Setting up %s", FINIT_FIFO);
127-
mkfifo(FINIT_FIFO, 0600);
127+
makefifo(FINIT_FIFO, 0600);
128128

129129
fifo_open();
130130
plugin_register(&plugin);

plugins/x11-common.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static void setup(void *UNUSED(arg))
3636
#endif
3737
char line[LINE_SIZE];
3838

39-
makepath("/var/run/console");
39+
makedir("/var/run/console", 01777);
4040
snprintf(line, sizeof(line), "/var/run/console/%s", username);
4141
touch(line);
4242

sig.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static void sigcont_handler(int sig, siginfo_t *info, void *UNUSED(ctx))
117117
_d("Received SIGCONT(%d) from %s (PID %d)",
118118
sig, pid_get_name(info->si_pid, NULL, 0), info->si_pid);
119119
stopped = 0;
120-
remove(SYNC_STOPPED);
120+
erase(SYNC_STOPPED);
121121
}
122122

123123
/*
@@ -152,8 +152,8 @@ void sig_setup(void)
152152
_d("Setup signals");
153153

154154
/* Cleanup any stale finit control files */
155-
remove(SYNC_SHUTDOWN);
156-
remove(SYNC_STOPPED);
155+
erase(SYNC_SHUTDOWN);
156+
erase(SYNC_STOPPED);
157157

158158
/* Standard SysV init calls ctrl-alt-delete handler */
159159
SETSIG(sa, SIGINT, shutdown_handler, 0);

svc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ void svc_runlevel(int newlevel)
211211
if (runlevel == 1)
212212
touch("/etc/nologin"); /* Disable login in single-user mode */
213213
else
214-
remove("/etc/nologin");
214+
erase("/etc/nologin");
215215

216216
if (0 != prevlevel)
217217
tty_runlevel(runlevel);

0 commit comments

Comments
 (0)