/
netlink.c
237 lines (204 loc) · 5.74 KB
/
netlink.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* netlink.c - Kernel ACPI Event Netlink Interface
*
* Handles the details of getting kernel ACPI events from netlink.
*
* Inspired by (and in some cases blatantly lifted from) Zhang Rui's
* acpi_genl and Alexey Kuznetsov's libnetlink. Thanks also to Yi Yang
* at intel.
*
* Copyright (C) 2008, Ted Felix (www.tedfelix.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* (tabs at 4)
*/
/* system */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
/* local */
#include "acpid.h"
#include "event.h"
#include "libnetlink.h"
#include "genetlink.h"
#include "acpi_genetlink.h"
#include "acpi_ids.h"
#include "connection_list.h"
static void
format_netlink(struct nlmsghdr *msg)
{
struct rtattr *tb[ACPI_GENL_ATTR_MAX + 1];
struct genlmsghdr *ghdr = NLMSG_DATA(msg);
int len;
struct rtattr *attrs;
len = msg->nlmsg_len;
/* if this message doesn't have the proper family ID, drop it */
if (msg->nlmsg_type != acpi_ids_getfamily()) {
if (logevents) {
acpid_log(LOG_INFO, "wrong netlink family ID.\n");
}
return;
}
len -= NLMSG_LENGTH(GENL_HDRLEN);
if (len < 0) {
acpid_log(LOG_WARNING,
"wrong netlink controller message len: %d\n", len);
return;
}
attrs = (struct rtattr *)((char *)ghdr + GENL_HDRLEN);
/* parse the attributes in this message */
parse_rtattr(tb, ACPI_GENL_ATTR_MAX, attrs, len);
/* if there's an ACPI event attribute... */
if (tb[ACPI_GENL_ATTR_EVENT]) {
/* get the actual event struct */
struct acpi_genl_event *event =
RTA_DATA(tb[ACPI_GENL_ATTR_EVENT]);
char buf[64];
/* format it */
snprintf(buf, sizeof(buf), "%s %s %08x %08x",
event->device_class, event->bus_id, event->type, event->data);
/* if we're locked, don't process the event */
if (locked()) {
if (logevents) {
acpid_log(LOG_INFO,
"lockfile present, not processing "
"netlink event \"%s\"\n", buf);
}
return;
}
if (logevents)
acpid_log(LOG_INFO,
"received netlink event \"%s\"\n", buf);
/* send the event off to the handler */
acpid_handle_event(buf);
if (logevents)
acpid_log(LOG_INFO,
"completed netlink event \"%s\"\n", buf);
}
}
/* (based on rtnl_listen() in libnetlink.c) */
void
process_netlink(int fd)
{
int status;
struct nlmsghdr *h;
/* the address for recvmsg() */
struct sockaddr_nl nladdr;
/* the io vector for recvmsg() */
struct iovec iov;
/* recvmsg() parameters */
struct msghdr msg = {
.msg_name = &nladdr,
.msg_namelen = sizeof(nladdr),
.msg_iov = &iov,
.msg_iovlen = 1,
};
/* buffer for the incoming data */
char buf[8192];
static int nerrs;
/* set up the netlink address */
memset(&nladdr, 0, sizeof(nladdr));
nladdr.nl_family = AF_NETLINK;
nladdr.nl_pid = 0;
nladdr.nl_groups = 0;
/* set up the I/O vector */
iov.iov_base = buf;
iov.iov_len = sizeof(buf);
/* read the data into the buffer */
status = recvmsg(fd, &msg, 0);
/* if there was a problem, print a message and keep trying */
if (status < 0) {
/* if we were interrupted by a signal, bail */
if (errno == EINTR)
return;
acpid_log(LOG_ERR, "netlink read error: %s (%d)\n",
strerror(errno), errno);
if (++nerrs >= ACPID_MAX_ERRS) {
acpid_log(LOG_ERR,
"too many errors reading via "
"netlink - aborting\n");
exit(EXIT_FAILURE);
}
return;
}
/* if an orderly shutdown has occurred, we're done */
if (status == 0) {
acpid_log(LOG_WARNING, "netlink connection closed\n");
exit(EXIT_FAILURE);
}
/* check to see if the address length has changed */
if (msg.msg_namelen != sizeof(nladdr)) {
acpid_log(LOG_WARNING, "netlink unexpected length: "
"%d expected: %d\n", msg.msg_namelen, sizeof(nladdr));
return;
}
/* for each message received */
for (h = (struct nlmsghdr*)buf; (unsigned)status >= sizeof(*h); ) {
int len = h->nlmsg_len;
int l = len - sizeof(*h);
if (l < 0 || len > status) {
if (msg.msg_flags & MSG_TRUNC) {
acpid_log(LOG_WARNING, "netlink msg truncated (1)\n");
return;
}
acpid_log(LOG_WARNING,
"malformed netlink msg, length %d\n", len);
return;
}
/* format the message */
format_netlink(h);
status -= NLMSG_ALIGN(len);
h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
}
if (msg.msg_flags & MSG_TRUNC) {
acpid_log(LOG_WARNING, "netlink msg truncated (2)\n");
return;
}
if (status) {
acpid_log(LOG_WARNING, "netlink remnant of size %d\n", status);
return;
}
return;
}
/* convert the netlink multicast group number into a bit map */
/* (e.g. 4 => 16, 5 => 32) */
static __u32
nl_mgrp(__u32 group)
{
if (group > 31) {
acpid_log(LOG_ERR, "Unexpected group number %d\n", group);
return 0;
}
return group ? (1 << (group - 1)) : 0;
}
void open_netlink(void)
{
struct rtnl_handle rth;
struct connection c;
/* open the appropriate netlink socket for input */
if (rtnl_open_byproto(
&rth, nl_mgrp(acpi_ids_getgroup()), NETLINK_GENERIC) < 0) {
acpid_log(LOG_ERR, "cannot open generic netlink socket\n");
return;
}
acpid_log(LOG_DEBUG, "netlink opened successfully\n");
/* add a connection to the list */
c.fd = rth.fd;
c.process = process_netlink;
add_connection(&c);
}