vmware-detect.c: implemented io-ports based check
[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 *  Latest change: Mit Jun 28 18:13:43 CEST 2006 [mika]
7 *******************************************************************************/
8 // return 0 if it is not running; return 1 if it is running
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14
15 int main(int argc, char *argv[])
16 {
17     struct flock fl = { F_WRLCK, SEEK_SET, 0, 0, 0 };
18     int fd;
19     fl.l_pid = getpid();
20
21     if ((fd = open("/var/lib/dpkg/lock", O_RDWR)) == -1) {
22         exit(1);
23     }
24
25     if (fcntl(fd, F_SETLK, &fl) == -1) {
26         exit(1); // it is running
27     }
28     else {
29         exit(0); // it is not running
30     }
31
32     return 0;
33 }
34
35 /* END OF FILE ****************************************************************/