+Flush configuration also just after the options parsing.
[udpgate.git] / src / startup-chkconfig.c
1 /* $Id$
2  * UDP Gateway utility startup scripts support using chkconfig(8)
3  * Copyright (C) 2004 Jan Kratochvil <project-udpgate@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include <glib/gmessages.h>
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <unistd.h>
27 #include <glib/gmem.h>
28 #include <unistd.h>
29 #include <string.h>
30
31 #include "startup-chkconfig.h"  /* self */
32
33 #ifdef ENABLE_BUNDLE
34 #include "bundle-util.h"
35 #endif
36
37
38 /* Config: */
39 #define INIT_D_PATHNAME G_STRINGIFY(SYSCONFDIR) "/rc.d/init.d/" PACKAGE
40
41
42 #define STATUS_0_1(status) ( \
43                 !(WIFEXITED((status)) && (WEXITSTATUS((status))==0 || WEXITSTATUS((status))==1) \
44                                                 && !WIFSIGNALED((status)) \
45                                                 && !WIFSTOPPED((status))) \
46                                 ? -1 : WEXITSTATUS((status)))
47
48 gboolean startup_chkconfig_init(void)
49 {
50 static gboolean r,r_set=FALSE;
51
52         if (!r_set) {
53 int status;
54 const gchar *command="chkconfig " PACKAGE;
55
56                 status=system(command);
57                 if (STATUS_0_1(status)<0) {
58                         g_warning(_("Error checking validity of chkconfig(8) setup; automatic startup disabled; failed command: %s"),command);
59                         r=FALSE;
60                         }
61                 else
62                         r=TRUE;
63                 r_set=TRUE;
64                 }
65         return r;
66 }
67
68 gboolean startup_chkconfig_query(gboolean *is_on)
69 {
70 int status,status_0_1;
71 const gchar *command="chkconfig " PACKAGE;
72
73         g_return_val_if_fail(is_on!=NULL,FALSE);
74
75         if (!startup_chkconfig_init())
76                 return FALSE;
77
78         status=system(command);
79         status_0_1=STATUS_0_1(status);
80         if (status_0_1<0) {
81                 g_warning(_("Error checking registrance of this program automatic startup by: %s"),command);
82                 return FALSE;
83                 }
84         *is_on=(status_0_1==0);
85         return TRUE;
86 }
87
88 gboolean startup_chkconfig_on(void)
89 {
90 int status;
91 const gchar *command="chkconfig --add " PACKAGE;
92
93         if (!startup_chkconfig_init())
94                 return FALSE;
95
96 #ifdef ENABLE_BUNDLE
97         if (!bundle_util_file_write(
98                         INIT_D_PATHNAME,        /* pathname */
99                         PACKAGE ".init",        /* basename */
100                         0755,   /* pathname_mode */
101                         TRUE))  /* pathname_backup */
102                 return FALSE;
103 #endif /* ENABLE_BUNDLE */
104         status=system(command);
105         if (0!=STATUS_0_1(status)) {
106                 g_warning(_("Error registering automatic program startup by: %s"),command);
107                 return FALSE;
108                 }
109         return TRUE;
110 }
111
112 gboolean startup_chkconfig_off(void)
113 {
114 const gchar *command="chkconfig --del " PACKAGE;
115 int status;
116
117         if (!startup_chkconfig_init())
118                 return FALSE;
119
120         status=system(command);
121         if (0!=STATUS_0_1(status)) {
122                 g_warning(_("Error removing program's system startup registrance by: %s"),command);
123                 return FALSE;
124                 }
125 #ifdef ENABLE_BUNDLE
126         if (!bundle_util_file_remove(INIT_D_PATHNAME,PACKAGE ".init"))
127                 return FALSE;
128 #endif /* ENABLE_BUNDLE */
129         return TRUE;
130 }