]> TLD Linux GIT Repositories - rc-scripts.git/blob - src/netreport.c
- from PLD
[rc-scripts.git] / src / netreport.c
1 /*
2  * Copyright (c) 1997-2002 Red Hat, Inc. All rights reserved.
3  *
4  * This software may be freely redistributed under the terms of the GNU
5  * public license.
6  *
7  * You should have received a copy of the GNU General Public License
8  * along with this program; if not, write to the Free Software
9  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
10  *
11  */
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18
19 /* this will be running setgid root, so be careful! */
20
21 static void
22 usage(void) {
23     fprintf(stderr, "usage: netreport [-r]\n");
24     exit(1);
25 }
26
27 #define ADD 1
28 #define DEL 0
29 int main(int argc, char ** argv) {
30     int action = ADD;
31     /* more than long enough for "/var/run/netreport/<pid>\0" */
32     char netreport_name[64];
33     int  netreport_file;
34
35     if (argc > 2) {
36         usage();
37     }
38
39     if (argc > 1) {
40           if (argc == 2 && strcmp(argv[1], "-r") == 0) {
41                   action = DEL;
42           } else {
43                   usage();
44           }
45     }
46
47     snprintf(netreport_name, sizeof(netreport_name),
48              "/var/run/netreport/%d", getppid());
49     if (action == ADD) {
50         netreport_file = open(netreport_name,
51                               O_EXCL|O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW, 0);
52         if (netreport_file == -1) {
53             if (errno != EEXIST) {
54                 perror("Could not create netreport file");
55                 exit (1);
56             }
57         } else {
58             close(netreport_file);
59         }
60     } else {
61         /* ignore errors; not much we can do, won't hurt anything */
62         unlink(netreport_name);
63     }
64
65     return 0;
66 }