]> git.pld-linux.org Git - projects/rc-scripts.git/blame - src/netreport.c
- merge from TOTALNEW branch (see NEWS for more info)
[projects/rc-scripts.git] / src / netreport.c
CommitLineData
7742e157
AF
1#include <errno.h>
2#include <fcntl.h>
3#include <stdio.h>
de1fc6ce 4#include <stdlib.h>
7742e157
AF
5#include <string.h>
6#include <unistd.h>
7
8/* this will be running setgid root, so be careful! */
9
de1fc6ce
JR
10static void
11usage(void) {
7742e157
AF
12 fprintf(stderr, "usage: netreport [-r]\n");
13 exit(1);
14}
15
16#define ADD 1
17#define DEL 0
18int main(int argc, char ** argv) {
19 int action = ADD;
20 /* more than long enough for "/var/run/netreport/<pid>\0" */
21 char netreport_name[64];
22 int netreport_file;
23
de1fc6ce
JR
24 if (argc > 2) {
25 usage();
26 }
7742e157 27
de1fc6ce
JR
28 if (argc > 1) {
29 if (strcmp(argv[1], "-r") == 0) {
30 action = DEL;
31 } else {
32 usage();
33 }
7742e157
AF
34 }
35
de1fc6ce
JR
36 snprintf(netreport_name, sizeof(netreport_name),
37 "/var/run/netreport/%d", getppid());
7742e157 38 if (action == ADD) {
de1fc6ce
JR
39 netreport_file = open(netreport_name,
40 O_EXCL|O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW, 0);
41 if (netreport_file == -1) {
7742e157
AF
42 if (errno != EEXIST) {
43 perror("Could not create netreport file");
44 exit (1);
45 }
46 } else {
47 close(netreport_file);
48 }
49 } else {
50 /* ignore errors; not much we can do, won't hurt anything */
51 unlink(netreport_name);
52 }
53
de1fc6ce 54 return 0;
7742e157 55}
This page took 0.087755 seconds and 4 git commands to generate.