]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/misc-progs/ipfirereboot.c
Readd udev dont rename netif if same name exsit patch.
[people/pmueller/ipfire-2.x.git] / src / misc-progs / ipfirereboot.c
CommitLineData
cd1a2927
MT
1/*\r
2 * This file is part of the IPCop Firewall.\r
3 *\r
4 * IPCop is free software; you can redistribute it and/or modify\r
5 * it under the terms of the GNU General Public License as published by\r
6 * the Free Software Foundation; either version 2 of the License, or\r
7 * (at your option) any later version.\r
8 *\r
9 * IPCop is distributed in the hope that it will be useful,\r
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
12 * GNU General Public License for more details.\r
13 *\r
14 * You should have received a copy of the GNU General Public License\r
15 * along with IPCop; if not, write to the Free Software\r
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
17 *\r
18 * Copyright (C) 2005-10-25 Franck Bourdonnec\r
19 *\r
20 * $Id: ipcopreboot.c,v 1.1.2.2 2005/10/24 23:05:50 franck78 Exp $\r
21 *\r
22 */\r
23\r
24#include <stdio.h>\r
25#include <string.h>\r
26#include <stdlib.h>\r
27#include "setuid.h"\r
28\r
29\r
30/* define operations */\r
31#define OP_REBOOT "boot"\r
32#define OP_REBOOT_FS "bootfs" // add filesystem check option (not yet in GUI)\r
33#define OP_SHUTDOWN "down"\r
34#define OP_SCHEDULE_ADD "cron+"\r
35#define OP_SCHEDULE_REM "cron-"\r
36#define OP_SCHEDULE_GET "cron?"\r
37\r
38int main(int argc, char**argv)\r
39{\r
40\r
41 if (!(initsetuid()))\r
42 return 1;\r
43\r
44 // Check what command is asked\r
45 if (argc==1)\r
46 { \r
47 fprintf (stderr, "Missing reboot command!\n");\r
48 return 1;\r
49 }\r
50\r
51 if (argc==2 && strcmp(argv[1], OP_SHUTDOWN)==0)\r
52 {\r
53 safe_system("/sbin/shutdown -h now");\r
54 return 0;\r
55 }\r
56\r
57 if (argc==2 && strcmp(argv[1], OP_REBOOT)==0)\r
58 {\r
59 safe_system("/sbin/shutdown -r now");\r
60 return 0;\r
61 }\r
62\r
63 if (argc==2 && strcmp(argv[1], OP_REBOOT_FS)==0)\r
64 {\r
65 safe_system("/sbin/shutdown -F -r now");\r
66 return 0;\r
67 }\r
68\r
69 // output schedule to stdout\r
70 if (argc==2 && strcmp(argv[1], OP_SCHEDULE_GET)==0)\r
71 {\r
72 safe_system("/bin/grep /sbin/shutdown /var/spool/cron/root.orig");\r
73 return 0;\r
74 }\r
75\r
76 if (argc==2 && strcmp(argv[1], OP_SCHEDULE_REM)==0)\r
77 {\r
78 safe_system("/usr/bin/perl -i -p -e 's/^.*\\/sbin\\/shutdown.*$//s' /var/spool/cron/root.orig");\r
79 safe_system("/usr/bin/fcrontab -u root -z");\r
80 return 0;\r
81 }\r
82\r
83 if (argc==6 && strcmp(argv[1], OP_SCHEDULE_ADD)==0)\r
84 {\r
85 // check args\r
86 if (!( strlen(argv[2])<3 &&\r
87 strspn(argv[2], "0123456789") == strlen (argv[2]) &&\r
88 strlen(argv[3])<3 &&\r
89 strspn(argv[3], "0123456789") == strlen (argv[3]) &&\r
90 strlen(argv[4])<14 &&\r
91 strspn(argv[4], "1234567,*") == strlen (argv[4]) &&\r
92 ((strcmp(argv[5], "-r")==0) || //reboot\r
93 (strcmp(argv[5], "-h")==0)) ) //hangup\r
94 ) {\r
95 fprintf (stderr, "Bad cron+ parameters!\n");\r
96 return 1;\r
97 }\r
98 \r
99 // remove old entry \r
100 safe_system("/usr/bin/perl -i -p -e 's/^.*\\/sbin\\/shutdown.*$//s' /var/spool/cron/root.orig");\r
101\r
102 // add new entry\r
103 FILE *fd = NULL;\r
104 if ((fd = fopen("/var/spool/cron/root.orig", "a")))\r
105 {\r
106 fprintf (fd,"%s %s * * %s /sbin/shutdown %s 1\n",argv[2],argv[3],argv[4],argv[5]);\r
107 fclose (fd);\r
108 }\r
109 \r
110 // inform cron\r
111 safe_system("/usr/bin/fcrontab -u root -z");\r
112 return 0;\r
113 }\r
114\r
115 fprintf (stderr, "Bad reboot command!\n");\r
116 return 1;\r
117}\r