File: checklock.c

package info (click to toggle)
base-config 0.33.2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 472 kB
  • ctags: 171
  • sloc: sh: 777; perl: 538; makefile: 47; ansic: 27
file content (35 lines) | stat: -rw-r--r-- 751 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
 * This simple program tries to lock a file the same way dpkg (and
 * apt) do. If it succeeds, it drops the lock and returns 0. If it fails,
 * it returns > 0.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/file.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

static int methlockfd;

int main (int argc, char **argv) {
  	struct flock fl;
  	if (argc < 2) {
	  	fprintf(stderr, "Must specify lock file to check.\n");
	  	exit(100);
	}
  	methlockfd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0660);
  	if (methlockfd == -1) {
	  	exit(2);
	}
  	fl.l_type= F_WRLCK;
  	fl.l_whence= SEEK_SET;
  	fl.l_start= 0;
  	fl.l_len= 0;
	if (fcntl(methlockfd,F_SETLK,&fl) == -1) {
	  	exit(1);
	}
  	exit (0);
}