Skip to content

Commit

Permalink
add support for stopping services
Browse files Browse the repository at this point in the history
  • Loading branch information
niaow committed Dec 31, 2017
1 parent 7fee949 commit 413384f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/inst.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ 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
$(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)
Expand Down
4 changes: 4 additions & 0 deletions src/linit-stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

echo $1 $LINITSOCK
/etc/init.d/$1 stop && linitctl state $1 stopped
44 changes: 44 additions & 0 deletions src/linitctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<stdbool.h>
#include<sys/socket.h>
#include<sys/un.h>

Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions src/linitd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 413384f

Please sign in to comment.