Fix #71: Add support for enabling/disabling services in /etc/finit.d

This patch adds support for enabling and disabling services in
/etc/finit.d/ by adding another subdirectory:

    /etc/finit.d/available

Enabled service/run/task stanzas are by convention symlinked
to the parent dir.  The initctl tool has three new convenience
commands to facilitate operating with this:

   initctl list
   initctl enable  <CONF>
   initctl disable <CONF>

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson
2017-06-29 22:23:38 +02:00
parent 023a881fb9
commit 3428cd48b7
4 changed files with 220 additions and 4 deletions
+2 -2
View File
@@ -41,8 +41,8 @@ else
finit_LDADD += -ldl
endif
initctl_SOURCES = initctl.c svc.c svc.h cond.c cond.h \
util.c util.h
initctl_SOURCES = initctl.c serv.c serv.h svc.c svc.h \
cond.c cond.h util.c util.h
initctl_CFLAGS = -W -Wall -Wextra -Wno-unused-parameter $(lite_CFLAGS)
initctl_LDADD = $(lite_LIBS)
+17 -2
View File
@@ -21,11 +21,12 @@
* THE SOFTWARE.
*/
#include "config.h"
#include <err.h>
#include <ftw.h>
#include <ctype.h>
#include <getopt.h>
#include <glob.h>
#include <paths.h>
#include <signal.h>
#include <stdio.h>
@@ -36,9 +37,9 @@
#include <sys/un.h>
#include <lite/lite.h>
#include "config.h"
#include "finit.h"
#include "cond.h"
#include "serv.h"
#include "service.h"
#include "util.h"
@@ -54,6 +55,7 @@ int verbose = 0;
int silent = 1; /* For helpers.c */
int runlevel = 0;
static int do_send(struct init_request *rq, ssize_t len)
{
int sd, result = 255;
@@ -584,16 +586,24 @@ static int usage(int rc)
" reload Reload *.conf in /etc/finit.d and activate changes\n"
" runlevel [0-9] Show or set runlevel: 0 halt, 6 reboot\n"
" status | show Show status of services\n"
"\n"
" list List all .conf in /etc/finit.d/\n"
" enable <CONF> Enable .conf in /etc/finit.d/available/\n"
" disable <CONF> Disable .conf in /etc/finit.d/[enabled/]\n"
"\n"
" cond set <COND> Set (assert) condition => +COND\n"
" cond clear <COND> Clear (deassert) condition => -COND\n"
" cond show Show condition status\n"
" cond dump Show all conditions and their status\n"
"\n"
" start <JOB|NAME>[:ID] Start service by job# or name, with optional ID\n"
" stop <JOB|NAME>[:ID] Stop/Pause a running service by job# or name\n"
" restart <JOB|NAME>[:ID] Restart (stop/start) service by job# or name\n"
"\n"
" reboot Reboot system\n"
" halt Halt system\n"
" poweroff Halt and power off system\n"
"\n"
" utmp show Raw dump of UTMP/WTMP db\n"
" version Show Finit version\n\n", prognm);
@@ -610,6 +620,11 @@ int main(int argc, char *argv[])
{ "runlevel", do_runlevel },
{ "status", show_status },
{ "show", show_status }, /* Convenience alias */
{ "list", serv_list },
{ "enable", serv_enable },
{ "disable", serv_disable },
{ "cond", do_cond },
{ "start", do_start },
{ "stop", do_stop },
+163
View File
@@ -0,0 +1,163 @@
/* List and enable/disable service configurations
*
* Copyright (c) 2017 Joachim Nilsson <troglobit@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "config.h"
#include <err.h>
#include <glob.h>
#include <stdio.h>
#include <lite/lite.h>
#define SCREEN_WIDTH 80 /* Calculate screen width as well, later */
static const char *cwd;
static const char *available = FINIT_RCSD "/available";
static const char *enabled = FINIT_RCSD "/enabled";
static void pushd(const char *dir)
{
cwd = get_current_dir_name();
chdir(dir);
}
static void popd(void)
{
chdir(cwd);
}
static int calc_width(char *arr[], size_t len)
{
int width = 0;
size_t i;
for (i = 0; i < len; i++) {
int w = strlen(arr[i]);
if (w > width)
width = w;
}
return width;
}
static void do_list(const char *heading, const char *path)
{
int num;
int width;
size_t i;
glob_t gl;
char h[SCREEN_WIDTH] = "===============================================================================";
pushd(path);
if (glob("*.conf", 0, NULL, &gl)) {
chdir(cwd);
return;
}
if (gl.gl_pathc <= 0)
goto done;
num = snprintf(h, sizeof(h) - 1, "%s", path);
h[num] = ' ';
puts(h);
width = calc_width(gl.gl_pathv, gl.gl_pathc);
num = SCREEN_WIDTH / width;
if ((num - 1) * 2 + num * width > SCREEN_WIDTH)
num--;
for (i = 0; i < gl.gl_pathc; i++) {
if (i > 0 && !(i % num))
puts("");
printf("%-*s ", width, gl.gl_pathv[i]);
}
puts("\n");
done:
globfree(&gl);
popd();
}
int serv_list(char *arg)
{
if (fisdir(available))
do_list("Available", available);
if (fisdir(enabled))
do_list("Enabled", enabled);
else if (fisdir(FINIT_RCSD))
do_list("Always enabled", FINIT_RCSD);
return 0;
}
int serv_enable(char *arg)
{
char corr[40];
char link[80];
char path[80];
if (!strstr(arg, ".conf")) {
snprintf(corr, sizeof(corr), "%s.conf", arg);
arg = corr;
}
pushd(FINIT_RCSD);
snprintf(path, sizeof(path), "available/%s", arg);
if (!fexist(path))
errx(1, "Cannot find %s", path);
snprintf(link, sizeof(link), "%s/%s", FINIT_RCSD, arg);
if (fexist(link))
errx(1, "%s already exists", link);
return symlink(path, link) != 0;
}
int serv_disable(char *arg)
{
char corr[40];
char link[80];
struct stat st;
if (!strstr(arg, ".conf")) {
snprintf(corr, sizeof(corr), "%s.conf", arg);
arg = corr;
}
snprintf(link, sizeof(link), "%s/%s", FINIT_RCSD, arg);
if (stat(link, &st))
err(1, "Cannot find %s", link);
if ((st.st_mode & S_IFMT) == S_IFLNK)
errx(1, "%s is not a symlink, move manually to %s first", link, available);
return remove(link) != 0;
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/
+38
View File
@@ -0,0 +1,38 @@
/* List and enable/disable service configurations
*
* Copyright (c) 2017 Joachim Nilsson <troglobit@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef FINIT_SERV_H_
#define FINIT_SERV_H_
int serv_list (char *arg);
int serv_enable (char *arg);
int serv_disable (char *arg);
#endif /* FINIT_SERV_H_ */
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/