]>
Commit | Line | Data |
---|---|---|
00196ec7 AM |
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 | */ | |
7742e157 AF |
12 | #include <errno.h> |
13 | #include <fcntl.h> | |
14 | #include <stdio.h> | |
de1fc6ce | 15 | #include <stdlib.h> |
7742e157 AF |
16 | #include <string.h> |
17 | #include <unistd.h> | |
18 | ||
19 | /* this will be running setgid root, so be careful! */ | |
20 | ||
de1fc6ce JR |
21 | static void |
22 | usage(void) { | |
7742e157 AF |
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 | ||
de1fc6ce JR |
35 | if (argc > 2) { |
36 | usage(); | |
37 | } | |
7742e157 | 38 | |
de1fc6ce | 39 | if (argc > 1) { |
00196ec7 | 40 | if (argc == 2 && strcmp(argv[1], "-r") == 0) { |
de1fc6ce JR |
41 | action = DEL; |
42 | } else { | |
43 | usage(); | |
44 | } | |
7742e157 AF |
45 | } |
46 | ||
de1fc6ce JR |
47 | snprintf(netreport_name, sizeof(netreport_name), |
48 | "/var/run/netreport/%d", getppid()); | |
7742e157 | 49 | if (action == ADD) { |
de1fc6ce JR |
50 | netreport_file = open(netreport_name, |
51 | O_EXCL|O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW, 0); | |
52 | if (netreport_file == -1) { | |
7742e157 AF |
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 | ||
de1fc6ce | 65 | return 0; |
7742e157 | 66 | } |