summaryrefslogtreecommitdiffstats
path: root/rfkill.c
diff options
context:
space:
mode:
authorJohannes Berg2009-07-05 14:33:25 +0200
committerJohannes Berg2009-07-05 14:33:25 +0200
commit51da1093e715266a8bb6be4f881c8be654053da8 (patch)
tree44594f456cec3bb1d959a29b93afb0d2413f7971 /rfkill.c
parentopen device read-only for list and event operations (diff)
downloadkernel-qcow2-util-linux-51da1093e715266a8bb6be4f881c8be654053da8.tar.gz
kernel-qcow2-util-linux-51da1093e715266a8bb6be4f881c8be654053da8.tar.xz
kernel-qcow2-util-linux-51da1093e715266a8bb6be4f881c8be654053da8.zip
use right event size for read()
In order to be compatible with future size increases of the kernel's rfkill structure, userspace should only read as much as it expects -- the kernel will truncate the event read if necessary, which is the way we plan to have compatibility between different versions, should they ever be necessary. Thus, the userspace tool needs to use the exact event size for a read(). If the userspace tool is ever compiled with a newer kernel then it will need to be adjusted to work with older kernels, however.
Diffstat (limited to 'rfkill.c')
-rw-r--r--rfkill.c30
1 files changed, 14 insertions, 16 deletions
diff --git a/rfkill.c b/rfkill.c
index a7f5f64b0..6b29144b1 100644
--- a/rfkill.c
+++ b/rfkill.c
@@ -18,8 +18,7 @@
static void rfkill_event(void)
{
- unsigned char buf[32];
- struct rfkill_event *event = (void *) buf;
+ struct rfkill_event event;
struct pollfd p;
ssize_t len;
int fd, n;
@@ -44,20 +43,20 @@ static void rfkill_event(void)
if (n == 0)
continue;
- len = read(fd, buf, sizeof(buf));
+ len = read(fd, &event, sizeof(event));
if (len < 0) {
perror("Reading of RFKILL events failed");
break;
}
- if (len != sizeof(struct rfkill_event)) {
+ if (len != sizeof(event)) {
fprintf(stderr, "Wrong size of RFKILL event\n");
continue;
}
printf("RFKILL event: idx %u type %u op %u soft %u hard %u\n",
- event->idx, event->type, event->op,
- event->soft, event->hard);
+ event.idx, event.type, event.op,
+ event.soft, event.hard);
}
close(fd);
@@ -113,8 +112,7 @@ static const char *type2string(enum rfkill_type type)
static void rfkill_list(void)
{
- unsigned char buf[32];
- struct rfkill_event *event = (void *) buf;
+ struct rfkill_event event;
const char *name;
ssize_t len;
int fd;
@@ -131,7 +129,7 @@ static void rfkill_list(void)
}
while (1) {
- len = read(fd, buf, sizeof(buf));
+ len = read(fd, &event, sizeof(event));
if (len < 0) {
if (errno == EAGAIN)
break;
@@ -139,20 +137,20 @@ static void rfkill_list(void)
break;
}
- if (len != sizeof(struct rfkill_event)) {
+ if (len != sizeof(event)) {
fprintf(stderr, "Wrong size of RFKILL event\n");
continue;
}
- if (event->op != RFKILL_OP_ADD)
+ if (event.op != RFKILL_OP_ADD)
continue;
- name = get_name(event->idx);
+ name = get_name(event.idx);
- printf("%u: %s: %s\n", event->idx, name,
- type2string(event->type));
- printf("\tSoft blocked: %s\n", event->soft ? "yes" : "no");
- printf("\tHard blocked: %s\n", event->hard ? "yes" : "no");
+ printf("%u: %s: %s\n", event.idx, name,
+ type2string(event.type));
+ printf("\tSoft blocked: %s\n", event.soft ? "yes" : "no");
+ printf("\tHard blocked: %s\n", event.hard ? "yes" : "no");
}
close(fd);