Write the process id in the pid file immediately after the 464xlat pid file has
been openend. Before the process id was written when the parent process exits
leaving a window where no valid process id was in place in the 464xlat pid file.
This lead to issues if the 464xlat utility was launched to terminate a running
464xlat utility as it could possibly terminate a random process.
If the parent process exits the pid file is updated with the process id of the
forked 464xlat utility.
Also rework the signal handling of SIGTERM so the running 464xlat utility is
correctly terminated.
Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
/* 464xlatcfg.c
*
* Copyright (c) 2015 Steven Barth <cyrus@openwrt.org>
/* 464xlatcfg.c
*
* Copyright (c) 2015 Steven Barth <cyrus@openwrt.org>
+ * Copyright (c) 2017 Hans Dedecker <dedeckeh@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
snprintf(buf, sizeof(buf), "/var/run/%s.pid", argv[1]);
FILE *fp = fopen(buf, "r");
if (fp) {
snprintf(buf, sizeof(buf), "/var/run/%s.pid", argv[1]);
FILE *fp = fopen(buf, "r");
if (fp) {
- fscanf(fp, "%d", &pid);
- kill(pid, SIGTERM);
+ if (fscanf(fp, "%d", &pid) == 1)
+ kill(pid, SIGTERM);
+
unlink(buf);
fclose(fp);
}
unlink(buf);
fclose(fp);
}
if (!argv[3] || !argv[4] || !(fp = fopen(buf, "wx")))
return 1;
if (!argv[3] || !argv[4] || !(fp = fopen(buf, "wx")))
return 1;
- signal(SIGTERM, sighandler);
+ signal(SIGTERM, SIG_DFL);
+ setvbuf(fp, NULL, _IOLBF, 0);
+ fprintf(fp, "%d\n", getpid());
prefix[sizeof(prefix) - 1] = 0;
strncpy(prefix, argv[3], sizeof(prefix) - 1);
prefix[sizeof(prefix) - 1] = 0;
strncpy(prefix, argv[3], sizeof(prefix) - 1);
fclose(stderr);
chdir("/");
setsid();
fclose(stderr);
chdir("/");
setsid();
+ signal(SIGTERM, sighandler);
pause();
nat46 = fopen("/proc/net/nat46/control", "w");
pause();
nat46 = fopen("/proc/net/nat46/control", "w");
fclose(nat46);
}
} else {
fclose(nat46);
}
} else {
fprintf(fp, "%d\n", pid);
}
fprintf(fp, "%d\n", pid);
}