From 413384f2e58110a8525a477d427ab48906b7eb57 Mon Sep 17 00:00:00 2001 From: Jaden Weiss Date: Sun, 31 Dec 2017 18:07:52 -0500 Subject: [PATCH] add support for stopping services --- src/inst.mk | 4 +++- src/linit-stop.sh | 4 ++++ src/linitctl.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/linitd.c | 11 +++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/linit-stop.sh diff --git a/src/inst.mk b/src/inst.mk index b7e9a2c..f7477a4 100644 --- a/src/inst.mk +++ b/src/inst.mk @@ -2,7 +2,7 @@ all: install install: cmds initd rcd rccommon -cmds: $(DESTDIR)/usr/bin/linitd $(DESTDIR)/usr/bin/linitctl $(DESTDIR)/usr/bin/linit-start +cmds: $(DESTDIR)/usr/bin/linitd $(DESTDIR)/usr/bin/linitctl $(DESTDIR)/usr/bin/linit-start $(DESTDIR)/usr/bin/linit-stop $(DESTDIR)/usr/bin/linitd: linitd.o install -D -m 0700 linitd.o $(DESTDIR)/usr/bin/linitd @@ -10,6 +10,8 @@ $(DESTDIR)/usr/bin/linitctl: linitctl.o install -D -m 0700 linitctl.o $(DESTDIR)/usr/bin/linitctl $(DESTDIR)/usr/bin/linit-start: linit-start.sh install -D -m 0700 linit-start.sh $(DESTDIR)/usr/bin/linit-start +$(DESTDIR)/usr/bin/linit-stop: linit-stop.sh + install -D -m 0700 linit-stop.sh $(DESTDIR)/usr/bin/linit-stop define initdscript $(DESTDIR)/etc/init.d/$(basename $(1)): scripts/init.d/$(1) diff --git a/src/linit-stop.sh b/src/linit-stop.sh new file mode 100644 index 0000000..787efc4 --- /dev/null +++ b/src/linit-stop.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +echo $1 $LINITSOCK +/etc/init.d/$1 stop && linitctl state $1 stopped diff --git a/src/linitctl.c b/src/linitctl.c index 368bee2..d19f253 100644 --- a/src/linitctl.c +++ b/src/linitctl.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -127,6 +128,47 @@ void cmd_state(int argc, char **argv, FILE *stream) { exit(65); } } +//command to stop a service +void cmd_stop(int argc, char **argv, FILE *stream) { + if(argc != 1) { + fprintf(stderr, "[FATAL] Missing arguments\n"); + exit(1); + } + char pre[] = "stop "; + size_t n = strlen(pre) + strlen(argv[0]) + 1; + char buf[n]; + char *b = buf; + strcpy(b, pre); + b += strlen(pre); + strcpy(b, argv[0]); + if(fwrite(buf, n, 1, stream) != 1) { + fprintf(stderr, "[FATAL] Failed to write command\n"); + exit(65); + } + //wait for completion + while(true) { + char *resp = rcvResp(stream); + char *stat = NULL; + int nspace = 0; + for(size_t i = 0; (resp[i] != '\0') && (nspace < 2); i++) { + if(resp[i] == ' ') { + stat = resp + i; + nspace++; + } + } + stat++; + if(nspace < 2) { + fprintf(stderr, "[FATAL] Bad response: \"%s\"\n", resp); + exit(65); + } + if(strcmp(stat, "stopped") == 0) { + free(resp); + return; + } + free(resp); + } +} + int main(int argc, char** argv) { if(argc < 2) { @@ -136,6 +178,8 @@ int main(int argc, char** argv) { FILE* stream = connectToServer(); if(strcmp(argv[1], "start") == 0) { cmd_start(argc - 2, argv + 2, stream); + } else if(strcmp(argv[1], "stop") == 0) { + cmd_stop(argc - 2, argv + 2, stream); } else if(strcmp(argv[1], "state") == 0) { cmd_state(argc - 2, argv + 2, stream); } else { diff --git a/src/linitd.c b/src/linitd.c index 7f57a33..72e7662 100644 --- a/src/linitd.c +++ b/src/linitd.c @@ -616,8 +616,19 @@ void cmd_stop(struct conn *c, char *arg) { goto end; } c->tx = ntx; + if(c->txoff && (c->tx.len > 0)) { //tx buffer was just populated + //enable write in epoll + struct epoll_event ev = {.events = EPOLLIN | EPOLLOUT, .data = {.ptr = &c->e}}; + if(epoll_ctl(epollfd, EPOLL_CTL_MOD, c->fd, &ev) == -1) { //failed to enable - close and forget + fprintf(stderr, "[ERROR] Failed to enable writing on conn %d\n", c->fd); + close_conn(c); + } else { + c->txoff = false; + } + } goto end; } + setNotify(svc, c); //stop service if(!runStop(arg)) { fprintf(stderr, "[ERROR] Failed to stop service %s: fork/exec error\n", arg);