2 * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
13 #include <system_info.h>
14 #include <util/KMessage.h>
17 extern const char* __progname
;
21 print_usage(bool error
)
23 fprintf(error
? stderr
: stdout
,
24 "Usage: %s [ <teamID> [ <events> ] ]\n"
25 " teamID - The team ID of the team to watch or -1 for all teams\n"
26 " (-1 is the default).\n"
27 " events - A string specifying the events to watch, consisting of:\n"
28 " 'C': team creation\n"
29 " 'D': team deletion\n"
30 " 'c': thread creation\n"
31 " 'd': thread deletion\n"
32 " 'p': thread properties\n"
33 " The default is all events.\n",
39 print_usage_and_exit(bool error
)
47 main(int argc
, const char* const* argv
)
50 uint32 watchEvents
= B_WATCH_SYSTEM_ALL
;
53 print_usage_and_exit(true);
56 const char* arg
= argv
[1];
57 if (strcmp(arg
, "-h") == 0 || strcmp(arg
, "--help") == 0)
58 print_usage_and_exit(false);
60 // first parameter is the team ID
61 watchTeam
= atol(arg
);
64 // second parameter is the events string
67 while (*arg
!= '\0') {
70 watchEvents
|= B_WATCH_SYSTEM_TEAM_CREATION
;
73 watchEvents
|= B_WATCH_SYSTEM_TEAM_DELETION
;
76 watchEvents
|= B_WATCH_SYSTEM_THREAD_CREATION
;
79 watchEvents
|= B_WATCH_SYSTEM_THREAD_DELETION
;
82 watchEvents
|= B_WATCH_SYSTEM_THREAD_PROPERTIES
;
85 print_usage_and_exit(true);
93 port_id port
= create_port(10, "system watching test");
95 fprintf(stderr
, "Failed to create port: %s\n", strerror(port
));
100 status_t error
= __start_watching_system(watchTeam
, watchEvents
, port
, 0);
102 fprintf(stderr
, "Failed to start watching: %s\n", strerror(error
));
106 // receive notifications
108 // read the message from the port
111 ssize_t bytesRead
= read_port(port
, &messageCode
, buffer
,
114 if (bytesRead
== B_INTERRUPTED
)
116 fprintf(stderr
, "Failed to read from port: %s\n",
117 strerror(bytesRead
));
123 error
= message
.SetTo((const void*)buffer
, bytesRead
);
125 fprintf(stderr
, "Failed to create message: %s\n", strerror(error
));
129 message
.Dump((void(*)(const char*,...))printf
);
132 if (message
.What() != B_SYSTEM_OBJECT_UPDATE
)
133 fprintf(stderr
, "Unexpected message what!\n");
137 if (message
.FindInt32("opcode", &opcode
) != B_OK
)
138 fprintf(stderr
, "Failed to get message opcode!\n");
143 printf("B_TEAM_CREATED: ");
145 if (message
.FindInt32("team", &teamID
) == B_OK
)
146 printf("team: %" B_PRId32
"\n", teamID
);
148 printf("no \"team\" field\n");
154 printf("B_TEAM_DELETED: ");
156 if (message
.FindInt32("team", &teamID
) == B_OK
)
157 printf("team: %" B_PRId32
"\n", teamID
);
159 printf("no \"team\" field\n");
165 printf("B_TEAM_EXEC: ");
167 if (message
.FindInt32("team", &teamID
) == B_OK
)
168 printf("team: %" B_PRId32
"\n", teamID
);
170 printf("no \"team\" field\n");
175 case B_THREAD_CREATED
:
177 printf("B_THREAD_CREATED: ");
179 if (message
.FindInt32("thread", &threadID
) == B_OK
)
180 printf("thread: %" B_PRId32
"\n", threadID
);
182 printf("no \"thread\" field\n");
186 case B_THREAD_DELETED
:
188 printf("B_THREAD_DELETED: ");
190 if (message
.FindInt32("thread", &threadID
) == B_OK
)
191 printf("thread: %" B_PRId32
"\n", threadID
);
193 printf("no \"thread\" field\n");
197 case B_THREAD_NAME_CHANGED
:
199 printf("B_THREAD_NAME_CHANGED: ");
201 if (message
.FindInt32("thread", &threadID
) == B_OK
)
202 printf("thread: %" B_PRId32
"\n", threadID
);
204 printf("no \"thread\" field\n");
210 fprintf(stderr
, "Unknown opcode!\n");