/* $Id$ * UDP Gateway utility startup scripts support using chkconfig(8) * Copyright (C) 2004 Jan Kratochvil * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; exactly version 2 of June 1991 is required * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "config.h" #include #include #include #include #include #include #include #include #include #include "startup-chkconfig.h" /* self */ #ifdef ENABLE_BUNDLE #include "bundle-util.h" #endif /* Config: */ /* Do not: /etc/rc.d/init.d/... * to comply with http://www.linuxbase.org/spec/booksets/LSB-Core/LSB-Core.html#INITSRCINSTRM */ #define INIT_D_PATHNAME G_STRINGIFY(SYSCONFDIR) "/init.d/" PACKAGE #define COMMAND_INSTALL_INIT_D "/usr/lib/lsb/install_initd" #define COMMAND_REMOVE_INIT_D "/usr/lib/lsb/remove_initd" #define STATUS_0_1(status) ( \ !(WIFEXITED((status)) && (WEXITSTATUS((status))==0 || WEXITSTATUS((status))==1) \ && !WIFSIGNALED((status)) \ && !WIFSTOPPED((status))) \ ? -1 : WEXITSTATUS((status))) static gboolean chkexec(const gchar *binary) { struct stat statbuf; g_return_val_if_fail(binary!=NULL,FALSE); if (stat(COMMAND_INSTALL_INIT_D,&statbuf)) return FALSE; if ((statbuf.st_mode&(S_IFMT|0111))!=(S_IFREG|0111)) return FALSE; return TRUE; } gboolean startup_chkconfig_init(void) { static gboolean r,r_set=FALSE; if (!r_set) { int status; const gchar *command="chkconfig " PACKAGE; /* LSB compliant 'query' is not standardized. */ if (chkexec(COMMAND_INSTALL_INIT_D) && chkexec(COMMAND_REMOVE_INIT_D)) r=TRUE; else { status=system(command); if (STATUS_0_1(status)<0) { g_warning(_("Error checking validity of chkconfig(8) setup; automatic startup disabled; failed command: %s"),command); r=FALSE; } else r=TRUE; } r_set=TRUE; } return r; } gboolean startup_chkconfig_query(gboolean *is_on) { int status,status_0_1; const gchar *command="chkconfig " PACKAGE; g_return_val_if_fail(is_on!=NULL,FALSE); if (!startup_chkconfig_init()) return FALSE; /* LSB compliant 'query' is not standardized. */ status=system(command); status_0_1=STATUS_0_1(status); if (status_0_1<0) { g_warning(_("Error checking registrance of this program automatic startup by: %s"),command); return FALSE; } *is_on=(status_0_1==0); return TRUE; } gboolean startup_chkconfig_on(void) { int status; const gchar *command="chkconfig --add"; if (!startup_chkconfig_init()) return FALSE; #ifdef ENABLE_BUNDLE if (!bundle_util_file_write( INIT_D_PATHNAME, /* pathname */ PACKAGE ".init", /* basename */ 0755, /* pathname_mode */ TRUE)) /* pathname_backup */ return FALSE; #endif /* ENABLE_BUNDLE */ if (chkexec(COMMAND_INSTALL_INIT_D)) command=COMMAND_INSTALL_INIT_D; command=udpgate_printf_alloca("%s %s",command,PACKAGE); status=system(command); if (0!=STATUS_0_1(status)) { g_warning(_("Error registering automatic program startup by: %s"),command); return FALSE; } return TRUE; } gboolean startup_chkconfig_off(void) { const gchar *command="chkconfig --del"; int status; if (!startup_chkconfig_init()) return FALSE; if (chkexec(COMMAND_REMOVE_INIT_D)) command=COMMAND_REMOVE_INIT_D; command=udpgate_printf_alloca("%s %s",command,PACKAGE); status=system(command); if (0!=STATUS_0_1(status)) { g_warning(_("Error removing program's system startup registrance by: %s"),command); return FALSE; } #ifdef ENABLE_BUNDLE if (!bundle_util_file_remove(INIT_D_PATHNAME,PACKAGE ".init")) return FALSE; #endif /* ENABLE_BUNDLE */ return TRUE; }