| [1108] | 1 | /* |
|---|
| 2 | * Copyright (c) 1997-2002 Red Hat, Inc. All rights reserved. |
|---|
| 3 | * |
|---|
| 4 | * This program is free software; you can redistribute it and/or modify |
|---|
| 5 | * it under the terms of the GNU General Public License, version 2, |
|---|
| 6 | * as published by the Free Software Foundation. |
|---|
| 7 | * |
|---|
| 8 | * This program is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * GNU General Public License for more details. |
|---|
| 12 | * |
|---|
| 13 | * You should have received a copy of the GNU General Public License |
|---|
| 14 | * along with this program; if not, write to the Free Software |
|---|
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|---|
| 16 | * |
|---|
| 17 | */ |
|---|
| 18 | #include <errno.h> |
|---|
| 19 | #include <fcntl.h> |
|---|
| 20 | #include <stdio.h> |
|---|
| 21 | #include <stdlib.h> |
|---|
| 22 | #include <string.h> |
|---|
| 23 | #include <unistd.h> |
|---|
| 24 | |
|---|
| 25 | /* this will be running setgid root, so be careful! */ |
|---|
| 26 | |
|---|
| 27 | static void |
|---|
| 28 | usage(void) { |
|---|
| 29 | fprintf(stderr, "usage: netreport [-r]\n"); |
|---|
| 30 | exit(1); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | #define ADD 1 |
|---|
| 34 | #define DEL 0 |
|---|
| 35 | int main(int argc, char ** argv) { |
|---|
| 36 | int action = ADD; |
|---|
| 37 | /* more than long enough for "/var/run/netreport/<pid>\0" */ |
|---|
| 38 | char netreport_name[64]; |
|---|
| 39 | int netreport_file; |
|---|
| 40 | |
|---|
| 41 | if (argc > 2) { |
|---|
| 42 | usage(); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | if (argc > 1) { |
|---|
| 46 | if (argc == 2 && strcmp(argv[1], "-r") == 0) { |
|---|
| 47 | action = DEL; |
|---|
| 48 | } else { |
|---|
| 49 | usage(); |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | snprintf(netreport_name, sizeof(netreport_name), |
|---|
| 54 | "/var/run/netreport/%d", getppid()); |
|---|
| 55 | if (action == ADD) { |
|---|
| 56 | netreport_file = open(netreport_name, |
|---|
| 57 | O_EXCL|O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW, 0); |
|---|
| 58 | if (netreport_file == -1) { |
|---|
| 59 | if (errno != EEXIST) { |
|---|
| 60 | perror("Could not create netreport file"); |
|---|
| 61 | exit (1); |
|---|
| 62 | } |
|---|
| 63 | } else { |
|---|
| 64 | close(netreport_file); |
|---|
| 65 | } |
|---|
| 66 | } else { |
|---|
| 67 | /* ignore errors; not much we can do, won't hurt anything */ |
|---|
| 68 | unlink(netreport_name); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | return 0; |
|---|
| 72 | } |
|---|