xref: /haiku/src/bin/listsem.c (revision 4c8e85b316c35a9161f5a1c50ad70bc91c83a76f)
1 /*
2  *	listsem.c
3  *
4  *	Lists all semaphores in all Teams.
5  *	by O.Siebenmarck.
6  *
7  *      04-27-2002 - mmu_man
8  *       added command line args
9  *
10  *	Legal stuff follows:
11 
12 	Copyright (c) 2002 Oliver Siebenmarck <olli@ithome.de>, Haiku project
13 
14 	Permission is hereby granted, free of charge, to any person obtaining a copy of
15 	this software and associated documentation files (the "Software"), to deal in
16 	the Software without restriction, including without limitation the rights to
17 	use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
18 	of the Software, and to permit persons to whom the Software is furnished to do
19 	so, subject to the following conditions:
20 
21 	The above copyright notice and this permission notice shall be included in all
22 	copies or substantial portions of the Software.
23 
24 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 	THE SOFTWARE.
31 
32 */
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <OS.h>
38 
39 
40 static void print_sem_info(sem_info *info)
41 {
42 	printf("%7" B_PRId32 "%31s%7" B_PRId32 "\n", info->sem, info->name,
43 		info->count);
44 }
45 
46 
47 static void print_header(team_info *tinfo)
48 {
49 	if (tinfo != NULL)
50 		printf("TEAM  %" B_PRId32 " (%s):\n", tinfo->team, tinfo->args);
51 
52 	printf("     ID                           name  count\n");
53 	printf("---------------------------------------------\n");
54 }
55 
56 
57 static void list_sems(team_info *tinfo)
58 {
59 	sem_info info;
60 	int32 cookie = 0;
61 
62 	print_header(tinfo);
63 
64 	while (get_next_sem_info(tinfo->team, &cookie, &info) == B_OK)
65 		print_sem_info(&info);
66 
67 	printf("\n");
68 }
69 
70 int main(int argc, char **argv)
71 {
72 	team_info tinfo;
73 	int32 cookie = 0;
74 	int32 i;
75 	system_info sysinfo;
76 
77 	// show up some stats first...
78 	get_system_info(&sysinfo);
79 	printf("sem: total: %5" B_PRIu32 ", used: %5" B_PRIu32 ", left: %5" B_PRIu32
80 		"\n\n", sysinfo.max_sems, sysinfo.used_sems,
81 		sysinfo.max_sems - sysinfo.used_sems);
82 
83 	if (argc == 1) {
84 		while (get_next_team_info( &cookie, &tinfo) == B_OK)
85 			list_sems(&tinfo);
86 
87 		return 0;
88 	}
89 
90 	for (i = 1; i < argc; i++) {
91 		if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
92 			fprintf(stderr, "Usage:  %s [-s semid]  [teamid]\n", argv[0]);
93 			fputs("        List the semaphores allocated by the specified\n",
94 				stderr);
95 			fputs("        team, or all teams if none is specified.\n",
96 				stderr);
97 			fputs("\n", stderr);
98 			fputs("        The -s option displays the sem_info data for a\n",
99 				stderr);
100 			fputs("        specified semaphore.\n", stderr);
101 			return 0;
102 		} else if (strcmp(argv[i], "-s") == 0) {
103 			if (argc < i + 2)
104 				printf("-s used without associated sem id\n");
105 			else {
106 				sem_id id = atoi(argv[i + 1]);
107 				sem_info info;
108 				if (get_sem_info(id, &info) == B_OK) {
109 					print_header(NULL);
110 					print_sem_info(&info);
111 				} else
112 					printf("semaphore %" B_PRId32 " unknown\n\n", id);
113 
114 				i++;
115 			}
116 		} else {
117 			team_id team;
118 			team = atoi(argv[i]);
119 			if (get_team_info(team, &tinfo) == B_OK)
120 				list_sems(&tinfo);
121 			else
122 				printf("team %" B_PRId32 " unknown\n\n", team);
123 		}
124 	}
125 
126 	return 0;
127 }
128 
129