From: Johannes Berg Date: Sun, 5 Jul 2009 12:33:25 +0000 (+0200) Subject: use right event size for read() X-Git-Tag: v2.31-rc1~98^2~13^2~34 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=51da1093e715266a8bb6be4f881c8be654053da8;p=thirdparty%2Futil-linux.git 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. --- diff --git a/rfkill.c b/rfkill.c index a7f5f64b01..6b29144b14 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);