xref: /haiku/src/bin/release.c (revision 4d8811742fa447ec05b4993a16a0931bc29aafab)
1 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
2 //
3 //  Copyright (c) 2004, OpenBeOS
4 //
5 //  This software is part of the OpenBeOS distribution and is covered
6 //  by the MIT License.
7 //
8 //
9 //  File:        release.c
10 //  Author:      Jérôme Duval (korli@chez.com)
11 //  Description: release a semaphore.
12 //
13 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <OS.h>
19 
20 static int usage(char *prog)
21 {
22 	printf("usage: release [-f] [-c count] semaphore_number\n");
23 	printf("warning: this can be hazardous...\n");
24 	exit(1);
25 }
26 
27 
28 int main(int argc, char **argv)
29 {
30 	int count = 1;
31 	bool force = false;
32 	sem_id semid = -1;
33 	status_t err;
34 	team_id teamid = -1;
35 
36 	int i;
37 	if (argc < 2 || argc > 5)
38 		usage("release");
39 	for (i = 1; i < argc; i++) {
40 		if (strncmp(argv[i], "-f", 2) == 0) {
41 			force = true;
42 		} else if (strncmp(argv[i], "-c", 2) == 0) {
43 			i++;
44 			if (i < argc) {
45 				count = strtol(argv[i], NULL, 10);
46 			} else
47 				usage("release");
48 		} else {
49 			semid = strtol(argv[i], NULL, 10);
50 		}
51 	}
52 
53 	if (semid <=0)
54 		usage("release");
55 
56 	if (force) {
57 		sem_info sinfo;
58 		thread_info tinfo;
59 		get_sem_info(semid, &sinfo);
60 		teamid = sinfo.team;
61 		get_thread_info(find_thread(NULL), &tinfo);
62 		set_sem_owner(semid, tinfo.team);
63 	}
64 
65 	printf("releasing semaphore %d\n", (int)semid);
66 	err = release_sem_etc(semid, count, 0);
67 
68 	if (err < B_OK) {
69 		fprintf(stderr, "release_sem failed: %s\n", strerror(err));
70 		return 1;
71 	} else if (force) {
72 		set_sem_owner(semid, teamid);
73 	}
74 
75 	return 0;
76 }
77