Skip to content

Instantly share code, notes, and snippets.

@yuikns
Created February 13, 2018 21:34
Show Gist options
  • Save yuikns/6ae8aaec8f1c89e1ee6eb87e36a8639a to your computer and use it in GitHub Desktop.
Save yuikns/6ae8aaec8f1c89e1ee6eb87e36a8639a to your computer and use it in GitHub Desktop.
#include "argcv/c/sys/signal.h"
#include <sys/types.h> // pid_t
#include <stdio.h> // printf
#include <signal.h> // signal
#include <sys/wait.h> // wait
sigfunc* _signal(int signo, sigfunc* func) {
struct sigaction act; // bits/sigaction.h
struct sigaction oact;
printf("signal called ...\n");
act.sa_handler = func;
sigemptyset(&act.sa_mask); // signal.h
act.sa_flags = 0;
if (signo == SIGALRM) {
#ifdef SA_INTERRUPT
act.sa_flags |= SA_INTERRUPT; // SunOS 4.x
#endif // SA_INTERRUPT
} else {
#ifdef SA_RESTART
act.sa_flags |= SA_RESTART; // SVR4, 4.4BSD
#endif // SA_RESTART
}
if (sigaction(signo, &act, &oact) < 0) return SIG_ERR;
return oact.sa_handler;
}
/**
*
*/
void sig_chld(int signo) {
pid_t pid;
int stat;
while ((pid = waitpid(-1, &stat, WNOHANG)) > 0) {
// #ifdef __DEBUG__
// if (daemon_proc) {
// syslog(LOG_DEBUG, "child %d terminated \n", pid);
// } else {
// fprintf(stdout, "child %d terminated \n", pid);
// }
// #endif // __DEBUG__
}
return;
}
#ifndef ARGCV_C_SYS_SIGNAL_H_
#define ARGCV_C_SYS_SIGNAL_H_
#ifdef __cplusplus
extern "C" {
#endif //
// ref P. 105 , unix network programming
// ref : https://docs.oracle.com/cd/E19455-01/806-4750/signals-7/index.html
typedef void sigfunc(int);
sigfunc* _signal(int signo, sigfunc* func);
void sig_chld(int signo);
#ifdef __cplusplus
}
#endif //
#endif // ARGCV_C_SYS_SIGNAL_H_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment