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
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 gboolean startup_chkconfig_init(void)
72 static gboolean r,r_set=FALSE;
76 const gchar *command="chkconfig " PACKAGE;
78 /* LSB compliant 'query' is not standardized. */
79 if (chkexec(COMMAND_INSTALL_INIT_D) && chkexec(COMMAND_REMOVE_INIT_D))
82 status=system(command);
83 if (STATUS_0_1(status)<0) {
84 g_warning(_("Error checking validity of chkconfig(8) setup; automatic startup disabled; failed command: %s"),command);
95 gboolean startup_chkconfig_query(gboolean *is_on)
97 int status,status_0_1;
98 const gchar *command="chkconfig " PACKAGE;
100 g_return_val_if_fail(is_on!=NULL,FALSE);
102 if (!startup_chkconfig_init())
105 /* LSB compliant 'query' is not standardized. */
106 status=system(command);
107 status_0_1=STATUS_0_1(status);
109 g_warning(_("Error checking registrance of this program automatic startup by: %s"),command);
112 *is_on=(status_0_1==0);
116 gboolean startup_chkconfig_on(void)
119 const gchar *command="chkconfig --add";
121 if (!startup_chkconfig_init())
125 if (!bundle_util_file_write(
126 INIT_D_PATHNAME, /* pathname */
127 PACKAGE ".init", /* basename */
128 0755, /* pathname_mode */
129 TRUE)) /* pathname_backup */
131 #endif /* ENABLE_BUNDLE */
133 if (chkexec(COMMAND_INSTALL_INIT_D))
134 command=COMMAND_INSTALL_INIT_D;
135 command=udpgate_printf_alloca("%s %s",command,PACKAGE);
136 status=system(command);
138 if (0!=STATUS_0_1(status)) {
139 g_warning(_("Error registering automatic program startup by: %s"),command);
145 gboolean startup_chkconfig_off(void)
147 const gchar *command="chkconfig --del";
150 if (!startup_chkconfig_init())
153 if (chkexec(COMMAND_REMOVE_INIT_D))
154 command=COMMAND_REMOVE_INIT_D;
155 command=udpgate_printf_alloca("%s %s",command,PACKAGE);
156 status=system(command);
157 if (0!=STATUS_0_1(status)) {
158 g_warning(_("Error removing program's system startup registrance by: %s"),command);
162 if (!bundle_util_file_remove(INIT_D_PATHNAME,PACKAGE ".init"))
164 #endif /* ENABLE_BUNDLE */