Skip to content

Commit d8d97f9

Browse files
committed
Timeout when setting semaphore in atop (issue #207)
1 parent e1a5055 commit d8d97f9

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

acctproc.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#define _FILE_OFFSET_BITS 64
2727

28+
#define _GNU_SOURCE
2829
#include <sys/types.h>
2930
#include <stdio.h>
3031
#include <fcntl.h>
@@ -34,9 +35,9 @@
3435
#include <unistd.h>
3536
#include <sys/time.h>
3637
#include <sys/stat.h>
38+
#include <sys/wait.h>
3739
#include <sys/ipc.h>
3840
#include <sys/sem.h>
39-
#include <sys/wait.h>
4041

4142
#include "atop.h"
4243
#include "photoproc.h"
@@ -101,7 +102,8 @@ struct sembuf semclaim = {0, -1, SEM_UNDO},
101102
semdecre = {1, -1, SEM_UNDO},
102103
semincre = {1, +1, SEM_UNDO};
103104

104-
struct sembuf semreglock[] = {{0, -1, SEM_UNDO}, {1, -1, SEM_UNDO}},
105+
struct sembuf semreglock[] = {{0, -1, SEM_UNDO|IPC_NOWAIT},
106+
{1, -1, SEM_UNDO|IPC_NOWAIT}},
105107
semunlock = {1, +1, SEM_UNDO};
106108

107109
/*
@@ -174,11 +176,17 @@ acctswon(void)
174176
FILE *cfp;
175177
char shadowpath[128];
176178
struct flock flock;
179+
struct timespec maxsemwait = {3, 0};
177180

178181
if (! droprootprivs() )
179182
mcleanstop(42, "failed to drop root privs\n");
180183

181-
(void) semop(sempacctpubid, semreglock, 2);
184+
if (semtimedop(sempacctpubid, semreglock, 2, &maxsemwait) == -1)
185+
{
186+
acctfd = -1;
187+
regainrootprivs();
188+
return 3;
189+
}
182190

183191
snprintf(shadowpath, sizeof shadowpath, "%s/%s/%s",
184192
pacctdir, PACCTSHADOWD, PACCTSHADOWC);

0 commit comments

Comments
 (0)