-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlafd.c
130 lines (103 loc) · 2.61 KB
/
lafd.c
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
#define DBUS_API_SUBJECT_TO_CHANGE
#include <dbus/dbus.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "laffun.h"
/**
* Connect to the DBUS bus and send a broadcast signal
*/
void send_signal(DBusConnection *conn, char* sigvalue)
{
DBusMessage* msg;
DBusMessageIter args;
dbus_uint32_t serial = 0;
if (DEBUG)
printf("Sending signal with value %s\n", sigvalue);
// create a signal & check for errors
msg = dbus_message_new_signal( "/laf/signal/alert", // object name of the signal
"laf.signal.source", // interface name of the signal
"event"); // name of the signal
if (NULL == msg)
{
fprintf(stderr, "Message Null\n");
exit(1);
}
// append arguments onto signal
dbus_message_iter_init_append(msg, &args);
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &sigvalue)) {
fprintf(stderr, "Out Of Memory!\n");
exit(1);
}
// send the message and flush the connection
if (!dbus_connection_send(conn, msg, &serial)) {
fprintf(stderr, "Out Of Memory!\n");
exit(1);
}
dbus_connection_flush(conn);
if (DEBUG)
printf("Signal Sent\n");
// free the message and close the connection
dbus_message_unref(msg);
}
int main(int argc, char** argv)
{
DBusConnection* conn;
DBusError err;
int ret;
char* buffer;
int nls;
pid_t pid;
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);
if (pid > 0)
exit(EXIT_SUCCESS);
if (setsid() < 0)
exit(EXIT_FAILURE);
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, SIG_IGN);
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);
if (pid > 0)
exit(EXIT_SUCCESS);
// initialise the error value
dbus_error_init(&err);
// connect to the DBUS system bus, and check for errors
conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Connection Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (NULL == conn) {
exit(1);
}
// register our name on the bus, and check for errors
ret = dbus_bus_request_name(conn, "laf.signal.source", DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Name Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
exit(1);
}
/* open netlink socket */
nls = open_netlink();
if (nls < 0)
return nls;
buffer = malloc(MAX_WL_SIZE);
bzero(buffer,MAX_WL_SIZE);
while (1) {
read_event_buf(nls, MSG_WAITALL, buffer, MAX_WL_SIZE);
send_signal(conn, buffer);
bzero(buffer,MAX_WL_SIZE);
}
dbus_connection_close(conn);
free(buffer);
close(nls);
return 0;
}