1 // $Id: util-lockfile.c 2678 2008-02-16 22:09:30Z dhozac $ --*- c -*--
3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "util-lockfile.h"
33 static volatile sig_atomic_t alarm_flag = 0;
36 alarmFunc(int UNUSED sig)
39 signal(SIGALRM, alarmFunc);
43 lockfile(int *fd, char const *filename, int op, long timeout,
44 struct ErrorInformation *err)
46 char const *errstr = 0;
47 void (*old_sighandler)(int) = 0;
50 *fd = open(filename, O_CREAT|O_RDONLY|O_NOFOLLOW|O_NONBLOCK, 0644);
51 if (*fd==-1) goto err;
54 errstr = "siginterrupt()";
55 if (siginterrupt(SIGALRM, 1)==-1) goto err;
58 old_sighandler = signal(SIGALRM, alarmFunc);
59 if (old_sighandler==SIG_ERR) goto err;
65 while (lockf(*fd, op, 0)==-1) {
66 if ((errno!=EINTR && errno!=EINTR) || alarm_flag) goto err;
69 if (timeout!=-1 && old_sighandler!=0)
70 signal(SIGALRM, old_sighandler);
73 if (fcntl(*fd, F_SETFD, FD_CLOEXEC)==-1) goto err;
82 if (timeout!=-1 && old_sighandler!=0)
83 signal(SIGALRM, old_sighandler);
84 if (*fd!=-1) close(*fd);