Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix filesystems not unmounted at shutdown #219

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 55 additions & 5 deletions src/sig.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@

#include <dirent.h>
#include <string.h> /* strerror() */
#include <sched.h>
#include <sys/reboot.h>
#include <sys/wait.h>
#ifdef _LIBITE_LITE
Expand Down Expand Up @@ -172,7 +173,7 @@ void unmount_regular(void);
*
* https://www.freedesktop.org/wiki/Software/systemd/RootStorageDaemons/
*/
void do_kill(int signo)
void do_iterate_proc(int (*callback)(int, int *), int *context)
{
DIR *dirp;

Expand All @@ -191,6 +192,8 @@ void do_kill(int signo)
pid = atoi(d->d_name);
if (!pid)
continue;
if (pid == 1)
continue;

snprintf(file, sizeof(file), "/proc/%s/cmdline", d->d_name);
fp = fopen(file, "r");
Expand All @@ -203,16 +206,62 @@ void do_kill(int signo)
else if (file[0] == '@')
_d("Skipping %s ...", &file[1]);
else
kill(pid, signo);
if (callback(pid, context)) {
print(0, "PID %d is still alive (%s)", pid, file);
break;
}
}
fclose(fp);
}
closedir(dirp);
}
}

static int kill_callback(int pid, int *context)
{
kill(pid, (int)context);
return 0;
}

static int status_callback(int pid, int *context)
{
*context = 1;
return 1;
}

/* return value:
* 1 - at least one process remaining
* 0 - no processes remaining
*/
static int do_wait(int secs)
{
int has_proc;
int tmo = 250000;
int iterations = secs*1000*1000/tmo;

do {
do_usleep(tmo);
while (waitpid(-1, NULL, WNOHANG) > 0)
;
has_proc = 0;
iterations--;
do_iterate_proc(status_callback, &has_proc);
}
while (has_proc && iterations > 0);

return has_proc;
}

void do_shutdown(shutop_t op)
{
struct sched_param sched_param = { .sched_priority = 99 };

/*
* On a PREEMPT-RT system, Finit must run as the highest prioritized
* RT process to ensure it completes the shutdown sequence.
*/
sched_setscheduler(1, SCHED_RR, &sched_param);

if (sdown)
run_interactive(sdown, "Calling shutdown hook: %s", sdown);

Expand All @@ -223,9 +272,10 @@ void do_shutdown(shutop_t op)
* Tell remaining non-monitored processes to exit, give them
* time to exit gracefully, 2 sec was customary, we go for 1.
*/
do_kill(SIGTERM);
do_sleep(1);
do_kill(SIGKILL);
do_iterate_proc(kill_callback, (int*)SIGTERM);
if (do_wait(1)) {
do_iterate_proc(kill_callback, (int*)SIGKILL);
}

/* Exit plugins and API gracefully */
plugin_exit();
Expand Down
14 changes: 14 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,20 @@ void do_sleep(unsigned int sec)
;
}

void do_usleep(unsigned int usec)
{
struct timespec deadline;

clock_gettime(CLOCK_MONOTONIC, &deadline);
troglobit marked this conversation as resolved.
Show resolved Hide resolved

deadline.tv_nsec += usec * 1000;
deadline.tv_sec += deadline.tv_nsec / 1000000000;
deadline.tv_nsec = deadline.tv_nsec % 1000000000;

while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &deadline, NULL) != 0 && errno == EINTR)
;
}

/* Seconds since boot, from sysinfo() */
long jiffies(void)
{
Expand Down
1 change: 1 addition & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ char *sig2str (int sig);
char *code2str (int code);

void do_sleep (unsigned int sec);
void do_usleep (unsigned int usec);
long jiffies (void);
char *uptime (long secs, char *buf, size_t len);
char *memsz (uint64_t sz, char *buf, size_t len);
Expand Down