Skip to content

Commit

Permalink
Merge pull request #1432 from eeff/eeff/fix
Browse files Browse the repository at this point in the history
fix: send signal to process group when neuron stopped
  • Loading branch information
fengzeroz authored Jul 31, 2023
2 parents 12a807c + a003e87 commit e6ce4c2
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,31 +138,35 @@ int neuron_already_running()

int neuron_stop()
{
int ret = -1;
int fd = -1;
char buf[16] = { 0 };
fd = open(NEURON_DAEMON_LOCK_FNAME, O_RDONLY);
if (fd < 0) {
int ret = -1;

FILE *fp = fopen(NEURON_DAEMON_LOCK_FNAME, "r");
if (NULL == fp) {
nlog_error("cannot open %s reason: %s\n", NEURON_DAEMON_LOCK_FNAME,
strerror(errno));
return ret;
}
int size = read(fd, buf, sizeof(buf) - 1);
if (size <= 0) {
nlog_error("cannot read %s reason: %s\n", NEURON_DAEMON_LOCK_FNAME,
strerror(errno));
ret = -1;

long pid = -1;
if (1 != fscanf(fp, "%ld", &pid)) {
nlog_error("cannot scan pid from: %s\n", NEURON_DAEMON_LOCK_FNAME);
goto end;
}

pid_t gid = getpgid((pid_t) pid);
if (-1 == gid) {
nlog_error("cannot get gpid reason: %s\n", strerror(errno));
goto end;
}

if (0 == kill((pid_t)(-gid), SIGINT)) {
ret = 0;
} else {
long pid = -1;
if (sscanf(buf, "%ld", &pid) == 1) {
if (kill((pid_t) pid, SIGINT) == -1) {
ret = -1;
} else {
ret = 0;
}
}
nlog_error("cannot kill gpid:%ld reason: %s\n", (long) gid,
strerror(errno));
}
close(fd);

end:
fclose(fp);
return ret;
}

0 comments on commit e6ce4c2

Please sign in to comment.