File: locking.c

package info (click to toggle)
gammu 1.28.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 21,972 kB
  • ctags: 8,504
  • sloc: ansic: 104,965; pascal: 7,209; cpp: 4,023; python: 3,639; php: 1,622; sh: 1,534; sql: 450; cs: 260; makefile: 192; perl: 107; asm: 31
file content (80 lines) | stat: -rw-r--r-- 2,047 bytes parent folder | download | duplicates (8)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <gammu.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "../libgammu/device/devfunc.h"

#include "common.h"

#define lock_path 	"/var/lock/LCK.."
#define TEST_DEVICE "/dev/foo/bar"
#define TEST_LOCK lock_path "bar"

void create_lock(const char *name, const void *lock_data, const size_t lock_data_len)
{
	FILE *fd;
	fd = fopen(name, "w");
	test_result(fd != NULL);
	test_result(fwrite(lock_data, 1, lock_data_len, fd) == lock_data_len);
	test_result(fclose(fd) == 0);
}

int main(int argc UNUSED, char **argv UNUSED)
{
	GSM_Debug_Info *debug_info;
	char *lock = NULL;
	int pid;
	char pids[20];

	pid = getpid();
	sprintf(pids, "%d", pid);

	debug_info = GSM_GetGlobalDebug();
	GSM_SetDebugFileDescriptor(stderr, FALSE, debug_info);
	GSM_SetDebugLevel("textall", debug_info);

	/* Non existing PID, ASCII */
	create_lock(TEST_LOCK, "1234567890", 10);
	test_result(lock_device(NULL, TEST_DEVICE, &lock) == ERR_NONE);
	test_result(lock != NULL);
	test_result(unlock_device(NULL, &lock) == TRUE);

	unlink(TEST_LOCK);

	/* Existing PID, ASCII */
	create_lock(TEST_LOCK, pids, strlen(pids));
	test_result(lock_device(NULL, TEST_DEVICE, &lock) == ERR_DEVICELOCKED);
	test_result(lock == NULL);
	test_result(unlock_device(NULL, &lock) == FALSE);

	unlink(TEST_LOCK);

	/* Existing PID, binary */
	create_lock(TEST_LOCK, &pid, sizeof(int));
	test_result(lock_device(NULL, TEST_DEVICE, &lock) == ERR_DEVICELOCKED);
	test_result(lock == NULL);
	test_result(unlock_device(NULL, &lock) == FALSE);

	unlink(TEST_LOCK);

	/* Existing PID, binary */
	pid = 0xfffffff;
	create_lock(TEST_LOCK, &pid, sizeof(int));
	test_result(lock_device(NULL, TEST_DEVICE, &lock) == ERR_NONE);
	test_result(lock != NULL);
	test_result(unlock_device(NULL, &lock) == TRUE);

	unlink(TEST_LOCK);

	/* No existing lock */
	test_result(lock_device(NULL, TEST_DEVICE, &lock) == ERR_NONE);
	test_result(lock != NULL);
	test_result(unlock_device(NULL, &lock) == TRUE);

	return 0;
}

/* Editor configuration
 * vim: noexpandtab sw=8 ts=8 sts=8 tw=72:
 */