/* $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 STATUS_0_1(status) ( \ !(WIFEXITED((status)) && (WEXITSTATUS((status))==0 || WEXITSTATUS((status))==1) \ && !WIFSIGNALED((status)) \ && !WIFSTOPPED((status))) \ ? -1 : WEXITSTATUS((status))) static gboolean udpgate_startup_chkconfig_init(UdpgateStartup *udpgate_startup) { int status; const gchar *command="chkconfig " PACKAGE " 2>/dev/null"; g_return_val_if_fail(UDPGATE_IS_STARTUP_CHKCONFIG(udpgate_startup),FALSE); status=system(command); if (STATUS_0_1(status)<0) return FALSE; return TRUE; } static gboolean udpgate_startup_chkconfig_query(UdpgateStartup *udpgate_startup,gboolean *is_on) { int status,status_0_1; /* FC4 chkconfig-1.3.20-1 dumps its help to stderr and returns 1 for: chkconfig PACKAGE * without its existing: /etc/init.d/PACKAGE */ const gchar *command="chkconfig " PACKAGE " 2>/dev/null"; g_return_val_if_fail(UDPGATE_IS_STARTUP_CHKCONFIG(udpgate_startup),FALSE); 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; } if (is_on) *is_on=(status_0_1==0); return TRUE; } static gboolean udpgate_startup_chkconfig_on(UdpgateStartup *udpgate_startup) { const gchar *command="chkconfig --add " PACKAGE; int status; g_return_val_if_fail(UDPGATE_IS_STARTUP_CHKCONFIG(udpgate_startup),FALSE); #ifdef ENABLE_BUNDLE if (!bundle_util_file_write( INIT_D_PATHNAME, /* pathname */ PACKAGE ".init", /* basename */ 0755, /* pathname_mode */ BUNDLE_UTIL_BACKUP_MASK)) /* flags */ return FALSE; #endif /* ENABLE_BUNDLE */ status=system(command); if (0!=STATUS_0_1(status)) { g_warning(_("Error registering automatic program startup by: %s"),command); return FALSE; } return TRUE; } static gboolean udpgate_startup_chkconfig_off(UdpgateStartup *udpgate_startup) { const gchar *command="chkconfig --del " PACKAGE; int status; g_return_val_if_fail(UDPGATE_IS_STARTUP_CHKCONFIG(udpgate_startup),FALSE); 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; } static void udpgate_startup_chkconfig_class_init (UdpgateStartupChkconfigClass *class) { UdpgateStartupClass *udpgate_startup_class = UDPGATE_STARTUP_CLASS(class); udpgate_startup_class->init = udpgate_startup_chkconfig_init; udpgate_startup_class->query = udpgate_startup_chkconfig_query; udpgate_startup_class->on = udpgate_startup_chkconfig_on; udpgate_startup_class->off = udpgate_startup_chkconfig_off; } GType udpgate_startup_chkconfig_get_type(void) { static GType udpgate_startup_chkconfig_type=0; if (!udpgate_startup_chkconfig_type) { static const GTypeInfo udpgate_startup_chkconfig_info={ sizeof(UdpgateStartupChkconfigClass), NULL, /* base_init */ NULL, /* base_finalize */ (GClassInitFunc)udpgate_startup_chkconfig_class_init, NULL, /* class_finalize */ NULL, /* class_data */ sizeof(UdpgateStartupChkconfig), 0, /* n_preallocs */ NULL, /* instance_init */ NULL, /* value_table */ }; udpgate_startup_chkconfig_type=g_type_register_static(UDPGATE_TYPE_STARTUP,"UdpgateStartupChkconfig", &udpgate_startup_chkconfig_info,0); } return udpgate_startup_chkconfig_type; }