#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <libintl.h>
-#include <locale.h>
/* this will be running setgid root, so be careful! */
-void usage(void) {
+static void
+usage(void) {
fprintf(stderr, "usage: netreport [-r]\n");
exit(1);
}
char netreport_name[64];
int netreport_file;
- if (argc > 2) usage();
+ if (argc > 2) {
+ usage();
+ }
- if ((argc > 1) && !strcmp(argv[1], "-r")) {
- action = DEL;
+ if (argc > 1) {
+ if (strcmp(argv[1], "-r") == 0) {
+ action = DEL;
+ } else {
+ usage();
+ }
}
- sprintf(netreport_name, "/var/run/netreport/%d", getppid());
+ snprintf(netreport_name, sizeof(netreport_name),
+ "/var/run/netreport/%d", getppid());
if (action == ADD) {
- netreport_file = creat(netreport_name, 0);
- if (netreport_file < 0) {
+ netreport_file = open(netreport_name,
+ O_EXCL|O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW, 0);
+ if (netreport_file == -1) {
if (errno != EEXIST) {
perror("Could not create netreport file");
exit (1);
unlink(netreport_name);
}
- exit(0);
+ return 0;
}