From: Chris Wilson <chris@chris-wilson.co.uk> To: intel-gfx@lists.freedesktop.org Cc: igt-dev@lists.freedesktop.org Subject: [PATCH i-g-t] lib/sysfs: Avoid using FILE* temporary for igt_sysfs_[v]printf Date: Thu, 30 Aug 2018 09:44:33 +0100 [thread overview] Message-ID: <20180830084433.10035-1-chris@chris-wilson.co.uk> (raw) Currently we wrap our fd inside a FILE* stream to make use of vfprintf, but the man page leaves the question of errno and signal handling in doubt. It is documented as returning a negative value and setting ferror(), but we have been interpreting errno to handle signal restarting. As that is in doubt, reduce it to a sprintf and reuse our common interrupt handling write() that already returns -errno. Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk> Cc: Katarzyna Dec <katarzyna.dec@intel.com> --- lib/igt_sysfs.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/lib/igt_sysfs.c b/lib/igt_sysfs.c index 8efe889be..b39da4c2a 100644 --- a/lib/igt_sysfs.c +++ b/lib/igt_sysfs.c @@ -387,22 +387,37 @@ int igt_sysfs_scanf(int dir, const char *attr, const char *fmt, ...) int igt_sysfs_vprintf(int dir, const char *attr, const char *fmt, va_list ap) { - FILE *file; - int fd; - int ret = -1; + char stack[128], *buf = stack; + va_list tmp; + int ret, fd; fd = openat(dir, attr, O_WRONLY); if (fd < 0) - return -1; + return -errno; - file = fdopen(fd, "w"); - if (file) { - do { - ret = vfprintf(file, fmt, ap); - } while (ret == -1 && errno == EINTR); - fclose(file); + va_copy(tmp, ap); + ret = vsnprintf(buf, sizeof(stack), fmt, tmp); + va_end(tmp); + if (ret < 0) + return -EINVAL; + + if (ret > sizeof(stack)) { + int len = ret + 1; + + buf = malloc(len); + if (!buf) + return -ENOMEM; + + ret = vsnprintf(buf, ret, fmt, ap); + if (ret > len) { + free(buf); + return -EINVAL; + } } - close(fd); + + ret = writeN(fd, buf, ret); + if (buf != stack) + free(buf); return ret; } -- 2.19.0.rc1 _______________________________________________ Intel-gfx mailing list Intel-gfx@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/intel-gfx
WARNING: multiple messages have this Message-ID (diff)
From: Chris Wilson <chris@chris-wilson.co.uk> To: intel-gfx@lists.freedesktop.org Cc: igt-dev@lists.freedesktop.org Subject: [Intel-gfx] [PATCH i-g-t] lib/sysfs: Avoid using FILE* temporary for igt_sysfs_[v]printf Date: Thu, 30 Aug 2018 09:44:33 +0100 [thread overview] Message-ID: <20180830084433.10035-1-chris@chris-wilson.co.uk> (raw) Currently we wrap our fd inside a FILE* stream to make use of vfprintf, but the man page leaves the question of errno and signal handling in doubt. It is documented as returning a negative value and setting ferror(), but we have been interpreting errno to handle signal restarting. As that is in doubt, reduce it to a sprintf and reuse our common interrupt handling write() that already returns -errno. Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk> Cc: Katarzyna Dec <katarzyna.dec@intel.com> --- lib/igt_sysfs.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/lib/igt_sysfs.c b/lib/igt_sysfs.c index 8efe889be..b39da4c2a 100644 --- a/lib/igt_sysfs.c +++ b/lib/igt_sysfs.c @@ -387,22 +387,37 @@ int igt_sysfs_scanf(int dir, const char *attr, const char *fmt, ...) int igt_sysfs_vprintf(int dir, const char *attr, const char *fmt, va_list ap) { - FILE *file; - int fd; - int ret = -1; + char stack[128], *buf = stack; + va_list tmp; + int ret, fd; fd = openat(dir, attr, O_WRONLY); if (fd < 0) - return -1; + return -errno; - file = fdopen(fd, "w"); - if (file) { - do { - ret = vfprintf(file, fmt, ap); - } while (ret == -1 && errno == EINTR); - fclose(file); + va_copy(tmp, ap); + ret = vsnprintf(buf, sizeof(stack), fmt, tmp); + va_end(tmp); + if (ret < 0) + return -EINVAL; + + if (ret > sizeof(stack)) { + int len = ret + 1; + + buf = malloc(len); + if (!buf) + return -ENOMEM; + + ret = vsnprintf(buf, ret, fmt, ap); + if (ret > len) { + free(buf); + return -EINVAL; + } } - close(fd); + + ret = writeN(fd, buf, ret); + if (buf != stack) + free(buf); return ret; } -- 2.19.0.rc1 _______________________________________________ Intel-gfx mailing list Intel-gfx@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/intel-gfx
next reply other threads:[~2018-08-30 8:44 UTC|newest] Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top 2018-08-30 8:44 Chris Wilson [this message] 2018-08-30 8:44 ` [Intel-gfx] [PATCH i-g-t] lib/sysfs: Avoid using FILE* temporary for igt_sysfs_[v]printf Chris Wilson 2018-08-30 8:51 ` Chris Wilson 2018-08-30 8:51 ` [igt-dev] " Chris Wilson 2018-08-30 10:26 ` Katarzyna Dec 2018-08-30 10:26 ` [igt-dev] " Katarzyna Dec 2018-08-30 17:43 ` [igt-dev] ✓ Fi.CI.BAT: success for " Patchwork 2018-08-31 0:56 ` [igt-dev] ✓ Fi.CI.IGT: " Patchwork
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20180830084433.10035-1-chris@chris-wilson.co.uk \ --to=chris@chris-wilson.co.uk \ --cc=igt-dev@lists.freedesktop.org \ --cc=intel-gfx@lists.freedesktop.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.