2 * UDP Gateway utility startup scripts support using debian(8)
3 * Copyright (C) 2004 Jan Kratochvil <project-udpgate@jankratochvil.net>
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
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.
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
22 #include <glib/gmessages.h>
24 #include <sys/types.h>
27 #include <glib/gmem.h>
34 #include "startup-debian.h" /* self */
37 #include "bundle-util.h"
42 /* Do not: /etc/rc.d/init.d/...
43 * to comply with http://www.linuxbase.org/spec/booksets/LSB-Core/LSB-Core.html#INITSRCINSTRM
45 #define INIT_D_PATHNAME G_STRINGIFY(SYSCONFDIR) "/init.d/" PACKAGE
48 #define STATUS_0_1(status) ( \
49 !(WIFEXITED((status)) && (WEXITSTATUS((status))==0 || WEXITSTATUS((status))==1) \
50 && !WIFSIGNALED((status)) \
51 && !WIFSTOPPED((status))) \
52 ? -1 : WEXITSTATUS((status)))
55 static gboolean udpgate_startup_debian_init(UdpgateStartup *udpgate_startup)
58 /* ">/dev/null" for: Adding system startup for /etc/init.d/PACKAGE ...
59 * "2>/dev/null" for: bash: update-rc.d: command not found
61 const gchar *command="update-rc.d -n " PACKAGE " defaults &>/dev/null";
63 g_return_val_if_fail(UDPGATE_IS_STARTUP_DEBIAN(udpgate_startup),FALSE);
65 status=system(command);
66 if (STATUS_0_1(status)<0) {
67 g_warning(_("Error checking validity of update-rc.d(8) setup; automatic startup disabled; failed command: %s"),command);
73 static gboolean udpgate_startup_debian_query(UdpgateStartup *udpgate_startup,gboolean *is_on)
75 const gchar *command="update-rc.d -n " PACKAGE " defaults 2>/dev/null";
76 static char buffer[0x1000];
81 g_return_val_if_fail(UDPGATE_IS_STARTUP_DEBIAN(udpgate_startup),FALSE);
83 if (!(f=popen(command,"r"))) {
85 g_warning(_("Error checking registrance of this program automatic startup opening \"%s\": %m"),command);
89 fread_got=fread(buffer,1,sizeof(buffer),f);
92 g_warning(_("Error checking registrance of this program automatic startup closing \"%s\": %m"),command);
96 if (fread_got<=0 || fread_got>=sizeof(buffer)) {
98 g_warning(_("Error checking registrance of this program automatic startup reading \"%s\": %m"),command);
102 buffer[fread_got]='\0';
103 for (s=buffer;*s && isspace(*s);s++);
104 /* Fortunately update-rc.d(8) is just a dumb script without messages localization. */
106 #define TEXT_ADDING_SYSTEM_STARTUP_FOR "Adding system startup for"
108 Adding system startup for /etc/init.d/PACKAGE ...
109 /etc/rc0.d/K20PACKAGE -> ../init.d/PACKAGE
111 /etc/rc5.d/S20PACKAGE -> ../init.d/PACKAGE
113 if (!strncasecmp(s,TEXT_ADDING_SYSTEM_STARTUP_FOR,strlen(TEXT_ADDING_SYSTEM_STARTUP_FOR))) {
119 #define TEXT_SYSTEM_STARTUP_LINKS_FOR "System startup links for"
121 System startup links for /etc/init.d/PACKAGE already exist.
123 if (!strncasecmp(s,TEXT_SYSTEM_STARTUP_LINKS_FOR,strlen(TEXT_SYSTEM_STARTUP_LINKS_FOR))) {
130 if ((s=strchr(buffer,'\n')))
132 g_warning(_("Error checking registrance of this program, unknown output of \"%s\": %s"),command,buffer);
136 static gboolean udpgate_startup_debian_on(UdpgateStartup *udpgate_startup)
138 /* Do not: " >/dev/null"
139 * as it is useful for: Adding system startup for /etc/init.d/PACKAGE ...
141 * Adding system startup for /etc/init.d/PACKAGE ...
142 * /etc/rc0.d/K20PACKAGE -> ../init.d/PACKAGE
143 * update-rc.d: symlink: Permission denied
144 * as the second line also gets hidden.
146 const gchar *command="update-rc.d " PACKAGE " defaults";
149 g_return_val_if_fail(UDPGATE_IS_STARTUP_DEBIAN(udpgate_startup),FALSE);
152 if (!bundle_util_file_write(
153 INIT_D_PATHNAME, /* pathname */
154 PACKAGE ".init", /* basename */
155 0755, /* pathname_mode */
156 TRUE)) /* pathname_backup */
158 #endif /* ENABLE_BUNDLE */
160 status=system(command);
162 if (0!=STATUS_0_1(status)) {
163 g_warning(_("Error registering automatic program startup by: %s"),command);
169 static gboolean udpgate_startup_debian_off(UdpgateStartup *udpgate_startup)
171 /* Do not: " >/dev/null"
172 * as it is useful for: Removing any system startup links for /etc/init.d/PACKAGE ...
174 * Removing any system startup links for /etc/init.d/PACKAGE ...
175 * /etc/rc0.d/S30PACKAGE
176 * update-rc.d: unlink: Permission denied
177 * as the second line also gets hidden.
179 /* "-f" can be prevented for: ENABLE_BUNDLE
180 * by removing 'INIT_D_PATHNAME' first but it would still not work for
181 * regular non-bundled packaging.
183 const gchar *command="update-rc.d -f " PACKAGE " remove";
186 g_return_val_if_fail(UDPGATE_IS_STARTUP_DEBIAN(udpgate_startup),FALSE);
189 /* init.d file must no longer exist for: update-rc.d PACKAGE remove
190 * without using its: -f|--force
192 if (!bundle_util_file_remove(INIT_D_PATHNAME,PACKAGE ".init"))
194 #endif /* ENABLE_BUNDLE */
196 status=system(command);
197 if (0!=STATUS_0_1(status)) {
198 g_warning(_("Error removing program's system startup registrance by: %s"),command);
205 static void udpgate_startup_debian_class_init (UdpgateStartupDebianClass *class)
207 UdpgateStartupClass *udpgate_startup_class = UDPGATE_STARTUP_CLASS(class);
209 udpgate_startup_class->init = udpgate_startup_debian_init;
210 udpgate_startup_class->query = udpgate_startup_debian_query;
211 udpgate_startup_class->on = udpgate_startup_debian_on;
212 udpgate_startup_class->off = udpgate_startup_debian_off;
215 GType udpgate_startup_debian_get_type(void)
217 static GType udpgate_startup_debian_type=0;
219 if (!udpgate_startup_debian_type) {
220 static const GTypeInfo udpgate_startup_debian_info={
221 sizeof(UdpgateStartupDebianClass),
222 NULL, /* base_init */
223 NULL, /* base_finalize */
224 (GClassInitFunc)udpgate_startup_debian_class_init,
225 NULL, /* class_finalize */
226 NULL, /* class_data */
227 sizeof(UdpgateStartupDebian),
229 NULL, /* instance_init */
230 NULL, /* value_table */
233 udpgate_startup_debian_type=g_type_register_static(UDPGATE_TYPE_STARTUP,"UdpgateStartupDebian",
234 &udpgate_startup_debian_info,0);
237 return udpgate_startup_debian_type;