File: util_lockfile_example.c

package info (click to toggle)
s390-tools 2.35.0-2
  • links: PTS
  • area: main
  • in suites: forky
  • size: 12,248 kB
  • sloc: ansic: 184,236; sh: 12,152; cpp: 4,954; makefile: 2,763; perl: 2,519; asm: 1,085; python: 697; xml: 29
file content (232 lines) | stat: -rw-r--r-- 5,800 bytes parent folder | download | duplicates (3)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
 * util_lockfile_example - Example program for util_lockfile
 *
 * Copyright IBM Corp. 2022
 *
 * s390-tools is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

//! [code]
#include <stdio.h>
#include <stdlib.h>
#include "lib/util_opt.h"
#include "lib/util_prg.h"
#include "lib/util_lockfile.h"

static const struct util_prg prg = {
	.desc = "Example for util_lockfile.",
	.copyright_vec = { {
			.owner = "IBM Corp.",
			.pub_first = 2022,
			.pub_last = 2022,
		},
		UTIL_PRG_COPYRIGHT_END }
};

static struct util_opt opt_vec[] = {
	UTIL_OPT_SECTION("OPTIONS"),
	{
		.option = { "file", required_argument, NULL, 'f' },
		.argument = "PATH",
		.desc = "Use the specified path for a lockfile.",
	},
	{
		.option = { "lock", required_argument, NULL, 'l' },
		.argument = "RETRIES",
		.desc = "Acquire the specified file lock using the parent "
			"process id of this process.  If not immediately "
			"successful, retry for the specified number of times",
	},
	{
		.option = { "release", 0, NULL, 'r' },
		.desc = "Release the specified lock file using the parent "
			"process id",
	},
	{
		.option = { "lock-and-release", required_argument, NULL, 'L' },
		.argument = "RETRIES",
		.desc = "Acquire the specified file lock using this process "
			"id, then release it.  If not immediately successful, "
			"retry for the specified number of times.",
	},
	UTIL_OPT_HELP,
	UTIL_OPT_VERSION,
	UTIL_OPT_END
};

enum prg_action {
	ACTION_NONE = 0,
	ACTION_LOCK,
	ACTION_RELEASE,
	ACTION_LOCK_AND_RELEASE,
};

static void print_single_action_error(void)
{
	warnx("Only a single action (--lock, --lock-parent, --release, "
	      "--release-parent) is allowed");
}

int main(int argc, char *argv[])
{
	enum prg_action action_id = ACTION_NONE;
	int opt, rc, retries = 0;
	char *endp, *path = NULL;

	util_prg_init(&prg);
	util_opt_init(opt_vec, NULL);

	while (1) {
		opt = util_opt_getopt_long(argc, argv);
		if (opt == -1)
			break;
		switch (opt) {
		case 'f':
			path = optarg;
			break;
		case 'L':
			if (action_id != ACTION_NONE) {
				print_single_action_error();
				return EXIT_FAILURE;
			}
			retries = strtol(optarg, &endp, 0);
			if (*optarg == '\0' || *endp != '\0' || retries < 0) {
				warnx("Invalid retry value for "
				      "--lock-and-release: '%s'",
				      optarg);
				util_prg_print_parse_error();
				return EXIT_FAILURE;
			}
			action_id = ACTION_LOCK_AND_RELEASE;
			break;
		case 'l':
			if (action_id != ACTION_NONE) {
				print_single_action_error();
				return EXIT_FAILURE;
			}
			retries = strtol(optarg, &endp, 0);
			if (*optarg == '\0' || *endp != '\0' || retries < 0) {
				warnx("Invalid retry value for "
				      "--parent-lock: '%s'",
				      optarg);
				util_prg_print_parse_error();
				return EXIT_FAILURE;
			}
			action_id = ACTION_LOCK;
			break;
		case 'r':
			if (action_id != ACTION_NONE) {
				print_single_action_error();
				return EXIT_FAILURE;
			}
			action_id = ACTION_RELEASE;
			break;
		case 'h':
			util_prg_print_help();
			util_opt_print_help();
			exit(EXIT_SUCCESS);
		case 'v':
			util_prg_print_version();
			exit(EXIT_SUCCESS);
		default:
			util_opt_print_parse_error(opt, argv);
			return EXIT_FAILURE;
		}
	}

	if (!path) {
		warnx("--file is required, see --help for more information");
		return EXIT_FAILURE;
	}

	if (action_id == ACTION_NONE) {
		warnx("One of the following actions must be specified: "
		      "--lock, --release, --lock-and-release");
		return EXIT_FAILURE;
	}

	/* Determine which util_lockfile function to call */
	switch (action_id) {
	case ACTION_LOCK:
		rc = util_lockfile_parent_lock(path, retries);
		switch (rc) {
		case UTIL_LOCKFILE_OK:
			printf("lock acquired successfully\n");
			break;
		case UTIL_LOCKFILE_LOCK_FAIL:
			warnx("lock was not acquired; already held");
			return EXIT_FAILURE;
		case UTIL_LOCKFILE_ERR:
			warnx("lock was not acquired; file error");
			return EXIT_FAILURE;
		default:
			warnx("Unknown util_lockfile rc %d", rc);
			return EXIT_FAILURE;
		}
		break;
	case ACTION_RELEASE:
		rc = util_lockfile_parent_release(path);
		switch (rc) {
		case UTIL_LOCKFILE_OK:
			printf("lock released successfully\n");
			break;
		case UTIL_LOCKFILE_RELEASE_NONE:
			warnx("lock file did not exist");
			return EXIT_FAILURE;
		case UTIL_LOCKFILE_RELEASE_FAIL:
			warnx("lock was not held by the specified pid");
			return EXIT_FAILURE;
		case UTIL_LOCKFILE_ERR:
			warnx("lock was not released; file error");
			return EXIT_FAILURE;
		default:
			warnx("Unknown util_lockfile rc %d", rc);
			return EXIT_FAILURE;
		}
		break;
	case ACTION_LOCK_AND_RELEASE:
		rc = util_lockfile_lock(path, retries);
		switch (rc) {
		case UTIL_LOCKFILE_OK:
			printf("lock acquired successfully, holding for 2 seconds\n");
			break;
		case UTIL_LOCKFILE_LOCK_FAIL:
			warnx("lock was not acquired; already held");
			return EXIT_FAILURE;
		case UTIL_LOCKFILE_ERR:
			warnx("lock was not acquired; file error");
			return EXIT_FAILURE;
		default:
			warnx("Unknown util_lockfile rc %d", rc);
			return EXIT_FAILURE;
		}
		/* Briefly sleep while holding the lock */
		sleep(2);
		rc = util_lockfile_release(path);
		switch (rc) {
		case UTIL_LOCKFILE_OK:
			printf("lock released successfully\n");
			break;
		case UTIL_LOCKFILE_RELEASE_NONE:
			warnx("lock file did not exist");
			return EXIT_FAILURE;
		case UTIL_LOCKFILE_RELEASE_FAIL:
			warnx("lock was not held by the specified pid");
			return EXIT_FAILURE;
		case UTIL_LOCKFILE_ERR:
			warnx("lock was not released; file error");
			return EXIT_FAILURE;
		default:
			warnx("Unknown util_lockfile rc %d", rc);
			return EXIT_FAILURE;
		}
		break;
	default:
		warnx("Unknown util_lockfile action");
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}
//! [code]