From: Cyril H. <su...@li...> - 2012-11-28 12:57:06
|
The branch, master, has been updated via b416d1c43c1f2395905cb042148cf412e9297892 (commit) via da6be579002abf4218a5d77b5c2206de923c4f31 (commit) via 4fee503f099dfdee13bf526b2e27879e446a4796 (commit) via dbec15077a51a6a3c936e93fc8da16a6c24044ee (commit) via 3a1e9bc978882a7e963e54bd8bac5ac7fe728d4b (commit) via 2664913bb05558b8bf9ee403311370ed9f4a984f (commit) via 42748953a9b645ea39d89e5496e04ef939a3b0b3 (commit) from 0d58d20e56b8c8d278c4e121a1c9fb93639a0dc2 (commit) - Log ----------------------------------------------------------------- commit b416d1c43c1f2395905cb042148cf412e9297892 Merge: 0d58d20 da6be57 Author: Cyril Hrubis <ch...@su...> Date: Wed Nov 28 13:22:15 2012 +0100 Merge ../ltp-dev commit da6be579002abf4218a5d77b5c2206de923c4f31 Author: Cyril Hrubis <ch...@su...> Date: Wed Nov 28 13:20:30 2012 +0100 syscalls/madvise03: Cleanup && use SAFE_FILE_* Signed-off-by: Cyril Hrubis <ch...@su...> commit 4fee503f099dfdee13bf526b2e27879e446a4796 Author: Cyril Hrubis <ch...@su...> Date: Wed Nov 28 13:07:27 2012 +0100 syscalls/wait402: Cleanup. Signed-off-by: Cyril Hrubis <ch...@su...> commit dbec15077a51a6a3c936e93fc8da16a6c24044ee Author: Cyril Hrubis <ch...@su...> Date: Wed Nov 28 12:59:23 2012 +0100 syscalls/wait402: Cleanup && use SAFE_FILE_* Signed-off-by: Cyril Hrubis <ch...@su...> commit 3a1e9bc978882a7e963e54bd8bac5ac7fe728d4b Author: Cyril Hrubis <ch...@su...> Date: Tue Nov 27 19:50:27 2012 +0100 syscalls/getsid02: Cleanup && use SAFE_FILE_* Signed-off-by: Cyril Hrubis <ch...@su...> commit 2664913bb05558b8bf9ee403311370ed9f4a984f Author: Cyril Hrubis <ch...@su...> Date: Tue Nov 27 19:42:50 2012 +0100 syscalls/fork13: Make use of SAFE_FILE_* interface. Signed-off-by: Cyril Hrubis <ch...@su...> commit 42748953a9b645ea39d89e5496e04ef939a3b0b3 Author: Cyril Hrubis <ch...@su...> Date: Tue Nov 27 19:26:45 2012 +0100 lib: Add functions to ease pseudo fs read/write This adds SAFE_FILE_SCANF() and SAFE_FILE_PRINTF() safe functions that operate like scanf and printf but works with a path instead of the FILE pointer. The file is opened and closed automatically and all possible error conditions are handled. With this we can reduce the number of fopen, fscanf, fclose sequencies (with possibly incomplete error handling). This also adds SAFE_CP() which is intended replacement for system("cp ...") code. Signed-off-by: Cyril Hrubis <ch...@su...> ----------------------------------------------------------------------- Summary of changes: include/safe_file_ops.h | 75 ++++++++++++ include/test.h | 2 + lib/safe_file_ops.c | 156 +++++++++++++++++++++++++ testcases/kernel/syscalls/fork/fork13.c | 42 ++----- testcases/kernel/syscalls/getsid/getsid02.c | 119 ++----------------- testcases/kernel/syscalls/madvise/madvise03.c | 95 ++-------------- testcases/kernel/syscalls/wait4/wait401.c | 106 +++-------------- testcases/kernel/syscalls/wait4/wait402.c | 136 +++------------------ 8 files changed, 301 insertions(+), 430 deletions(-) create mode 100644 include/safe_file_ops.h create mode 100644 lib/safe_file_ops.c diff --git a/include/safe_file_ops.h b/include/safe_file_ops.h new file mode 100644 index 0000000..ad1eec8 --- /dev/null +++ b/include/safe_file_ops.h @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2012 Cyril Hrubis ch...@su... + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it would be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * Further, this software is distributed without any warranty that it is + * free of the rightful claim of any third person regarding infringement + * or the like. Any license provided herein, whether implied or + * otherwise, applies only to this software file. Patent licenses, if + * any, provided herein do not apply to combinations of this program with + * other software, or any other product whatsoever. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + + /* + + This code helps with file reading/writing files providing scanf/printf like + interface that opens and closes the file automatically. + + This kind of interface is especially useful for reading/writing values + from/to pseudo filesystems like procfs or sysfs. + + */ + +#ifndef SAFE_FILE_OPS +#define SAFE_FILE_OPS + +#include <sys/stat.h> + +#include "test.h" + +/* + * All-in-one function to scanf value(s) from a file. + */ +void safe_file_scanf(const char *file, const int lineno, + void (*cleanup_fn)(void), + const char *path, const char *fmt, ...) + __attribute__ ((format (scanf, 5, 6))); + +#define SAFE_FILE_SCANF(cleanup_fn, path, fmt, ...) \ + safe_file_scanf(__FILE__, __LINE__, (cleanup_fn), \ + (path), (fmt), ## __VA_ARGS__) + +/* + * All-in-one function that lets you printf directly into a file. + */ +void safe_file_printf(const char *file, const int lineno, + void (*cleanup_fn)(void), + const char *path, const char *fmt, ...) + __attribute__ ((format (printf, 5, 6))); + +#define SAFE_FILE_PRINTF(cleanup_fn, path, fmt, ...) \ + safe_file_printf(__FILE__, __LINE__, (cleanup_fn), \ + (path), (fmt), ## __VA_ARGS__) + +/* + * Safe function to copy files, no more system("cp ...") please. + */ +void safe_cp(const char *file, const int lineno, + void (*cleanup_fn)(void), + const char *src, const char *dst); + +#define SAFE_CP(cleanup_fn, src, dst) \ + safe_cp(__FILE__, __LINE__, (cleanup_fn), (src), (dst)) + +#endif /* SAFE_FILE_OPS */ diff --git a/include/test.h b/include/test.h index e05c0f1..7eda7d1 100644 --- a/include/test.h +++ b/include/test.h @@ -44,6 +44,8 @@ #include "compiler.h" +#include "safe_file_ops.h" + /* Use low 6 bits to encode test type */ #define TTYPE_MASK 0x3f #define TPASS 0 /* Test passed flag */ diff --git a/lib/safe_file_ops.c b/lib/safe_file_ops.c new file mode 100644 index 0000000..b1b11f3 --- /dev/null +++ b/lib/safe_file_ops.c @@ -0,0 +1,156 @@ +/* + * Copyright (C) 2012 Cyril Hrubis ch...@su... + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it would be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * Further, this software is distributed without any warranty that it is + * free of the rightful claim of any third person regarding infringement + * or the like. Any license provided herein, whether implied or + * otherwise, applies only to this software file. Patent licenses, if + * |