Add error handling for write() to tracefs, cpuset control files, and system() calls to avoid ignoring errors. Signed-off-by: Cheng-Yang Chou <yphbchou0911@xxxxxxxxx> --- src/sched_deadline/cyclicdeadline.c | 32 ++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/sched_deadline/cyclicdeadline.c b/src/sched_deadline/cyclicdeadline.c index 04e61ed..6b5d3e0 100644 --- a/src/sched_deadline/cyclicdeadline.c +++ b/src/sched_deadline/cyclicdeadline.c @@ -206,6 +206,7 @@ static void ftrace_write(char *buf, const char *fmt, ...) { va_list ap; int n; + int ret; if (mark_fd < 0) return; @@ -214,7 +215,9 @@ static void ftrace_write(char *buf, const char *fmt, ...) n = my_vsprintf(buf, BUFSIZ, fmt, ap); va_end(ap); - write(mark_fd, buf, n); + ret = write(mark_fd, buf, n); + if (ret < 0) + perror("ftrace write failed"); } static void setup_ftrace_marker(void) @@ -448,6 +451,10 @@ static int mount_cpuset(void) if (fd < 0) return fd; ret = write(fd, "0", 2); + if (ret < 0) { + close(fd); + return ret; + } close(fd); return 0; @@ -634,7 +641,14 @@ static void destroy_cpuset(const char *name, int print) sprintf(buf, "%d", pid); if (print) printf("Moving %d out of %s\n", pid, name); - write(fd, buf, strlen(buf)); + ret = write(fd, buf, strlen(buf)); + if (ret < 0 && errno == ENOSPC) { + fclose(fp); + close(fd); + fatal("Cannot move tasks out of cpuset %s\n", name); + } + + } fclose(fp); close(fd); @@ -656,19 +670,24 @@ static void destroy_cpuset(const char *name, int print) static void teardown(void) { int fd; + int ret; if (all_cpus) return; fd = open_cpuset(CPUSET_PATH, "cpuset.cpu_exclusive"); if (fd >= 0) { - write(fd, "0", 2); + ret = write(fd, "0", 2); + if (ret < 0) + perror("cpuset.cpu_exclusive"); close(fd); } fd = open_cpuset(CPUSET_PATH, "cpuset.sched_load_balance"); if (fd >= 0) { - write(fd, "1", 2); + ret = write(fd, "1", 2); + if (ret < 0) + perror("cpuset.sched_load_balance"); close(fd); } @@ -1164,6 +1183,7 @@ int main(int argc, char **argv) int nr_cpus; int i; int c; + int ret; rt_init(argc, argv); @@ -1387,7 +1407,9 @@ int main(int argc, char **argv) CPUSET_FL_CLONE_CHILDREN | CPUSET_FL_TASKS, pids); - system("cat /sys/fs/cgroup/cpuset/my_cpuset/tasks"); + ret = system("cat /sys/fs/cgroup/cpuset/my_cpuset/tasks"); + if (ret < 0) + perror("system call failed"); } debug(debug_enable, "main thread %d\n", gettid()); -- 2.48.1