Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a function async-signal-safe?

I have the following sigaction handler function

void signal_term_handler(int sig)
{
    printf("EXIT :TERM signal Received!\n");
    int rc = flock(pid_file, LOCK_UN | LOCK_NB);
    if(rc) {
        char *piderr = "PID file unlock failed!";
        fprintf(stderr, "%s\n", piderr);
        printf(piderr);
    }
    abort();
}

Someone told me that flock and printf aren't async-signal-safe. And I could not find an alternate async-signal-safe function for flockin this list.

and according to the above link:

when a signal interrupts an unsafe function and the signal-catching function calls an unsafe function, the behavior is undefined

Is there a way to make flock async-signal-safe? Or is there another solution to execute flock when I receive TERM signal?

like image 831
MOHAMED Avatar asked Jun 06 '13 16:06

MOHAMED


2 Answers

You can use fcntl() as an alternative to flock().

like image 183
grasbueschel Avatar answered Oct 06 '22 23:10

grasbueschel


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 POSIX's list of async-signal-safe functions because it is not in POSIX at all.

You likely do not need the explicit unlock because flock() locks are released automatically when all file descriptors referring to the open file description are closed.

The printf() and fprintf() calls should be replaced with appropriate write() calls. The stdio functions are not in the list of async-signal-safe functions and are often strongly async-signal-unsafe.

The abort() call is probably best replaced by setting the signal to the default action and resending it to self; this way, shells know that your program exited because of the signal and can abort command sequences when appropriate.

like image 33
jilles Avatar answered Oct 06 '22 23:10

jilles