diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2006-11-18 22:03:26 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2006-11-18 22:03:26 +0000 |
commit | 61126ab30a90b74e45a79ccb97074ab71afa6054 (patch) | |
tree | cbc4d021bafef2561cbb37ea0d0c955bfd985401 | |
parent | 5a3395bc01cd4b11309595a6ecdaf32f8279f378 (diff) | |
download | busybox-w32-61126ab30a90b74e45a79ccb97074ab71afa6054.tar.gz busybox-w32-61126ab30a90b74e45a79ccb97074ab71afa6054.tar.bz2 busybox-w32-61126ab30a90b74e45a79ccb97074ab71afa6054.zip |
small fixes: using fd-based io instead of FILE*-based,
missed O_TRUNC, etc
-rw-r--r-- | debianutils/start_stop_daemon.c | 28 | ||||
-rw-r--r-- | networking/udhcp/common.c | 4 | ||||
-rw-r--r-- | networking/udhcp/dumpleases.c | 31 | ||||
-rw-r--r-- | networking/udhcp/files.c | 54 | ||||
-rw-r--r-- | networking/udhcp/pidfile.c | 8 |
5 files changed, 63 insertions, 62 deletions
diff --git a/debianutils/start_stop_daemon.c b/debianutils/start_stop_daemon.c index a9f82c5cc..6d3877a7c 100644 --- a/debianutils/start_stop_daemon.c +++ b/debianutils/start_stop_daemon.c | |||
@@ -40,7 +40,8 @@ static inline void push(pid_t pid) | |||
40 | 40 | ||
41 | static int pid_is_exec(pid_t pid, const char *name) | 41 | static int pid_is_exec(pid_t pid, const char *name) |
42 | { | 42 | { |
43 | char buf[32], *execbuf; | 43 | char buf[sizeof("/proc//exe") + sizeof(int)*3]; |
44 | char *execbuf; | ||
44 | int equal; | 45 | int equal; |
45 | 46 | ||
46 | sprintf(buf, "/proc/%d/exe", pid); | 47 | sprintf(buf, "/proc/%d/exe", pid); |
@@ -56,7 +57,7 @@ static int pid_is_exec(pid_t pid, const char *name) | |||
56 | static int pid_is_user(int pid, int uid) | 57 | static int pid_is_user(int pid, int uid) |
57 | { | 58 | { |
58 | struct stat sb; | 59 | struct stat sb; |
59 | char buf[32]; | 60 | char buf[sizeof("/proc/") + sizeof(int)*3]; |
60 | 61 | ||
61 | sprintf(buf, "/proc/%d", pid); | 62 | sprintf(buf, "/proc/%d", pid); |
62 | if (stat(buf, &sb) != 0) | 63 | if (stat(buf, &sb) != 0) |
@@ -66,7 +67,7 @@ static int pid_is_user(int pid, int uid) | |||
66 | 67 | ||
67 | static int pid_is_cmd(pid_t pid, const char *name) | 68 | static int pid_is_cmd(pid_t pid, const char *name) |
68 | { | 69 | { |
69 | char buf[32]; | 70 | char buf[sizeof("/proc//stat") + sizeof(int)*3]; |
70 | FILE *f; | 71 | FILE *f; |
71 | int c; | 72 | int c; |
72 | 73 | ||
@@ -115,7 +116,6 @@ static void do_pidfile(void) | |||
115 | fclose(f); | 116 | fclose(f); |
116 | } else if (errno != ENOENT) | 117 | } else if (errno != ENOENT) |
117 | bb_perror_msg_and_die("open pidfile %s", pidfile); | 118 | bb_perror_msg_and_die("open pidfile %s", pidfile); |
118 | |||
119 | } | 119 | } |
120 | 120 | ||
121 | static void do_procinit(void) | 121 | static void do_procinit(void) |
@@ -146,28 +146,28 @@ static void do_procinit(void) | |||
146 | 146 | ||
147 | static int do_stop(void) | 147 | static int do_stop(void) |
148 | { | 148 | { |
149 | RESERVE_CONFIG_BUFFER(what, 1024); | 149 | char *what; |
150 | struct pid_list *p; | 150 | struct pid_list *p; |
151 | int killed = 0; | 151 | int killed = 0; |
152 | 152 | ||
153 | do_procinit(); | 153 | do_procinit(); |
154 | 154 | ||
155 | if (cmdname) | 155 | if (cmdname) |
156 | strcpy(what, cmdname); | 156 | what = xstrdup(cmdname); |
157 | else if (execname) | 157 | else if (execname) |
158 | strcpy(what, execname); | 158 | what = xstrdup(execname); |
159 | else if (pidfile) | 159 | else if (pidfile) |
160 | sprintf(what, "process in pidfile `%.200s'", pidfile); | 160 | what = xasprintf("process in pidfile '%s'", pidfile); |
161 | else if (userspec) | 161 | else if (userspec) |
162 | sprintf(what, "process(es) owned by `%s'", userspec); | 162 | what = xasprintf("process(es) owned by '%s'", userspec); |
163 | else | 163 | else |
164 | bb_error_msg_and_die ("internal error, please report"); | 164 | bb_error_msg_and_die("internal error, please report"); |
165 | 165 | ||
166 | if (!found) { | 166 | if (!found) { |
167 | if (!quiet) | 167 | if (!quiet) |
168 | printf("no %s found; none killed.\n", what); | 168 | printf("no %s found; none killed\n", what); |
169 | if (ENABLE_FEATURE_CLEAN_UP) | 169 | if (ENABLE_FEATURE_CLEAN_UP) |
170 | RELEASE_CONFIG_BUFFER(what); | 170 | free(what); |
171 | return -1; | 171 | return -1; |
172 | } | 172 | } |
173 | for (p = found; p; p = p->next) { | 173 | for (p = found; p; p = p->next) { |
@@ -183,10 +183,10 @@ static int do_stop(void) | |||
183 | for (p = found; p; p = p->next) | 183 | for (p = found; p; p = p->next) |
184 | if(p->pid < 0) | 184 | if(p->pid < 0) |
185 | printf(" %d", -p->pid); | 185 | printf(" %d", -p->pid); |
186 | printf(").\n"); | 186 | puts(")"); |
187 | } | 187 | } |
188 | if (ENABLE_FEATURE_CLEAN_UP) | 188 | if (ENABLE_FEATURE_CLEAN_UP) |
189 | RELEASE_CONFIG_BUFFER(what); | 189 | free(what); |
190 | return killed; | 190 | return killed; |
191 | } | 191 | } |
192 | 192 | ||
diff --git a/networking/udhcp/common.c b/networking/udhcp/common.c index 4c18e5d51..3e916f422 100644 --- a/networking/udhcp/common.c +++ b/networking/udhcp/common.c | |||
@@ -29,9 +29,7 @@ long uptime(void) | |||
29 | */ | 29 | */ |
30 | static inline void sanitize_fds(void) | 30 | static inline void sanitize_fds(void) |
31 | { | 31 | { |
32 | int fd = open(bb_dev_null, O_RDWR, 0); | 32 | int fd = xopen(bb_dev_null, O_RDWR); |
33 | if (fd < 0) | ||
34 | return; | ||
35 | while (fd < 3) | 33 | while (fd < 3) |
36 | fd = dup(fd); | 34 | fd = dup(fd); |
37 | close(fd); | 35 | close(fd); |
diff --git a/networking/udhcp/dumpleases.c b/networking/udhcp/dumpleases.c index 4422d3099..a0e81bb13 100644 --- a/networking/udhcp/dumpleases.c +++ b/networking/udhcp/dumpleases.c | |||
@@ -13,9 +13,9 @@ | |||
13 | 13 | ||
14 | int dumpleases_main(int argc, char *argv[]) | 14 | int dumpleases_main(int argc, char *argv[]) |
15 | { | 15 | { |
16 | FILE *fp; | 16 | int fp; |
17 | int i, c, mode = REMAINING; | 17 | int i, c, mode = REMAINING; |
18 | long expires; | 18 | unsigned long expires; |
19 | const char *file = LEASES_FILE; | 19 | const char *file = LEASES_FILE; |
20 | struct dhcpOfferedAddr lease; | 20 | struct dhcpOfferedAddr lease; |
21 | struct in_addr addr; | 21 | struct in_addr addr; |
@@ -43,11 +43,11 @@ int dumpleases_main(int argc, char *argv[]) | |||
43 | } | 43 | } |
44 | } | 44 | } |
45 | 45 | ||
46 | fp = xfopen(file, "r"); | 46 | fp = xopen(file, O_RDONLY); |
47 | 47 | ||
48 | printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at"); | 48 | printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at"); |
49 | /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */ | 49 | /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */ |
50 | while (fread(&lease, sizeof(lease), 1, fp)) { | 50 | while (full_read(fp, &lease, sizeof(lease)) == sizeof(lease)) { |
51 | printf(":%02x"+1, lease.chaddr[0]); | 51 | printf(":%02x"+1, lease.chaddr[0]); |
52 | for (i = 1; i < 6; i++) { | 52 | for (i = 1; i < 6; i++) { |
53 | printf(":%02x", lease.chaddr[i]); | 53 | printf(":%02x", lease.chaddr[i]); |
@@ -59,23 +59,16 @@ int dumpleases_main(int argc, char *argv[]) | |||
59 | if (!expires) | 59 | if (!expires) |
60 | printf("expired\n"); | 60 | printf("expired\n"); |
61 | else { | 61 | else { |
62 | if (expires > 60*60*24) { | 62 | unsigned d, h, m; |
63 | printf("%ld days, ", expires / (60*60*24)); | 63 | d = expires / (24*60*60); expires %= (24*60*60); |
64 | expires %= 60*60*24; | 64 | h = expires / (60*60); expires %= (60*60); |
65 | } | 65 | m = expires / 60; expires %= 60; |
66 | if (expires > 60*60) { | 66 | if (d) printf("%u days ", d); |
67 | printf("%ld hours, ", expires / (60*60)); | 67 | printf("%02u:%02u:%02u\n", h, m, (unsigned)expires); |
68 | expires %= 60*60; | ||
69 | } | ||
70 | if (expires > 60) { | ||
71 | printf("%ld minutes, ", expires / 60); | ||
72 | expires %= 60; | ||
73 | } | ||
74 | printf("%ld seconds\n", expires); | ||
75 | } | 68 | } |
76 | } else printf("%s", ctime(&expires)); | 69 | } else fputs(ctime(&expires), stdout); |
77 | } | 70 | } |
78 | fclose(fp); | 71 | /* close(fp); */ |
79 | 72 | ||
80 | return 0; | 73 | return 0; |
81 | } | 74 | } |
diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c index 317e861c0..2c2c5422c 100644 --- a/networking/udhcp/files.c +++ b/networking/udhcp/files.c | |||
@@ -272,27 +272,32 @@ int read_config(const char *file) | |||
272 | if (keywords[i].def[0]) | 272 | if (keywords[i].def[0]) |
273 | keywords[i].handler(keywords[i].def, keywords[i].var); | 273 | keywords[i].handler(keywords[i].def, keywords[i].var); |
274 | 274 | ||
275 | if (!(in = fopen(file, "r"))) { | 275 | in = fopen(file, "r"); |
276 | bb_error_msg("unable to open config file: %s", file); | 276 | if (!in) { |
277 | bb_error_msg("cannot open config file: %s", file); | ||
277 | return 0; | 278 | return 0; |
278 | } | 279 | } |
279 | 280 | ||
280 | while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) { | 281 | while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) { |
281 | char debug_orig[READ_CONFIG_BUF_SIZE]; | 282 | char debug_orig[READ_CONFIG_BUF_SIZE]; |
283 | char *p; | ||
282 | 284 | ||
283 | lm++; | 285 | lm++; |
284 | if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0'; | 286 | p = strchr(buffer, '\n'); |
287 | if (p) *p = '\0'; | ||
285 | if (ENABLE_FEATURE_UDHCP_DEBUG) strcpy(debug_orig, buffer); | 288 | if (ENABLE_FEATURE_UDHCP_DEBUG) strcpy(debug_orig, buffer); |
286 | if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0'; | 289 | p = strchr(buffer, '#'); |
290 | if (p) *p = '\0'; | ||
287 | 291 | ||
288 | if (!(token = strtok(buffer, " \t"))) continue; | 292 | if (!(token = strtok(buffer, " \t"))) continue; |
289 | if (!(line = strtok(NULL, ""))) continue; | 293 | if (!(line = strtok(NULL, ""))) continue; |
290 | 294 | ||
291 | /* eat leading whitespace */ | 295 | /* eat leading whitespace */ |
292 | line = line + strspn(line, " \t="); | 296 | line = skip_whitespace(line); |
293 | /* eat trailing whitespace */ | 297 | /* eat trailing whitespace */ |
294 | for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--); | 298 | i = strlen(line) - 1; |
295 | line[i] = '\0'; | 299 | while (i >= 0 && isspace(line[i])) |
300 | line[i--] = '\0'; | ||
296 | 301 | ||
297 | for (i = 0; keywords[i].keyword[0]; i++) | 302 | for (i = 0; keywords[i].keyword[0]; i++) |
298 | if (!strcasecmp(token, keywords[i].keyword)) | 303 | if (!strcasecmp(token, keywords[i].keyword)) |
@@ -311,14 +316,14 @@ int read_config(const char *file) | |||
311 | 316 | ||
312 | void write_leases(void) | 317 | void write_leases(void) |
313 | { | 318 | { |
314 | FILE *fp; | 319 | int fp; |
315 | unsigned int i; | 320 | unsigned i; |
316 | char buf[255]; | ||
317 | time_t curr = time(0); | 321 | time_t curr = time(0); |
318 | unsigned long tmp_time; | 322 | unsigned long tmp_time; |
319 | 323 | ||
320 | if (!(fp = fopen(server_config.lease_file, "w"))) { | 324 | fp = open(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
321 | bb_error_msg("unable to open %s for writing", server_config.lease_file); | 325 | if (fp < 0) { |
326 | bb_error_msg("cannot open %s for writing", server_config.lease_file); | ||
322 | return; | 327 | return; |
323 | } | 328 | } |
324 | 329 | ||
@@ -334,33 +339,38 @@ void write_leases(void) | |||
334 | else leases[i].expires -= curr; | 339 | else leases[i].expires -= curr; |
335 | } /* else stick with the time we got */ | 340 | } /* else stick with the time we got */ |
336 | leases[i].expires = htonl(leases[i].expires); | 341 | leases[i].expires = htonl(leases[i].expires); |
337 | fwrite(&leases[i], sizeof(struct dhcpOfferedAddr), 1, fp); | 342 | // FIXME: error check?? |
343 | full_write(fp, &leases[i], sizeof(leases[i])); | ||
338 | 344 | ||
339 | /* Then restore it when done. */ | 345 | /* then restore it when done */ |
340 | leases[i].expires = tmp_time; | 346 | leases[i].expires = tmp_time; |
341 | } | 347 | } |
342 | } | 348 | } |
343 | fclose(fp); | 349 | close(fp); |
344 | 350 | ||
345 | if (server_config.notify_file) { | 351 | if (server_config.notify_file) { |
346 | sprintf(buf, "%s %s", server_config.notify_file, server_config.lease_file); | 352 | char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file); |
347 | system(buf); | 353 | system(cmd); |
354 | free(cmd); | ||
348 | } | 355 | } |
349 | } | 356 | } |
350 | 357 | ||
351 | 358 | ||
352 | void read_leases(const char *file) | 359 | void read_leases(const char *file) |
353 | { | 360 | { |
354 | FILE *fp; | 361 | int fp; |
355 | unsigned int i = 0; | 362 | unsigned int i = 0; |
356 | struct dhcpOfferedAddr lease; | 363 | struct dhcpOfferedAddr lease; |
357 | 364 | ||
358 | if (!(fp = fopen(file, "r"))) { | 365 | fp = open(file, O_RDONLY); |
359 | bb_error_msg("unable to open %s for reading", file); | 366 | if (fp < 0) { |
367 | bb_error_msg("cannot open %s for reading", file); | ||
360 | return; | 368 | return; |
361 | } | 369 | } |
362 | 370 | ||
363 | while (i < server_config.max_leases && (fread(&lease, sizeof lease, 1, fp) == 1)) { | 371 | while (i < server_config.max_leases |
372 | && full_read(fp, &lease, sizeof(lease)) == sizeof(lease) | ||
373 | ) { | ||
364 | /* ADDME: is it a static lease */ | 374 | /* ADDME: is it a static lease */ |
365 | if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) { | 375 | if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) { |
366 | lease.expires = ntohl(lease.expires); | 376 | lease.expires = ntohl(lease.expires); |
@@ -373,5 +383,5 @@ void read_leases(const char *file) | |||
373 | } | 383 | } |
374 | } | 384 | } |
375 | DEBUG("Read %d leases", i); | 385 | DEBUG("Read %d leases", i); |
376 | fclose(fp); | 386 | close(fp); |
377 | } | 387 | } |
diff --git a/networking/udhcp/pidfile.c b/networking/udhcp/pidfile.c index 8d00490af..bcb2608c5 100644 --- a/networking/udhcp/pidfile.c +++ b/networking/udhcp/pidfile.c | |||
@@ -23,7 +23,7 @@ | |||
23 | #include "common.h" | 23 | #include "common.h" |
24 | 24 | ||
25 | 25 | ||
26 | static char *saved_pidfile; | 26 | static const char *saved_pidfile; |
27 | 27 | ||
28 | static void pidfile_delete(void) | 28 | static void pidfile_delete(void) |
29 | { | 29 | { |
@@ -36,14 +36,14 @@ int pidfile_acquire(const char *pidfile) | |||
36 | int pid_fd; | 36 | int pid_fd; |
37 | if (!pidfile) return -1; | 37 | if (!pidfile) return -1; |
38 | 38 | ||
39 | pid_fd = open(pidfile, O_CREAT | O_WRONLY, 0644); | 39 | pid_fd = open(pidfile, O_CREAT|O_WRONLY|O_TRUNC, 0644); |
40 | if (pid_fd < 0) { | 40 | if (pid_fd < 0) { |
41 | bb_perror_msg("unable to open pidfile %s", pidfile); | 41 | bb_perror_msg("cannot open pidfile %s", pidfile); |
42 | } else { | 42 | } else { |
43 | lockf(pid_fd, F_LOCK, 0); | 43 | lockf(pid_fd, F_LOCK, 0); |
44 | if (!saved_pidfile) | 44 | if (!saved_pidfile) |
45 | atexit(pidfile_delete); | 45 | atexit(pidfile_delete); |
46 | saved_pidfile = (char *) pidfile; | 46 | saved_pidfile = pidfile; |
47 | } | 47 | } |
48 | 48 | ||
49 | return pid_fd; | 49 | return pid_fd; |