2 * UDP Gateway utility startup scripts support using lsb(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>
32 #include "startup-lsb.h" /* self */
35 #include "bundle-util.h"
40 /* Do not: /etc/rc.d/init.d/...
41 * to comply with http://www.linuxbase.org/spec/booksets/LSB-Core/LSB-Core.html#INITSRCINSTRM
43 #define INIT_D_PATHNAME G_STRINGIFY(SYSCONFDIR) "/init.d/" PACKAGE
45 #define COMMAND_INSTALL_INIT_D "/usr/lib/lsb/install_initd"
46 #define COMMAND_REMOVE_INIT_D "/usr/lib/lsb/remove_initd"
49 #define STATUS_0_1(status) ( \
50 !(WIFEXITED((status)) && (WEXITSTATUS((status))==0 || WEXITSTATUS((status))==1) \
51 && !WIFSIGNALED((status)) \
52 && !WIFSTOPPED((status))) \
53 ? -1 : WEXITSTATUS((status)))
55 static gboolean chkexec(const gchar *binary)
59 g_return_val_if_fail(binary!=NULL,FALSE);
61 if (stat(COMMAND_INSTALL_INIT_D,&statbuf))
63 if ((statbuf.st_mode&(S_IFMT|0111))!=(S_IFREG|0111))
70 static gboolean udpgate_startup_lsb_init(UdpgateStartup *udpgate_startup)
72 g_return_val_if_fail(UDPGATE_IS_STARTUP_LSB(udpgate_startup),FALSE);
74 /* LSB compliant 'query' is not standardized. */
75 if (chkexec(COMMAND_INSTALL_INIT_D) && chkexec(COMMAND_REMOVE_INIT_D))
80 static gboolean udpgate_startup_lsb_query(UdpgateStartup *udpgate_startup,gboolean *is_on)
82 g_return_val_if_fail(UDPGATE_IS_STARTUP_LSB(udpgate_startup),FALSE);
84 /* LSB compliant 'query' is not standardized. */
88 static gboolean udpgate_startup_lsb_on(UdpgateStartup *udpgate_startup)
90 const gchar *command=COMMAND_INSTALL_INIT_D " " PACKAGE;
93 g_return_val_if_fail(UDPGATE_IS_STARTUP_LSB(udpgate_startup),FALSE);
96 if (!bundle_util_file_write(
97 INIT_D_PATHNAME, /* pathname */
98 PACKAGE ".init", /* basename */
99 0755, /* pathname_mode */
100 BUNDLE_UTIL_BACKUP_MASK)) /* flags */
102 #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);
112 static gboolean udpgate_startup_lsb_off(UdpgateStartup *udpgate_startup)
114 const gchar *command=COMMAND_REMOVE_INIT_D " " PACKAGE;
117 g_return_val_if_fail(UDPGATE_IS_STARTUP_LSB(udpgate_startup),FALSE);
119 status=system(command);
120 if (0!=STATUS_0_1(status)) {
121 g_warning(_("Error removing program's system startup registrance by: %s"),command);
126 if (!bundle_util_file_remove(INIT_D_PATHNAME,PACKAGE ".init"))
128 #endif /* ENABLE_BUNDLE */
133 static void udpgate_startup_lsb_class_init (UdpgateStartupLsbClass *class)
135 UdpgateStartupClass *udpgate_startup_class = UDPGATE_STARTUP_CLASS(class);
137 udpgate_startup_class->init = udpgate_startup_lsb_init;
138 udpgate_startup_class->query = udpgate_startup_lsb_query;
139 udpgate_startup_class->on = udpgate_startup_lsb_on;
140 udpgate_startup_class->off = udpgate_startup_lsb_off;
143 GType udpgate_startup_lsb_get_type(void)
145 static GType udpgate_startup_lsb_type=0;
147 if (!udpgate_startup_lsb_type) {
148 static const GTypeInfo udpgate_startup_lsb_info={
149 sizeof(UdpgateStartupLsbClass),
150 NULL, /* base_init */
151 NULL, /* base_finalize */
152 (GClassInitFunc)udpgate_startup_lsb_class_init,
153 NULL, /* class_finalize */
154 NULL, /* class_data */
155 sizeof(UdpgateStartupLsb),
157 NULL, /* instance_init */
158 NULL, /* value_table */
161 udpgate_startup_lsb_type=g_type_register_static(UDPGATE_TYPE_STARTUP,"UdpgateStartupLsb",
162 &udpgate_startup_lsb_info,0);
165 return udpgate_startup_lsb_type;