Skip to content

Commit

Permalink
add daemon stuff ((hopefully) fixes #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
em1lyy committed Jan 3, 2021
1 parent c2ffb3a commit 4581f48
Showing 1 changed file with 67 additions and 118 deletions.
185 changes: 67 additions & 118 deletions kanrisha.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,8 @@ struct servlist get_running_servs() {
}

while((dent = readdir(srcdir)) != NULL) {
struct stat st;

if(strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0)
continue;

if(fstatat(dirfd(srcdir), dent->d_name, &st, 0) < 0) {
perror(dent->d_name);
continue;
}

if(S_ISDIR(st.st_mode)) {
char* pidfname;
if (!(pidfname = malloc(sizeof(char) * (32 + strlen(dent->d_name))))) malloc_fail();

strcpy(pidfname, "/etc/kanrisha.d/available/");
strcat(pidfname, dent->d_name);
strcat(pidfname, "/pid");

if(access(pidfname, F_OK) == -1) {
// servs[dir_count++] = dent->d_name;
strcpy(servslist.services[servslist.servc++], dent->d_name);
}
free(pidfname);
for (int i = 0; i < service_count; i++) {
strcpy(servslist.services[servslist.servc++], services[i]->name);
}
}
closedir(srcdir);
Expand Down Expand Up @@ -235,32 +214,24 @@ int status(char servname[]) {
* the bases of chiyoko before dealing with these minor details.
**/

char* pidfname;
char* logfname;
if (!(pidfname = malloc(sizeof(char) * (32 + strlen(servname))))) malloc_fail();
if (!(logfname = malloc(sizeof(char) * (32 + strlen(servname))))) malloc_fail();

strcpy(pidfname, "/etc/kanrisha.d/available/");
strcat(pidfname, servname);
strcpy(logfname, pidfname);
strcat(pidfname, "/pid");
strcpy(logfname, "/etc/kanrisha.d/available/");
strcat(logfname, servname);
strcat(logfname, "/log");

char status[12] = "not running";
pid_t pid = -1;
if(access(pidfname, F_OK) != -1) {
FILE *pidf;
pidf = fopen(pidfname, "r");
if (pidf == NULL) {
strcat(output, "error: ");
strcat(output, strerror(errno));
strcat(output, "\n");
return -1;
int running = 0, i;
for (i = 0; i < service_count; i++) {
if (!strcmp(services[i]->name, servname)) {
running = 1;
break;
}
}

fscanf(pidf, "%d", &pid);
fclose(pidf);

char status[12] = "not running";
pid_t pid = running ? services[i]->procid : -1;
if(running) {
kill(pid, 0);
if (errno == ESRCH) {
strcpy(status, "dead");
Expand All @@ -275,7 +246,6 @@ int status(char servname[]) {
servname, status, pid);
strcat(output, tempout);

free(pidfname);
free(logfname);

// printing the last lines of a file in C is a fucking nightmare when
Expand Down Expand Up @@ -370,58 +340,55 @@ int start_serv(char servname[]) {
strcat(output, "...\n");

char* fname;
char* pidfname;
char* logfname;
if (!(fname = malloc(sizeof(char) * (32 + strlen(servname))))) malloc_fail();
if (!(pidfname = malloc(sizeof(char) * (32 + strlen(servname))))) malloc_fail();
if (!(pidfname = malloc(sizeof(char) * (32 + strlen(servname))))) malloc_fail();

strcpy(fname, "/etc/kanrisha.d/available/");
strcat(fname, servname);
strcpy(pidfname, fname);
strcpy(logfname, fname);
strcat(fname, "/run");
strcat(pidfname, "/pid");
strcat(logfname, "/log");

for (int i = 0; i < service_count; i++) {
if (!strcmp(services[i]->name, servname)) {
strcat(output, "error: ");
strcat(output, servname);
strcat(output, " is already running\n");
return 1;
}
}

if(access(fname, F_OK|X_OK) != -1) {
if(access(pidfname, F_OK) == -1) {
if(access(fname, F_OK|W_OK) != -1) {
pid_t child_pid = fork();
if (child_pid == 0) {
char *const args[] = { "--run-by-kanrisha", "true", NULL };

int fd;
if((fd = open(logfname, O_CREAT | O_WRONLY | O_TRUNC, LOGFILEPERMS)) < 0){
perror("open");
return -1;
}

dup2(fd, 1);
close(fd);

execvp(fname, args);
sleep(3);
stop_serv(servname);
exit(-1);
} else {
struct service *started_serv = malloc(sizeof(struct service));
started_serv->name = strdup(servname);
started_serv->procid = child_pid;
started_serv->restart_when_dead = 1;
started_serv->restart_times = 0;
started_serv->exited_normally = 0;

services[service_count++] = started_serv;
if(access(fname, F_OK|W_OK) != -1) {
pid_t child_pid = fork();
if (child_pid == 0) {
char *const args[] = { "--run-by-kanrisha", "true", NULL };

int fd;
if((fd = open(logfname, O_CREAT | O_WRONLY | O_TRUNC, LOGFILEPERMS)) < 0){
perror("open");
return -1;
}

dup2(fd, 1);
close(fd);

execvp(fname, args);
sleep(3);
stop_serv(servname);
exit(-1);
} else {
strcat(output, "error: missing permissions\n");
return 1;
struct service *started_serv = malloc(sizeof(struct service));
started_serv->name = strdup(servname);
started_serv->procid = child_pid;
started_serv->restart_when_dead = 1;
started_serv->restart_times = 0;
started_serv->exited_normally = 0;

services[service_count++] = started_serv;
}
} else {
strcat(output, "error: ");
strcat(output, servname);
strcat(output, " is already running\n");
strcat(output, "error: missing permissions\n");
return 1;
}
} else {
Expand All @@ -435,7 +402,6 @@ int start_serv(char servname[]) {
strcat(output, " has been started\n");

free(fname);
free(pidfname);
free(logfname);

return 0;
Expand All @@ -455,24 +421,17 @@ int stop_serv(char servname[]) {
strcat(output, servname);
strcat(output, "...\n");

char* fname;
if (!(fname = malloc(sizeof(char) * (32 + strlen(servname))))) malloc_fail();

strcpy(fname, "/etc/kanrisha.d/available/");
strcat(fname, servname);
strcat(fname, "/pid");

if(access(fname, F_OK|R_OK) != -1) {
FILE *fp;
fp = fopen(fname, "r");

int pid;
fscanf(fp, "%d", &pid);

fclose(fp);
int running = 0, i;
for (i = 0; i < service_count; i++) {
if (!strcmp(services[i]->name, servname)) {
running = 1;
break;
}
}

if(running) {
int needtokillproc = 1;
if(kill((pid_t)pid, SIGTERM) != 0) {
if(kill(services[i]->procid, SIGTERM) != 0) {
if (errno == EPERM) {
strcat(output, "error: missing permissions\n");
return 1;
Expand All @@ -482,7 +441,7 @@ int stop_serv(char servname[]) {
}

for (int i = 0; i < SIGKILLTIMEOUT; i++) {
if(kill((pid_t)pid, SIGTERM) != 0) {
if(kill(services[i]->procid, SIGTERM) != 0) {
if (errno == ESRCH) {
needtokillproc = 0;
break;
Expand All @@ -493,20 +452,13 @@ int stop_serv(char servname[]) {
strcat(output, "service ");
strcat(output, servname);
strcat(output, " won't terminate, killing it\n");
if(kill((pid_t)pid, SIGKILL) != 0) {
if(kill(services[i]->procid, SIGKILL) != 0) {
if (errno == EPERM) {
strcat(output, "error: missing permissions\n");
return 1;
}
}
}

if (unlink(fname) != 0) {
strcat(output, "error: cannot delete pidfile. please remove it manually or problems will occur ( ");
strcat(output, strerror(errno));
strcat(output, " )\n");
return 1;
}
} else {
strcat(output, "error: ");
strcat(output, servname);
Expand All @@ -517,8 +469,6 @@ int stop_serv(char servname[]) {
strcat(output, servname);
strcat(output, " has been stopped\n");

free(fname);

return 0;
}

Expand All @@ -532,14 +482,15 @@ int stop_all() {
}

int restart_serv(char servname[]) {
char* fname;
if (!(fname = malloc(sizeof(char) * (32 + strlen(servname))))) malloc_fail();

strcpy(fname, "/etc/kanrisha.d/available/");
strcat(fname, servname);
strcat(fname, "/pid");
int running = 0, i;
for (i = 0; i < service_count; i++) {
if (!strcmp(services[i]->name, servname)) {
running = 1;
break;
}
}

if(access(fname, F_OK|R_OK) != -1) {
if (running) {
stop_serv(servname);
start_serv(servname);
} else {
Expand All @@ -552,8 +503,6 @@ int restart_serv(char servname[]) {
strcat(output, servname);
strcat(output, " has been restarted\n");

free(fname);

return 0;
}

Expand Down

0 comments on commit 4581f48

Please sign in to comment.