Implemented crossplatform automatic startup management.
[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 #include <sys/stat.h>
31
32 #include "startup-chkconfig.h"  /* self */
33
34 #ifdef ENABLE_BUNDLE
35 #include "bundle-util.h"
36 #endif
37
38
39 /* Config: */
40 /* Do not: /etc/rc.d/init.d/...
41  * to comply with http://www.linuxbase.org/spec/booksets/LSB-Core/LSB-Core.html#INITSRCINSTRM
42  */
43 #define INIT_D_PATHNAME G_STRINGIFY(SYSCONFDIR) "/init.d/" PACKAGE
44
45
46 #define STATUS_0_1(status) ( \
47                 !(WIFEXITED((status)) && (WEXITSTATUS((status))==0 || WEXITSTATUS((status))==1) \
48                                                 && !WIFSIGNALED((status)) \
49                                                 && !WIFSTOPPED((status))) \
50                                 ? -1 : WEXITSTATUS((status)))
51
52
53 static gboolean udpgate_startup_chkconfig_init(UdpgateStartup *udpgate_startup)
54 {
55 int status;
56 const gchar *command="chkconfig " PACKAGE " 2>/dev/null";
57
58         g_return_val_if_fail(UDPGATE_IS_STARTUP_CHKCONFIG(udpgate_startup),FALSE);
59
60         status=system(command);
61         if (STATUS_0_1(status)<0)
62                 return FALSE;
63         return TRUE;
64 }
65
66 static gboolean udpgate_startup_chkconfig_query(UdpgateStartup *udpgate_startup,gboolean *is_on)
67 {
68 int status,status_0_1;
69 const gchar *command="chkconfig " PACKAGE;
70
71         g_return_val_if_fail(UDPGATE_IS_STARTUP_CHKCONFIG(udpgate_startup),FALSE);
72
73         status=system(command);
74         status_0_1=STATUS_0_1(status);
75         if (status_0_1<0) {
76                 g_warning(_("Error checking registrance of this program automatic startup by: %s"),command);
77                 return FALSE;
78                 }
79         if (is_on)
80                 *is_on=(status_0_1==0);
81         return TRUE;
82 }
83
84 static gboolean udpgate_startup_chkconfig_on(UdpgateStartup *udpgate_startup)
85 {
86 const gchar *command="chkconfig --add " PACKAGE;
87 int status;
88
89         g_return_val_if_fail(UDPGATE_IS_STARTUP_CHKCONFIG(udpgate_startup),FALSE);
90
91 #ifdef ENABLE_BUNDLE
92         if (!bundle_util_file_write(
93                         INIT_D_PATHNAME,        /* pathname */
94                         PACKAGE ".init",        /* basename */
95                         0755,   /* pathname_mode */
96                         TRUE))  /* pathname_backup */
97                 return FALSE;
98 #endif /* ENABLE_BUNDLE */
99
100         status=system(command);
101
102         if (0!=STATUS_0_1(status)) {
103                 g_warning(_("Error registering automatic program startup by: %s"),command);
104                 return FALSE;
105                 }
106         return TRUE;
107 }
108
109 static gboolean udpgate_startup_chkconfig_off(UdpgateStartup *udpgate_startup)
110 {
111 const gchar *command="chkconfig --del " PACKAGE;
112 int status;
113
114         g_return_val_if_fail(UDPGATE_IS_STARTUP_CHKCONFIG(udpgate_startup),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
122 #ifdef ENABLE_BUNDLE
123         if (!bundle_util_file_remove(INIT_D_PATHNAME,PACKAGE ".init"))
124                 return FALSE;
125 #endif /* ENABLE_BUNDLE */
126
127         return TRUE;
128 }
129
130 static void udpgate_startup_chkconfig_class_init (UdpgateStartupChkconfigClass *class)
131 {
132         UdpgateStartupClass *udpgate_startup_class = UDPGATE_STARTUP_CLASS(class);
133
134         udpgate_startup_class->init = udpgate_startup_chkconfig_init;
135         udpgate_startup_class->query = udpgate_startup_chkconfig_query;
136         udpgate_startup_class->on = udpgate_startup_chkconfig_on;
137         udpgate_startup_class->off = udpgate_startup_chkconfig_off;
138 }
139
140 GType udpgate_startup_chkconfig_get_type(void)
141 {
142   static GType udpgate_startup_chkconfig_type=0;
143
144   if (!udpgate_startup_chkconfig_type) {
145                 static const GTypeInfo udpgate_startup_chkconfig_info={
146                         sizeof(UdpgateStartupChkconfigClass),
147                         NULL,           /* base_init */
148                         NULL,           /* base_finalize */
149                         (GClassInitFunc)udpgate_startup_chkconfig_class_init,
150                         NULL,           /* class_finalize */
151                         NULL,           /* class_data */
152                         sizeof(UdpgateStartupChkconfig),
153                         0,              /* n_preallocs */
154                         NULL,           /* instance_init */
155                         NULL,           /* value_table */
156       };
157
158                 udpgate_startup_chkconfig_type=g_type_register_static(UDPGATE_TYPE_STARTUP,"UdpgateStartupChkconfig",
159                                  &udpgate_startup_chkconfig_info,0);
160     }
161
162   return udpgate_startup_chkconfig_type;
163 }