2 * UDP Gateway utility startup scripts support using chkconfig(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-chkconfig.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
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)))
53 static gboolean udpgate_startup_chkconfig_init(UdpgateStartup *udpgate_startup)
56 const gchar *command="chkconfig " PACKAGE " 2>/dev/null";
58 g_return_val_if_fail(UDPGATE_IS_STARTUP_CHKCONFIG(udpgate_startup),FALSE);
60 status=system(command);
61 if (STATUS_0_1(status)<0)
66 static gboolean udpgate_startup_chkconfig_query(UdpgateStartup *udpgate_startup,gboolean *is_on)
68 int status,status_0_1;
69 const gchar *command="chkconfig " PACKAGE;
71 g_return_val_if_fail(UDPGATE_IS_STARTUP_CHKCONFIG(udpgate_startup),FALSE);
73 status=system(command);
74 status_0_1=STATUS_0_1(status);
76 g_warning(_("Error checking registrance of this program automatic startup by: %s"),command);
80 *is_on=(status_0_1==0);
84 static gboolean udpgate_startup_chkconfig_on(UdpgateStartup *udpgate_startup)
86 const gchar *command="chkconfig --add " PACKAGE;
89 g_return_val_if_fail(UDPGATE_IS_STARTUP_CHKCONFIG(udpgate_startup),FALSE);
92 if (!bundle_util_file_write(
93 INIT_D_PATHNAME, /* pathname */
94 PACKAGE ".init", /* basename */
95 0755, /* pathname_mode */
96 TRUE)) /* pathname_backup */
98 #endif /* ENABLE_BUNDLE */
100 status=system(command);
102 if (0!=STATUS_0_1(status)) {
103 g_warning(_("Error registering automatic program startup by: %s"),command);
109 static gboolean udpgate_startup_chkconfig_off(UdpgateStartup *udpgate_startup)
111 const gchar *command="chkconfig --del " PACKAGE;
114 g_return_val_if_fail(UDPGATE_IS_STARTUP_CHKCONFIG(udpgate_startup),FALSE);
116 status=system(command);
117 if (0!=STATUS_0_1(status)) {
118 g_warning(_("Error removing program's system startup registrance by: %s"),command);
123 if (!bundle_util_file_remove(INIT_D_PATHNAME,PACKAGE ".init"))
125 #endif /* ENABLE_BUNDLE */
130 static void udpgate_startup_chkconfig_class_init (UdpgateStartupChkconfigClass *class)
132 UdpgateStartupClass *udpgate_startup_class = UDPGATE_STARTUP_CLASS(class);
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;
140 GType udpgate_startup_chkconfig_get_type(void)
142 static GType udpgate_startup_chkconfig_type=0;
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),
154 NULL, /* instance_init */
155 NULL, /* value_table */
158 udpgate_startup_chkconfig_type=g_type_register_static(UDPGATE_TYPE_STARTUP,"UdpgateStartupChkconfig",
159 &udpgate_startup_chkconfig_info,0);
162 return udpgate_startup_chkconfig_type;