bundle_util_file_write() has now 'flags' parameter.
[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 /* FC4 chkconfig-1.3.20-1 dumps its help to stderr and returns 1 for: chkconfig PACKAGE
70  * without its existing: /etc/init.d/PACKAGE
71  */
72 const gchar *command="chkconfig " PACKAGE " 2>/dev/null";
73
74         g_return_val_if_fail(UDPGATE_IS_STARTUP_CHKCONFIG(udpgate_startup),FALSE);
75
76         status=system(command);
77         status_0_1=STATUS_0_1(status);
78         if (status_0_1<0) {
79                 g_warning(_("Error checking registrance of this program automatic startup by: %s"),command);
80                 return FALSE;
81                 }
82         if (is_on)
83                 *is_on=(status_0_1==0);
84         return TRUE;
85 }
86
87 static gboolean udpgate_startup_chkconfig_on(UdpgateStartup *udpgate_startup)
88 {
89 const gchar *command="chkconfig --add " PACKAGE;
90 int status;
91
92         g_return_val_if_fail(UDPGATE_IS_STARTUP_CHKCONFIG(udpgate_startup),FALSE);
93
94 #ifdef ENABLE_BUNDLE
95         if (!bundle_util_file_write(
96                         INIT_D_PATHNAME,        /* pathname */
97                         PACKAGE ".init",        /* basename */
98                         0755,   /* pathname_mode */
99                         BUNDLE_UTIL_BACKUP_MASK))       /* flags */
100                 return FALSE;
101 #endif /* ENABLE_BUNDLE */
102
103         status=system(command);
104
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 static gboolean udpgate_startup_chkconfig_off(UdpgateStartup *udpgate_startup)
113 {
114 const gchar *command="chkconfig --del " PACKAGE;
115 int status;
116
117         g_return_val_if_fail(UDPGATE_IS_STARTUP_CHKCONFIG(udpgate_startup),FALSE);
118
119         status=system(command);
120         if (0!=STATUS_0_1(status)) {
121                 g_warning(_("Error removing program's system startup registrance by: %s"),command);
122                 return FALSE;
123                 }
124
125 #ifdef ENABLE_BUNDLE
126         if (!bundle_util_file_remove(INIT_D_PATHNAME,PACKAGE ".init"))
127                 return FALSE;
128 #endif /* ENABLE_BUNDLE */
129
130         return TRUE;
131 }
132
133 static void udpgate_startup_chkconfig_class_init (UdpgateStartupChkconfigClass *class)
134 {
135         UdpgateStartupClass *udpgate_startup_class = UDPGATE_STARTUP_CLASS(class);
136
137         udpgate_startup_class->init = udpgate_startup_chkconfig_init;
138         udpgate_startup_class->query = udpgate_startup_chkconfig_query;
139         udpgate_startup_class->on = udpgate_startup_chkconfig_on;
140         udpgate_startup_class->off = udpgate_startup_chkconfig_off;
141 }
142
143 GType udpgate_startup_chkconfig_get_type(void)
144 {
145   static GType udpgate_startup_chkconfig_type=0;
146
147   if (!udpgate_startup_chkconfig_type) {
148                 static const GTypeInfo udpgate_startup_chkconfig_info={
149                         sizeof(UdpgateStartupChkconfigClass),
150                         NULL,           /* base_init */
151                         NULL,           /* base_finalize */
152                         (GClassInitFunc)udpgate_startup_chkconfig_class_init,
153                         NULL,           /* class_finalize */
154                         NULL,           /* class_data */
155                         sizeof(UdpgateStartupChkconfig),
156                         0,              /* n_preallocs */
157                         NULL,           /* instance_init */
158                         NULL,           /* value_table */
159       };
160
161                 udpgate_startup_chkconfig_type=g_type_register_static(UDPGATE_TYPE_STARTUP,"UdpgateStartupChkconfig",
162                                  &udpgate_startup_chkconfig_info,0);
163     }
164
165   return udpgate_startup_chkconfig_type;
166 }