#include <sys/queue.h>
#include <sys/select.h>
#include <sys/types.h>
-#include <sys/wait.h>
#include <pthread.h>
#if defined(__OpenBSD__) || defined(__APPLE__)
if (kill(pid, server->sig_code) != 0) {
lwsl_err("kill: %d, errno: %d (%s)\n", pid, errno, strerror(errno));
}
- int status;
- while (waitpid(client->pid, &status, 0) == -1 && errno == EINTR)
- ;
- lwsl_notice("process exited with code %d, pid: %d\n", status, client->pid);
+ pid_t pid_out;
+ int status = wait_proc(client->pid, &pid_out);
+ if (pid_out > 0) {
+ lwsl_notice("process exited with code %d, pid: %d\n", status, pid_out);
+ }
close(client->pty);
cleanup:
lwsl_notice("send ^C to force exit.\n");
}
+void
+sigchld_handler() {
+ pid_t pid;
+ int status = wait_proc(-1, &pid);
+ if (pid > 0) {
+ lwsl_notice("process exited with code %d, pid: %d\n", status, pid);
+ }
+}
+
int
calc_command_start(int argc, char **argv) {
// make a copy of argc and argv
signal(SIGINT, sig_handler); // ^C
signal(SIGTERM, sig_handler); // kill
+ signal(SIGCHLD, sigchld_handler);
context = lws_create_context(&info);
if (context == NULL) {
#include <ctype.h>
#include <string.h>
#include <signal.h>
+#include <sys/errno.h>
+#include <sys/wait.h>
#ifdef __linux__
// https://github.com/karelzak/util-linux/blob/master/misc-utils/kill.c
return atoi(sig_name);
}
+int
+wait_proc(pid_t in, pid_t *out) {
+ int stat = 0, pid;
+ do {
+ pid = waitpid(in, &stat, WNOHANG);
+ } while (pid < 0 && errno == EINTR);
+ if (out != NULL) *out = pid;
+ int status = -1;
+ if (WIFEXITED(stat)) {
+ status = WEXITSTATUS(stat);
+ } else if (WIFSIGNALED(status)) {
+ status = WTERMSIG(stat);
+ }
+ return status;
+}
+
int
open_uri(char *uri) {
#ifdef __APPLE__
int
get_sig(const char *sig_name);
+// waitpid with WNOHANG and return the status
+int
+wait_proc(pid_t in, pid_t *out);
+
// Open uri with the default application of system
int
open_uri(char *uri);