c397f462caf39ac7a59ca8bf5c81d5a5ca8f0507
[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(INIT_D_PATHNAME,PACKAGE ".init",0755))
98                 return FALSE;
99 #endif /* ENABLE_BUNDLE */
100         status=system(command);
101         if (0!=STATUS_0_1(status)) {
102                 g_warning(_("Error registering automatic program startup by: %s"),command);
103                 return FALSE;
104                 }
105         return TRUE;
106 }
107
108 gboolean startup_chkconfig_off(void)
109 {
110 const gchar *command="chkconfig --del " PACKAGE;
111 int status;
112
113         if (!startup_chkconfig_init())
114                 return FALSE;
115
116         status=system(command);
117         if (0!=STATUS_0_1(status)) {
118                 g_warning(_("Error removing program's system startup registrance by: %s"),command);
119                 return FALSE;
120                 }
121 #ifdef ENABLE_BUNDLE
122         if (!bundle_util_file_remove(INIT_D_PATHNAME,PACKAGE ".init"))
123                 return FALSE;
124 #endif /* ENABLE_BUNDLE */
125         return TRUE;
126 }