dahdi_cfg: Unlink semaphore on early exit.
If dahdi_cfg is terminated while holding the named semaphore, it is possible to leave it behind and all subsequenct invocations of dahdi_cfg will block waiting for it. Signed-off-by: Shaun Ruffell <sruffell@digium.com> Signed-off-by: Russ Meyerriecks <rmeyerriecks@digium.com>
This commit is contained in:
parent
64e7c688d3
commit
8045f7f493
30
dahdi_cfg.c
30
dahdi_cfg.c
@ -29,6 +29,7 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
|
#include <signal.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -1540,15 +1541,27 @@ static int span_restrict(char *str)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const char *SEM_NAME = "dahdi_cfg";
|
||||||
|
static sem_t *lock = SEM_FAILED;
|
||||||
|
|
||||||
|
static void signal_handler(int signal)
|
||||||
|
{
|
||||||
|
if (SEM_FAILED != lock) {
|
||||||
|
sem_unlink(SEM_NAME);
|
||||||
|
}
|
||||||
|
/* The default handler should have been restored before this handler was
|
||||||
|
* called, so we can let the "normal" processing finish the cleanup. */
|
||||||
|
raise(signal);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int c;
|
int c;
|
||||||
char *buf;
|
char *buf;
|
||||||
char *key, *value;
|
char *key, *value;
|
||||||
int x,found;
|
int x,found;
|
||||||
sem_t *lock = SEM_FAILED;
|
|
||||||
const char *SEM_NAME = "dahdi_cfg";
|
|
||||||
int exit_code = 0;
|
int exit_code = 0;
|
||||||
|
struct sigaction act;
|
||||||
|
|
||||||
while((c = getopt(argc, argv, "fthc:vsd::C:S:")) != -1) {
|
while((c = getopt(argc, argv, "fthc:vsd::C:S:")) != -1) {
|
||||||
switch(c) {
|
switch(c) {
|
||||||
@ -1670,6 +1683,19 @@ finish:
|
|||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sigemptyset(&act.sa_mask);
|
||||||
|
act.sa_handler = signal_handler;
|
||||||
|
act.sa_flags = SA_RESETHAND;
|
||||||
|
|
||||||
|
if (sigaction(SIGTERM, &act, NULL) == -1) {
|
||||||
|
perror("Failed to install SIGTERM handler.");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
if (sigaction(SIGINT, &act, NULL) == -1) {
|
||||||
|
perror("Failed to install SIGINT handler.");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
lock = sem_open(SEM_NAME, O_CREAT, O_RDWR, 1);
|
lock = sem_open(SEM_NAME, O_CREAT, O_RDWR, 1);
|
||||||
if (SEM_FAILED == lock) {
|
if (SEM_FAILED == lock) {
|
||||||
perror("Unable to create 'dahdi_cfg' mutex");
|
perror("Unable to create 'dahdi_cfg' mutex");
|
||||||
|
Loading…
Reference in New Issue
Block a user