signals

sigaction passes SIGINT to system call, but not signal

孤街浪徒 提交于 2019-12-01 09:34:31
I have a loop handling an accept(2) call. I want to be able to perform some cleanup when a SIGINT is sent to the program. My first thought was to use the signal function. void signal_handler(int signal) { printf("Caught signal\n"); } int main() { signal(SIGINT, &signal_handler); // ... int accept_fd = accept(sock, NULL, NULL); if (accept_fd == -1) { close(sock); perror("accept"); return 1; } // ... } However, this simply prints "Caught signal" and then the program continues on. If I modify main to use sigaction , the program works as expected. int main() { struct sigaction sa; sa.sa_handler =

Using self-pipe, how can I avoid that the event loop stalls on read()?

大城市里の小女人 提交于 2019-12-01 09:33:09
问题 I am trying to make use of the self-pipe trick to get a portable implementation (across Solaris, MacOSX, Linux, BSDs) of my application. So in addition to the two pipes for stderr and stdout which I am using to fetch the output of the forked child (I use no exec in the child, the child executes the same code as the parent), I have the pipe for signals ( enum {SIG_PIPE, STDOUT_PIPE, STDERR_PIPE, MAX_PIPE} provides the symbolic names). O_NONBLOCK is set on the pipes, prior to calling handle

Exactly which variables need to be sig_atomic_t in the context of signal handling?

非 Y 不嫁゛ 提交于 2019-12-01 08:43:55
问题 Here is a simple toy program that uses volatile sig_atomic_t . #include <stdio.h> #include <signal.h> #include <stdlib.h> #include <unistd.h> #define UNUSED(x) (void) (x) volatile sig_atomic_t quit; void sigusr1_handler(int sig) { UNUSED(sig); write(1, "handler\n", 8); quit = 1; } int main() { struct sigaction sa; sa.sa_handler = sigusr1_handler; sa.sa_flags = 0; sigemptyset(&sa.sa_mask); if (sigaction(SIGUSR1, &sa, NULL) == -1) { perror("sigaction"); return 1; } quit = 0; while (!quit) ;

ContentType matching query does not exist on post_syncdb

天涯浪子 提交于 2019-12-01 08:36:01
I am trying to add add some data to the database as soon as tables are created, using the post_syncdb signal. signals.post_syncdb.connect(init) Then in the init function, I want to set permission, so I use ct = ContentType.objects.get(app_label='news', model='Article') Permission(name='Approve articles', codename='can_approve_article', content_type=ct) But if I drop all the tables and run syncdb , I get ... File "...\base\functions\init.py", line 11, in init ct = ContentType.objects.get(app_label='news', model='Article') ... django.contrib.contenttypes.models.DoesNotExist: ContentType matching

SIGINT handling and getline

て烟熏妆下的殇ゞ 提交于 2019-12-01 07:14:10
问题 I wrote this simple program: void sig_ha(int signum) { cout<<"received SIGINT\n"; } int main() { string name; struct sigaction newact, old; newact.sa_handler = sig_ha; sigemptyset(&newact.sa_mask); newact.sa_flags = 0; sigaction(SIGINT,&newact,&old); for (int i=0;i<5;i++) { cout<<"Enter text: "; getline(cin,name); if (name!="") cout<<"Text entered: "<<name; cout<<endl; } return 0; } If I hit ctrl-c while the program waits for input I get the following output: Enter text: received SIGINT Enter

Use flock() in the sigaction handler

倖福魔咒の 提交于 2019-12-01 06:39:05
flock() is generally async-signal-safe because it is a system call. Its semantics make it hard to implement it differently. It is not in the POSIX's list of async-signal-safe functions because it is not in POSIX at all. Is it possible to use flock() in the sigaction handler without problems? MOHAMED According to @alk answer in the following topic : We can develop our property flock() function (its name could be async_flock() ). we copy in this new function the content of the origin lockf() code and then we make the following changes in order to get an async-signal-safe function: replace _

What is the use of ignoring `SIGCHLD` signal with `sigaction(2)`?

▼魔方 西西 提交于 2019-12-01 05:56:12
It turns out that we can prevent appearing of a zombie process (i.e. the one whose parent doesn't wait() for it to _exit() ) by specifying SIGCHLD signal to be ignored with sigaction() by its parent. However, it seems like SIGCHLD is ignored by default anyway. How come does this work? int main (void) { struct sigaction sa; sa.sa_handler = SIG_IGN; //handle signal by ignoring sigemptyset(&sa.sa_mask); sa.sa_flags = 0; if (sigaction(SIGCHLD, &sa, 0) == -1) { perror(0); exit(1); } int pid = fork(); if (pid == 0) { //child process _exit(0); } do_something(); //parent process return 0; } Barmar The

sem_post, signal handlers, and undefined behavior

纵饮孤独 提交于 2019-12-01 05:20:55
Does this use of sem_post() in a signal handler rely on undefined behavior? /* * excerpted from the 2017-09-15 Linux man page for sem_wait(3) * http://man7.org/linux/man-pages/man3/sem_wait.3.html */ ... sem_t sem; ... static void handler(int sig) { write(STDOUT_FILENO, "sem_post() from handler\n", 24); if (sem_post(&sem) == -1) { write(STDERR_FILENO, "sem_post() failed\n", 18); _exit(EXIT_FAILURE); } } The semaphore sem has static storage duration. While the call to sem_post () is async-signal-safe, the POSIX.1-2008 treatment of signal actions seems to disallow referencing that semaphore

What is the correct way to force an app to core dump and quit?

假装没事ソ 提交于 2019-12-01 05:13:54
I just came across some code which used the kill system call to send a SIGSEGV signal to an app. The rationale behind this was that this would force the app to core dump and quit. This seems so wrong to me, is this normal practice? SIGQUIT is the correct signal to send to a program if you wish to produce a core dump. kill is the correct command line program to send signals (it is of course poorly named, since not all signals will kill the program). Note, you should not send random signals to the program, not all of them will produce a core dump. Many of them will be handled by the program

C++ Sending a simple signal in Windows

只愿长相守 提交于 2019-12-01 04:55:23
问题 is there an equivalent to the function kill() on Windows? int kill(pid_t pid, int sig); If not, would it be possible to test if a process is running based on its PID? Thanks 回答1: Windows doesn't have signals in the unix sense. You can use OpenProcess to check if a process exists - If it succeeds, or fails with an access error, then the process exists. bool processExists(DWORD ProcessID) { HANDLE hProcess = OpenProcess(SYNCHRONIZE, FALSE, ProcessID); if (hProcess != NULL) { CloseHandle