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 /* 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
72 const gchar *command="chkconfig " PACKAGE " 2>/dev/null";
74 g_return_val_if_fail(UDPGATE_IS_STARTUP_CHKCONFIG(udpgate_startup),FALSE);
76 status=system(command);
77 status_0_1=STATUS_0_1(status);
79 g_warning(_("Error checking registrance of this program automatic startup by: %s"),command);
83 *is_on=(status_0_1==0);
87 static gboolean udpgate_startup_chkconfig_on(UdpgateStartup *udpgate_startup)
89 const gchar *command="chkconfig --add " PACKAGE;
92 g_return_val_if_fail(UDPGATE_IS_STARTUP_CHKCONFIG(udpgate_startup),FALSE);
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 */
101 #endif /* ENABLE_BUNDLE */
103 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_chkconfig_off(UdpgateStartup *udpgate_startup)
114 const gchar *command="chkconfig --del " PACKAGE;
117 g_return_val_if_fail(UDPGATE_IS_STARTUP_CHKCONFIG(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_chkconfig_class_init (UdpgateStartupChkconfigClass *class)
135 UdpgateStartupClass *udpgate_startup_class = UDPGATE_STARTUP_CLASS(class);
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;
143 GType udpgate_startup_chkconfig_get_type(void)
145 static GType udpgate_startup_chkconfig_type=0;
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),
157 NULL, /* instance_init */
158 NULL, /* value_table */
161 udpgate_startup_chkconfig_type=g_type_register_static(UDPGATE_TYPE_STARTUP,"UdpgateStartupChkconfig",
162 &udpgate_startup_chkconfig_info,0);
165 return udpgate_startup_chkconfig_type;