From: Henrik Austad <haustad@cisco.com>
To: linux-rt-users@vger.kernel.org
Cc: Henrik Austad <haustad@cisco.com>,
Clark Williams <williams@redhat.com>,
John Kacur <jkacur@redhat.com>
Subject: [PATCH 1/4] Properly test for return-values from read() and write()
Date: Tue, 6 Oct 2015 11:08:20 +0200 [thread overview]
Message-ID: <1444122503-8112-1-git-send-email-haustad@cisco.com> (raw)
It is conceivable that read() and write() will fail, and when they do,
it is useful to catch these errors. This also has the benefit of
removing gcc-warnings like :
warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
write(tracing_enabled, "0", 1);
^
warning: ignoring return value of ‘ftruncate’, declared with attribute warn_unused_result [-Wunused-result]
ftruncate(shmem, totalsize);
^
warning: ignoring return value of ‘read’, declared with attribute warn_unused_result [-Wunused-result]
read(path, timestamp, sizeof(timestamp));
^
Signed-off-by: Henrik Austad <haustad@cisco.com>
Cc: Clark Williams <williams@redhat.com>
Cc: John Kacur <jkacur@redhat.com>
---
src/backfire/sendme.c | 8 ++++++--
src/cyclictest/cyclictest.c | 9 ++++++---
src/pmqtest/pmqtest.c | 3 ++-
src/ptsematest/ptsematest.c | 3 ++-
src/rt-migrate-test/rt-migrate-test.c | 5 +++--
src/sigwaittest/sigwaittest.c | 8 ++++++--
src/svsematest/svsematest.c | 9 +++++++--
7 files changed, 32 insertions(+), 13 deletions(-)
diff --git a/src/backfire/sendme.c b/src/backfire/sendme.c
index c1854d9..34504b7 100644
--- a/src/backfire/sendme.c
+++ b/src/backfire/sendme.c
@@ -256,9 +256,13 @@ int main(int argc, char *argv[])
ts.tv_nsec = (interval % USEC_PER_SEC) * 1000;
gettimeofday(&before, NULL);
- write(path, sigtest, strlen(sigtest));
+ if (write(path, sigtest, strlen(sigtest)) < 0)
+ fprintf(stderr, "Could not write sigtest to backfire-path\n");
+
while (after.tv_sec == 0);
- read(path, timestamp, sizeof(timestamp));
+ if (read(path, timestamp, sizeof(timestamp)) <= 0)
+ fprintf(stderr, "Trouble reading file backfire-path\n");
+
if (sscanf(timestamp, "%lu,%lu\n", &sendtime.tv_sec,
&sendtime.tv_usec) != 2)
break;
diff --git a/src/cyclictest/cyclictest.c b/src/cyclictest/cyclictest.c
index 58f1983..22932e9 100644
--- a/src/cyclictest/cyclictest.c
+++ b/src/cyclictest/cyclictest.c
@@ -450,7 +450,8 @@ static void tracemark(char *fmt, ...)
va_start(ap, fmt);
len = vsnprintf(tracebuf, TRACEBUFSIZ, fmt, ap);
va_end(ap);
- write(tracemark_fd, tracebuf, len);
+ if (write(tracemark_fd, tracebuf, len) < 0)
+ err_msg_n(errno, "WARN: could not write to tracebuf");
}
@@ -463,7 +464,8 @@ static void tracing(int on)
case KV_26_LT24: prctl(0, 1); break;
case KV_26_33:
case KV_30:
- write(trace_fd, "1", 1);
+ if (write(trace_fd, "1", 1) < 0)
+ err_msg_n(errno, "Could not set trace_fd");
break;
default: break;
}
@@ -473,7 +475,8 @@ static void tracing(int on)
case KV_26_LT24: prctl(0, 0); break;
case KV_26_33:
case KV_30:
- write(trace_fd, "0", 1);
+ if (write(trace_fd, "0", 1) < 0)
+ err_msg_n(errno, "Could not set trace_fd");
break;
default: break;
}
diff --git a/src/pmqtest/pmqtest.c b/src/pmqtest/pmqtest.c
index 75d5ee8..78eaa9c 100644
--- a/src/pmqtest/pmqtest.c
+++ b/src/pmqtest/pmqtest.c
@@ -210,7 +210,8 @@ void *pmqthread(void *param)
int tracing_enabled =
open(tracing_enabled_file, O_WRONLY);
if (tracing_enabled >= 0) {
- write(tracing_enabled, "0", 1);
+ if (write(tracing_enabled, "0", 1) < 0)
+ err_msg_n(errno, "WARN: could write to tracing_enabled");
close(tracing_enabled);
} else
snprintf(par->error, sizeof(par->error),
diff --git a/src/ptsematest/ptsematest.c b/src/ptsematest/ptsematest.c
index a31c745..f777b38 100644
--- a/src/ptsematest/ptsematest.c
+++ b/src/ptsematest/ptsematest.c
@@ -137,7 +137,8 @@ void *semathread(void *param)
int tracing_enabled =
open(tracing_enabled_file, O_WRONLY);
if (tracing_enabled >= 0) {
- write(tracing_enabled, "0", 1);
+ if (write(tracing_enabled, "0", 1) < 0)
+ err_msg_n(errno, "WARN: Could not enable tracing.");
close(tracing_enabled);
} else
snprintf(par->error, sizeof(par->error),
diff --git a/src/rt-migrate-test/rt-migrate-test.c b/src/rt-migrate-test/rt-migrate-test.c
index d7b68dd..352a331 100644
--- a/src/rt-migrate-test/rt-migrate-test.c
+++ b/src/rt-migrate-test/rt-migrate-test.c
@@ -44,7 +44,7 @@
#include <errno.h>
#include <sched.h>
#include <pthread.h>
-
+#include "error.h"
#define gettid() syscall(__NR_gettid)
int nr_tasks;
@@ -87,7 +87,8 @@ static void ftrace_write(const char *fmt, ...)
n = vsnprintf(buff, BUFSIZ, fmt, ap);
va_end(ap);
- write(mark_fd, buff, n);
+ if (write(mark_fd, buff, n) < 0)
+ err_msg_n(errno, "WARN: Could not write to trace_marker.");
}
#define nano2sec(nan) (nan / 1000000000ULL)
diff --git a/src/sigwaittest/sigwaittest.c b/src/sigwaittest/sigwaittest.c
index 91fcdaa..abeaa35 100644
--- a/src/sigwaittest/sigwaittest.c
+++ b/src/sigwaittest/sigwaittest.c
@@ -36,6 +36,7 @@
#include <sys/time.h>
#include <linux/unistd.h>
#include <utmpx.h>
+#include "error.h"
#include "rt-utils.h"
#include "rt-get_cpu.h"
@@ -185,7 +186,8 @@ void *semathread(void *param)
int tracing_enabled =
open(tracing_enabled_file, O_WRONLY);
if (tracing_enabled >= 0) {
- write(tracing_enabled, "0", 1);
+ if (write(tracing_enabled, "0", 1) < 0)
+ err_msg_n(errno, "WARN: Could not disable tracing.");
close(tracing_enabled);
} else
snprintf(par->error, sizeof(par->error),
@@ -378,7 +380,9 @@ int main(int argc, char *argv[])
fprintf(stderr, "Could not create shared memory\n");
return 1;
}
- ftruncate(shmem, totalsize);
+ if (ftruncate(shmem, totalsize) < 0)
+ err_msg_n(errno, "WARN: Could not truncate file to %d bytes.", totalsize);
+
param = mmap(0, totalsize, PROT_READ|PROT_WRITE, MAP_SHARED,
shmem, 0);
if (param == MAP_FAILED) {
diff --git a/src/svsematest/svsematest.c b/src/svsematest/svsematest.c
index eeb8285..9d4d2b9 100644
--- a/src/svsematest/svsematest.c
+++ b/src/svsematest/svsematest.c
@@ -191,7 +191,8 @@ void *semathread(void *param)
int tracing_enabled =
open(tracing_enabled_file, O_WRONLY);
if (tracing_enabled >= 0) {
- write(tracing_enabled, "0", 1);
+ if (write(tracing_enabled, "0", 1))
+ err_msg_n(errno, "WARN: Could not write to tracing_enabled!");
close(tracing_enabled);
} else
snprintf(par->error, sizeof(par->error),
@@ -431,7 +432,11 @@ int main(int argc, char *argv[])
fprintf(stderr, "Could not create shared memory\n");
return 1;
}
- ftruncate(shmem, totalsize);
+ if (ftruncate(shmem, totalsize) < 0) {
+ fprintf(stderr, "Could not truncate file to specified size (%d)\n", totalsize);
+ return 1;
+
+ }
param = mmap(0, totalsize, PROT_READ|PROT_WRITE, MAP_SHARED,
shmem, 0);
if (param == MAP_FAILED) {
--
1.9.1
--
To unsubscribe from this list: send the line "unsubscribe linux-rt-users" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
next reply other threads:[~2015-10-06 9:11 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-06 9:08 Henrik Austad [this message]
2015-10-06 9:08 ` [PATCH 2/4] signaltest: properly test return value from system() Henrik Austad
2015-10-07 10:44 ` John Kacur
2015-10-06 9:08 ` [PATCH 3/4] Android: clean up the bypass ifdeffery Henrik Austad
2015-10-07 10:34 ` John Kacur
2015-10-07 11:14 ` Henrik Austad
2015-10-07 11:55 ` John Kacur
2015-10-06 9:08 ` [PATCH 4/4] cyclictest: move redefine of CPUSET back to uclib Henrik Austad
2015-10-07 10:28 ` John Kacur
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=1444122503-8112-1-git-send-email-haustad@cisco.com \
--to=haustad@cisco.com \
--cc=jkacur@redhat.com \
--cc=linux-rt-users@vger.kernel.org \
--cc=williams@redhat.com \
/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: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).