Release new version 2.13.0
[grml-scripts.git] / compile / dpkg_not_running.c
1 /* Filename:      dpkg_not_running
2 *  Purpose:       check whether Debian's package management (dpkg) is running
3 *  Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
4 *  Bug-Reports:   see http://grml.org/bugs/
5 *  License:       This file is licensed under the GPL v2.
6 *******************************************************************************/
7 // return 0 if it is not running; return 1 if it is running
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13
14 int main(int argc, char *argv[])
15 {
16     struct flock fl = { F_WRLCK, SEEK_SET, 0, 0, 0 };
17     int fd;
18     fl.l_pid = getpid();
19
20     if ((fd = open("/var/lib/dpkg/lock", O_RDWR)) == -1) {
21         exit(1);
22     }
23
24     if (fcntl(fd, F_SETLK, &fl) == -1) {
25         exit(1); // it is running
26     }
27     else {
28         exit(0); // it is not running
29     }
30
31     return 0;
32 }
33
34 /* END OF FILE ****************************************************************/