Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame^] | 1 | Upstream-status: BackPort [http://pkgs.fedoraproject.org/cgit/bridge-utils.git/diff/bridge-utils-1.5-check-error-returns-from-write-to-sysfs.patch?id=b0d10717fd7cebf5d85eed3f941b409fa0384f08] |
| 2 | |
| 3 | Signed-off-by: Roy.Li <rongqing.li@windriver.com> |
| 4 | |
| 5 | From bb9970a9df95837e39d680021b1f73d231e85406 Mon Sep 17 00:00:00 2001 |
| 6 | From: Stephen Hemminger <shemminger@vyatta.com> |
| 7 | Date: Tue, 3 May 2011 09:52:43 -0700 |
| 8 | Subject: [PATCH 3/3] Check error returns from write to sysfs |
| 9 | |
| 10 | Add helper function to check write to sysfs files. |
| 11 | |
| 12 | Signed-off-by: Petr Sabata <contyk@redhat.com> |
| 13 | --- |
| 14 | libbridge/libbridge_devif.c | 37 +++++++++++++++++++++++-------------- |
| 15 | 1 files changed, 23 insertions(+), 14 deletions(-) |
| 16 | |
| 17 | diff --git a/libbridge/libbridge_devif.c b/libbridge/libbridge_devif.c |
| 18 | index aa8bc36..1e83925 100644 |
| 19 | --- a/libbridge/libbridge_devif.c |
| 20 | +++ b/libbridge/libbridge_devif.c |
| 21 | @@ -280,25 +280,38 @@ fallback: |
| 22 | return old_get_port_info(brname, port, info); |
| 23 | } |
| 24 | |
| 25 | +static int set_sysfs(const char *path, unsigned long value) |
| 26 | +{ |
| 27 | + int fd, ret = 0, cc; |
| 28 | + char buf[32]; |
| 29 | + |
| 30 | + fd = open(path, O_WRONLY); |
| 31 | + if (fd < 0) |
| 32 | + return -1; |
| 33 | + |
| 34 | + cc = snprintf(buf, sizeof(buf), "%lu\n", value); |
| 35 | + if (write(fd, buf, cc) < 0) |
| 36 | + ret = -1; |
| 37 | + close(fd); |
| 38 | + |
| 39 | + return ret; |
| 40 | +} |
| 41 | + |
| 42 | |
| 43 | static int br_set(const char *bridge, const char *name, |
| 44 | unsigned long value, unsigned long oldcode) |
| 45 | { |
| 46 | int ret; |
| 47 | char path[SYSFS_PATH_MAX]; |
| 48 | - FILE *f; |
| 49 | |
| 50 | - snprintf(path, SYSFS_PATH_MAX, SYSFS_CLASS_NET "%s/%s", bridge, name); |
| 51 | + snprintf(path, SYSFS_PATH_MAX, SYSFS_CLASS_NET "%s/bridge/%s", |
| 52 | + bridge, name); |
| 53 | |
| 54 | - f = fopen(path, "w"); |
| 55 | - if (f) { |
| 56 | - ret = fprintf(f, "%ld\n", value); |
| 57 | - fclose(f); |
| 58 | - } else { |
| 59 | + if ((ret = set_sysfs(path, value)) < 0) { |
| 60 | /* fallback to old ioctl */ |
| 61 | struct ifreq ifr; |
| 62 | unsigned long args[4] = { oldcode, value, 0, 0 }; |
| 63 | - |
| 64 | + |
| 65 | strncpy(ifr.ifr_name, bridge, IFNAMSIZ); |
| 66 | ifr.ifr_data = (char *) &args; |
| 67 | ret = ioctl(br_socket_fd, SIOCDEVPRIVATE, &ifr); |
| 68 | @@ -348,14 +361,10 @@ static int port_set(const char *bridge, const char *ifname, |
| 69 | { |
| 70 | int ret; |
| 71 | char path[SYSFS_PATH_MAX]; |
| 72 | - FILE *f; |
| 73 | |
| 74 | snprintf(path, SYSFS_PATH_MAX, SYSFS_CLASS_NET "%s/brport/%s", ifname, name); |
| 75 | - f = fopen(path, "w"); |
| 76 | - if (f) { |
| 77 | - ret = fprintf(f, "%ld\n", value); |
| 78 | - fclose(f); |
| 79 | - } else { |
| 80 | + |
| 81 | + if ((ret = set_sysfs(path, value)) < 0) { |
| 82 | int index = get_portno(bridge, ifname); |
| 83 | |
| 84 | if (index < 0) |
| 85 | -- |
| 86 | 1.7.5.2 |
| 87 | |