aboutsummaryrefslogtreecommitdiff
path: root/alarm-tools/alarms-show.c
blob: 938401e329d9c08deaca0f77f7b3ea226253ff9b (plain)
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
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/un.h>

#include "alarmd_proto.h"

int dump_alarms(int sock)
{
	ssize_t i = 0;
	ssize_t nread = 0;
	size_t length = 0;
	uint32_t count = 0;
	uint32_t packet_type = 0;
	uint8_t is_raised = 0;
	char buffer[128];
	pid_t owner = 0;

	packet_type = ALARMD_PACKET_TYPE_QUERY;

	if (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type)) {
		perror("send");
		return 1;
	}

	nread = recv(sock, &count, sizeof(count), 0);
	if (nread < 0) {
		perror("recv");
		return 1;
	} else if (nread < sizeof(count)) {
		fprintf(stderr, "Alarm count size too small\n");
		return 1;
	}

	printf("Alarms\n"
	       "------\n");

	for (i = 0; i < count; i++) {
		if ((length = recv_string(sock, &buffer)) < 0) {
			perror("recv");
			break;
		}
		if (recv(sock, &is_raised, sizeof(is_raised), 0) != sizeof(is_raised)) {
			perror("recv");
			break;
		}
		if (recv(sock, &owner, sizeof(owner), 0) != sizeof(owner)) {
			perror("recv");
			break;
		}
		printf("[%s] [pid %d] %s\n", is_raised ? "\x1b[1;31mRAISE\x1b[0m" : "\x1b[1;32mCLEAR\x1b[0m", owner, buffer);
	}

	if (count == 0) {
		printf("No alarms registered.\n");
	}

	if (i < count) {
		fprintf(stderr, "Not all alarms received, output must be missing some\n");
		return 1;
	}

	printf("\n");

	return 0;
}

int main(int argc, char **argv)
{
	int sock = 0;
	struct sockaddr_un server;

	if (argc != 2) {
		fprintf(stderr, "Syntax: %s socket_name\n", argv[0]);
		return 1;
	}

	if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
		perror("socket");
		return 1;
	}

	server.sun_family = AF_UNIX;
	strcpy(server.sun_path, argv[1]);
	printf("Connecting...\n");
	if (connect(sock, (struct sockaddr *)&server, strlen(server.sun_path) + sizeof(server.sun_family)) < 0) {
		close(sock);
		perror("connect");
		return 1;
	}
	printf("Connected.\n");

	dump_alarms(sock);
	close(sock);
}