* [LTP] [PATCH v2 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags()
@ 2014-04-03 12:00 gux.fnst
2014-04-03 12:00 ` [LTP] [PATCH v2 2/2] openat/openat02.c: add a new case to test flags gux.fnst
` (4 more replies)
0 siblings, 5 replies; 17+ messages in thread
From: gux.fnst @ 2014-04-03 12:00 UTC (permalink / raw)
To: ltp-list@lists.sourceforge.net
Create a function tst_path_has_mnt_flags() for checking whether
a path is on a system that is mounted with specified flags.
Signed-off-by: Xing Gu <gux.fnst@cn.fujitsu.com>
---
include/test.h | 9 ++++
lib/tst_path_has_mnt_flags.c | 99 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 108 insertions(+)
create mode 100644 lib/tst_path_has_mnt_flags.c
diff --git a/include/test.h b/include/test.h
index baa3ef6..39bc5a1 100644
--- a/include/test.h
+++ b/include/test.h
@@ -295,6 +295,15 @@ gid_t tst_get_unused_gid(void);
unsigned short tst_get_unused_port(void (cleanup_fn)(void),
unsigned short family, int type);
+/* lib/tst_path_has_mnt_flags.c
+ *
+ * Check whether a path is on a filesystem that is mounted with
+ * specified flags.
+ * @path: path to file
+ * @flags: mount flags
+ */
+int tst_path_has_mnt_flags(const char *path, const char *flags[]);
+
#ifdef TST_USE_COMPAT16_SYSCALL
#define TCID_BIT_SUFFIX "_16"
#elif TST_USE_NEWER64_SYSCALL
diff --git a/lib/tst_path_has_mnt_flags.c b/lib/tst_path_has_mnt_flags.c
new file mode 100644
index 0000000..257b8a4
--- /dev/null
+++ b/lib/tst_path_has_mnt_flags.c
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 2014 Fujitsu Ltd.
+ * Author: Xing Gu <gux.fnst@cn.fujitsu.com>
+ *
+ * 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.
+ *
+ * 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.
+ */
+
+#include <unistd.h>
+#include <mntent.h>
+#include "test.h"
+
+/*
+ * Returns if prefix is prefix of a string and the lenght of prefix.
+ */
+int strpref(const char *str, const char *pref)
+{
+ int i;
+
+ for (i = 0; pref[i] != '\0'; i++) {
+ /* string ended too soon */
+ if (str[i] == 0)
+ return -1;
+
+ /* string is diferent */
+ if (str[i] != pref[i])
+ return -1;
+ }
+
+ /* returns lenght of prefix */
+ return i;
+}
+
+/*
+ * Check whether a path is on a filesystem that is mounted with
+ * specified flags.
+ *
+ * Returns:
+ * -1 - an error has occurred
+ * 0 - the filesystem does not have any specified flags
+ * 1 - the filesystem has at least one flag matched
+ */
+int tst_path_has_mnt_flags(const char *path, const char *flags[])
+{
+ struct mntent *mnt;
+ int prefix_max = 0, prefix;
+ int has_flags = 0, has_opt;
+ FILE *f;
+ int i;
+
+ if (path == NULL) {
+ printf("The path is NULL.\n");
+ return -1;
+ }
+
+ if (access(path, F_OK) == -1) {
+ printf("The path %s doesn't exist.\n", path);
+ return -1;
+ }
+
+ f = setmntent("/proc/mounts", "r");
+
+ if (f == NULL) {
+ printf("Couldn't mount /proc/mounts.\n");
+ return -1;
+ }
+
+ while ((mnt = getmntent(f))) {
+ /* ignore all pseudo fs */
+ if (mnt->mnt_fsname[0] != '/')
+ continue;
+
+ prefix = strpref(path, mnt->mnt_dir);
+
+ if (prefix > prefix_max) {
+ prefix_max = prefix;
+ has_opt = 0;
+ i = 0;
+ while ((has_opt == 0) && (flags[i] != NULL)) {
+ has_opt = hasmntopt(mnt, flags[i]) != NULL;
+ i++;
+ }
+ has_flags = has_opt;
+ }
+ }
+
+ endmntent(f);
+
+ return has_flags;
+}
--
1.8.3.1
------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [LTP] [PATCH v2 2/2] openat/openat02.c: add a new case to test flags
2014-04-03 12:00 [LTP] [PATCH v2 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags() gux.fnst
@ 2014-04-03 12:00 ` gux.fnst
2014-04-04 10:01 ` [LTP] [PATCH v2 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags() Jan Stancek
` (3 subsequent siblings)
4 siblings, 0 replies; 17+ messages in thread
From: gux.fnst @ 2014-04-03 12:00 UTC (permalink / raw)
To: ltp-list@lists.sourceforge.net
create a new case to test flags for openat(2):
O_APPEND
O_CLOEXEC
O_LARGEFILE
O_NOATIME
O_NOFOLLOW
O_TRUNC
Signed-off-by: Xing Gu <gux.fnst@cn.fujitsu.com>
---
configure.ac | 1 +
m4/ltp-openat.m4 | 25 ++
runtest/syscalls | 1 +
testcases/kernel/syscalls/.gitignore | 1 +
testcases/kernel/syscalls/openat/openat.h | 35 +++
testcases/kernel/syscalls/openat/openat01.c | 13 +-
testcases/kernel/syscalls/openat/openat02.c | 306 ++++++++++++++++++++++
testcases/kernel/syscalls/openat/openat02_child.c | 41 +++
8 files changed, 414 insertions(+), 9 deletions(-)
create mode 100644 m4/ltp-openat.m4
create mode 100644 testcases/kernel/syscalls/openat/openat.h
create mode 100644 testcases/kernel/syscalls/openat/openat02.c
create mode 100644 testcases/kernel/syscalls/openat/openat02_child.c
diff --git a/configure.ac b/configure.ac
index 44016e7..4e8f1c6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -175,5 +175,6 @@ LTP_CHECK_FCHOWNAT
LTP_CHECK_MKNODAT
LTP_CHECK_FALLOCATE
LTP_CHECK_SYSCALL_FCNTL
+LTP_CHECK_OPENAT
AC_OUTPUT
diff --git a/m4/ltp-openat.m4 b/m4/ltp-openat.m4
new file mode 100644
index 0000000..aa3b0a3
--- /dev/null
+++ b/m4/ltp-openat.m4
@@ -0,0 +1,25 @@
+dnl
+dnl Copyright (c) Linux Test Project, 2014
+dnl
+dnl This program is free software; you can redistribute it and/or modify
+dnl it under the terms of the GNU General Public License as published by
+dnl the Free Software Foundation; either version 2 of the License, or
+dnl (at your option) any later version.
+dnl
+dnl This program is distributed in the hope that it will be useful,
+dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
+dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
+dnl the GNU General Public License for more details.
+dnl
+dnl You should have received a copy of the GNU General Public License
+dnl along with this program; if not, write to the Free Software
+dnl Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+dnl
+
+dnl
+dnl LTP_CHECK_OPENAT
+dnl ----------------------------
+dnl
+AC_DEFUN([LTP_CHECK_OPENAT],[
+AC_CHECK_FUNCS(openat,,)
+])
diff --git a/runtest/syscalls b/runtest/syscalls
index fb3e59f..a775e52 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -710,6 +710,7 @@ open11 open11
#openat test cases
openat01 openat01
+openat02 openat02
mincore01 mincore01
mincore02 mincore02
diff --git a/testcases/kernel/syscalls/.gitignore b/testcases/kernel/syscalls/.gitignore
index d5c7bac..90a47b7 100644
--- a/testcases/kernel/syscalls/.gitignore
+++ b/testcases/kernel/syscalls/.gitignore
@@ -602,6 +602,7 @@
/open/open10
/open/open11
/openat/openat01
+/openat/openat02
/pathconf/pathconf01
/pause/pause01
/pause/pause02
diff --git a/testcases/kernel/syscalls/openat/openat.h b/testcases/kernel/syscalls/openat/openat.h
new file mode 100644
index 0000000..fce4e3f
--- /dev/null
+++ b/testcases/kernel/syscalls/openat/openat.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) International Business Machines Corp., 2007
+ * Copyright (c) 2014 Fujitsu Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#ifndef OPENAT_H
+#define OPENAT_H
+
+#include <sys/types.h>
+#include "config.h"
+#include "linux_syscall_numbers.h"
+
+#if !defined(HAVE_OPENAT)
+int openat(int dirfd, const char *pathname, int flags, mode_t mode)
+{
+ return ltp_syscall(__NR_openat, dirfd, pathname, flags, mode);
+}
+#endif
+
+#endif /* OPENAT_H */
diff --git a/testcases/kernel/syscalls/openat/openat01.c b/testcases/kernel/syscalls/openat/openat01.c
index 2f68d2f..f03de07 100644
--- a/testcases/kernel/syscalls/openat/openat01.c
+++ b/testcases/kernel/syscalls/openat/openat01.c
@@ -55,11 +55,11 @@
#include "test.h"
#include "usctest.h"
#include "linux_syscall_numbers.h"
+#include "lapi/fcntl.h"
+#include "openat.h"
#define TEST_CASES 5
-#ifndef AT_FDCWD
-#define AT_FDCWD -100
-#endif
+
void setup();
void cleanup();
void setup_every_copy();
@@ -75,11 +75,6 @@ int fds[TEST_CASES];
char *filenames[TEST_CASES];
int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, 0 };
-int myopenat(int dirfd, const char *filename, int flags, int mode)
-{
- return ltp_syscall(__NR_openat, dirfd, filename, flags, mode);
-}
-
int main(int ac, char **av)
{
int lc;
@@ -116,7 +111,7 @@ int main(int ac, char **av)
* Call openat
*/
for (i = 0; i < TST_TOTAL; i++) {
- TEST(myopenat
+ TEST(openat
(fds[i], filenames[i], O_CREAT | O_WRONLY, 0600));
/* check return code */
diff --git a/testcases/kernel/syscalls/openat/openat02.c b/testcases/kernel/syscalls/openat/openat02.c
new file mode 100644
index 0000000..88c516c
--- /dev/null
+++ b/testcases/kernel/syscalls/openat/openat02.c
@@ -0,0 +1,306 @@
+/*
+ * Copyright (c) 2014 Fujitsu Ltd.
+ * Author: Xing Gu <gux.fnst@cn.fujitsu.com>
+ *
+ * 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.
+ *
+ * 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.
+ */
+/*
+ * Description:
+ * Verify that,
+ * 1)openat() succeeds to open a file in append mode, when
+ * 'flags' is set to O_APPEND.
+ * 2)openat() succeeds to enable the close-on-exec flag for a
+ * file descriptor, when 'flags' is set to O_CLOEXEC.
+ * 3)openat() succeeds to allow files whose sizes cannot be
+ * represented in an off_t but can be represented in an off64_t
+ * to be opened, when 'flags' is set to O_LARGEFILE.
+ * 4)openat() succeeds to not update the file last access time
+ * (st_atime in the inode) when the file is read, when 'flags'
+ * is set to O_NOATIME.
+ * 5)openat() succeeds to open the file failed if the file is a
+ * symbolic link, when 'flags' is set to O_NOFOLLOW.
+ * 6)openat() succeeds to truncate the file to length 0 if the file
+ * already exists and is a regular file and the open mode allows
+ * writing, when 'flags' is set to O_TRUNC.
+ */
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <mntent.h>
+
+#include "test.h"
+#include "usctest.h"
+#include "safe_macros.h"
+#include "lapi/fcntl.h"
+#include "openat.h"
+
+#define TEST_APP "openat02_child"
+
+#define TEST_FILE "test_file"
+#define SFILE "sfile"
+#define LARGE_FILE "large_file"
+
+#define STR "abcdefg"
+
+static void setup(void);
+static void cleanup(void);
+
+static void testfunc_append(void);
+static void testfunc_cloexec(void);
+static void testfunc_largefile(void);
+static void testfunc_noatime(void);
+static void testfunc_nofollow(void);
+static void testfunc_trunc(void);
+
+static void (*testfunc[])(void) = {
+ testfunc_append,
+ testfunc_cloexec,
+ testfunc_largefile,
+ testfunc_noatime,
+ testfunc_nofollow,
+ testfunc_trunc,
+};
+
+char *TCID = "openat02";
+int TST_TOTAL = ARRAY_SIZE(testfunc);
+
+int main(int ac, char **av)
+{
+ int lc;
+ int i;
+ char *msg;
+
+ msg = parse_opts(ac, av, NULL, NULL);
+ if (msg != NULL)
+ tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
+
+ setup();
+
+ for (lc = 0; TEST_LOOPING(lc); lc++) {
+ tst_count = 0;
+
+ for (i = 0; i < TST_TOTAL; i++)
+ (*testfunc[i])();
+ }
+
+ cleanup();
+ tst_exit();
+}
+
+void setup(void)
+{
+ tst_require_root(NULL);
+
+ TEST_PAUSE;
+
+ tst_sig(FORK, DEF_HANDLER, cleanup);
+
+ tst_tmpdir();
+
+ SAFE_FILE_PRINTF(cleanup, TEST_FILE, "test file");
+
+ SAFE_SYMLINK(cleanup, TEST_FILE, SFILE);
+}
+
+void testfunc_append(void)
+{
+ off_t file_offset;
+
+ SAFE_FILE_PRINTF(cleanup, TEST_FILE, "test file");
+
+ TEST(openat(AT_FDCWD, TEST_FILE, O_APPEND | O_RDWR, 0777));
+
+ if (TEST_RETURN == -1) {
+ tst_resm(TFAIL | TTERRNO, "openat failed");
+ return;
+ }
+
+ SAFE_WRITE(cleanup, 1, TEST_RETURN, STR, sizeof(STR) - 1);
+
+ file_offset = SAFE_LSEEK(cleanup, TEST_RETURN, 0, SEEK_CUR);
+
+ if (file_offset > (off_t)(sizeof(STR) - 1))
+ tst_resm(TPASS, "test O_APPEND for openat success");
+ else
+ tst_resm(TFAIL, "test O_APPEND for openat failed");
+
+ SAFE_CLOSE(cleanup, TEST_RETURN);
+}
+
+void testfunc_cloexec(void)
+{
+ pid_t pid;
+ int status;
+ char buf[20];
+
+ if ((tst_kvercmp(2, 6, 23)) < 0) {
+ tst_resm(TCONF, "test O_CLOEXEC flags for openat "
+ "needs kernel 2.6.23 or higher");
+ return;
+ }
+
+ TEST(openat(AT_FDCWD, TEST_FILE, O_CLOEXEC | O_RDWR, 0777));
+
+ if (TEST_RETURN == -1) {
+ tst_resm(TFAIL | TTERRNO, "openat failed");
+ return;
+ }
+
+ sprintf(buf, "%ld", TEST_RETURN);
+
+ pid = tst_fork();
+
+ if (pid < 0)
+ tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
+
+ if (pid == 0) {
+ if (execlp(TEST_APP, TEST_APP, buf, NULL))
+ exit(1);
+ }
+
+ SAFE_CLOSE(cleanup, TEST_RETURN);
+
+ if (wait(&status) == -1)
+ tst_brkm(TBROK | TERRNO, cleanup, "wait() failed");
+
+ if (WIFEXITED(status)) {
+ switch ((int8_t)WEXITSTATUS(status)) {
+ case -1:
+ tst_resm(TPASS, "test O_CLOEXEC for openat success");
+ break;
+ case 1:
+ tst_brkm(TBROK, cleanup, "execlp() failed");
+ default:
+ tst_resm(TFAIL, "test O_CLOEXEC for openat failed");
+ }
+ } else {
+ tst_brkm(TBROK, cleanup,
+ "openat2_exec exits with unexpected error");
+ }
+}
+
+void testfunc_largefile(void)
+{
+ int fd;
+ off64_t offset;
+
+ fd = SAFE_OPEN(cleanup, LARGE_FILE,
+ O_LARGEFILE | O_RDWR | O_CREAT, 0777);
+
+ offset = lseek64(fd, 4.1*1024*1024*1024, SEEK_SET);
+ if (offset == -1)
+ tst_brkm(TBROK | TERRNO, cleanup, "lseek64 failed");
+
+ SAFE_WRITE(cleanup, 1, fd, STR, sizeof(STR) - 1);
+
+ SAFE_CLOSE(cleanup, fd);
+
+ TEST(openat(AT_FDCWD, LARGE_FILE, O_LARGEFILE | O_RDONLY, 0777));
+
+ if (TEST_RETURN == -1) {
+ tst_resm(TFAIL, "test O_LARGEFILE for openat failed");
+ } else {
+ tst_resm(TPASS, "test O_LARGEFILE for openat success");
+ SAFE_CLOSE(cleanup, TEST_RETURN);
+ }
+}
+
+void testfunc_noatime(void)
+{
+ struct stat file_stat, file_newstat;
+ char buf;
+ const char *flags[] = {"noatime", "relatime", NULL};
+
+ if ((tst_kvercmp(2, 6, 8)) < 0) {
+ tst_resm(TCONF, "test O_NOATIME flags for openat "
+ "needs kernel 2.6.8 or higher");
+ return;
+ }
+
+ if (tst_path_has_mnt_flags(TEST_FILE, flags)) {
+ tst_resm(TCONF, "test O_NOATIME flag for openat needs "
+ "filesystems which are mounted without "
+ "noatime and relatime");
+ return;
+ }
+
+ SAFE_STAT(cleanup, TEST_FILE, &file_stat);
+
+ sleep(2);
+
+ TEST(openat(AT_FDCWD, TEST_FILE, O_NOATIME | O_RDONLY, 0777));
+
+ if (TEST_RETURN == -1) {
+ tst_resm(TFAIL | TTERRNO, "openat failed");
+ return;
+ }
+
+ SAFE_READ(cleanup, 1, TEST_RETURN, &buf, 1);
+
+ SAFE_CLOSE(cleanup, TEST_RETURN);
+
+ SAFE_STAT(cleanup, TEST_FILE, &file_newstat);
+
+ if (file_stat.st_atime == file_newstat.st_atime)
+ tst_resm(TPASS, "test O_NOATIME for openat success");
+ else
+ tst_resm(TFAIL, "test O_NOATIME for openat failed");
+}
+
+void testfunc_nofollow(void)
+{
+ TEST(openat(AT_FDCWD, SFILE, O_NOFOLLOW | O_RDONLY, 0777));
+
+ if (TEST_RETURN == -1 && TEST_ERRNO == ELOOP) {
+ tst_resm(TPASS, "test O_NOFOLLOW for openat success");
+ } else {
+ tst_resm(TFAIL, "test O_NOFOLLOW for openat failed");
+ SAFE_CLOSE(cleanup, TEST_RETURN);
+ }
+}
+
+void testfunc_trunc(void)
+{
+ struct stat file_stat;
+
+ TEST(openat(AT_FDCWD, TEST_FILE, O_TRUNC | O_RDWR, 0777));
+
+ if (TEST_RETURN == -1) {
+ tst_resm(TFAIL | TTERRNO, "openat failed");
+ return;
+ }
+
+ SAFE_FSTAT(cleanup, TEST_RETURN, &file_stat);
+
+ if (file_stat.st_size == 0)
+ tst_resm(TPASS, "test O_TRUNC for openat success");
+ else
+ tst_resm(TFAIL, "test O_TRUNC for openat failed");
+
+ SAFE_CLOSE(cleanup, TEST_RETURN);
+}
+
+void cleanup(void)
+{
+ TEST_CLEANUP;
+
+ tst_rmdir();
+}
diff --git a/testcases/kernel/syscalls/openat/openat02_child.c b/testcases/kernel/syscalls/openat/openat02_child.c
new file mode 100644
index 0000000..d2af36b
--- /dev/null
+++ b/testcases/kernel/syscalls/openat/openat02_child.c
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2014 Fujitsu Ltd.
+ * Author: Xing Gu <gux.fnst@cn.fujitsu.com>
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+
+#define STR "abc"
+
+char *TCID = "openat02_child";
+
+int main(int argc, char **argv)
+{
+ int fd;
+ int ret;
+
+ if (argc != 2) {
+ fprintf(stderr, "%s <fd>\n", argv[0]);
+ exit(1);
+ }
+
+ fd = atoi(argv[1]);
+ ret = write(fd, STR, sizeof(STR) - 1);
+
+ exit(ret);
+}
--
1.8.3.1
------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply related [flat|nested] 17+ messages in thread
* Re: [LTP] [PATCH v2 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags()
2014-04-03 12:00 [LTP] [PATCH v2 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags() gux.fnst
2014-04-03 12:00 ` [LTP] [PATCH v2 2/2] openat/openat02.c: add a new case to test flags gux.fnst
@ 2014-04-04 10:01 ` Jan Stancek
2014-04-09 2:26 ` gux.fnst
2014-04-09 3:46 ` [LTP] [PATCH v3] " gux.fnst
` (2 subsequent siblings)
4 siblings, 1 reply; 17+ messages in thread
From: Jan Stancek @ 2014-04-04 10:01 UTC (permalink / raw)
To: gux fnst; +Cc: ltp-list
----- Original Message -----
> From: "gux fnst" <gux.fnst@cn.fujitsu.com>
> To: ltp-list@lists.sourceforge.net
> Sent: Thursday, 3 April, 2014 2:00:44 PM
> Subject: [LTP] [PATCH v2 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags()
>
> Create a function tst_path_has_mnt_flags() for checking whether
> a path is on a system that is mounted with specified flags.
>
> Signed-off-by: Xing Gu <gux.fnst@cn.fujitsu.com>
> ---
> include/test.h | 9 ++++
> lib/tst_path_has_mnt_flags.c | 99
> ++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 108 insertions(+)
> create mode 100644 lib/tst_path_has_mnt_flags.c
>
> diff --git a/include/test.h b/include/test.h
> index baa3ef6..39bc5a1 100644
> --- a/include/test.h
> +++ b/include/test.h
> @@ -295,6 +295,15 @@ gid_t tst_get_unused_gid(void);
> unsigned short tst_get_unused_port(void (cleanup_fn)(void),
> unsigned short family, int type);
>
> +/* lib/tst_path_has_mnt_flags.c
> + *
> + * Check whether a path is on a filesystem that is mounted with
> + * specified flags.
> + * @path: path to file
> + * @flags: mount flags
> + */
> +int tst_path_has_mnt_flags(const char *path, const char *flags[]);
> +
> #ifdef TST_USE_COMPAT16_SYSCALL
> #define TCID_BIT_SUFFIX "_16"
> #elif TST_USE_NEWER64_SYSCALL
> diff --git a/lib/tst_path_has_mnt_flags.c b/lib/tst_path_has_mnt_flags.c
> new file mode 100644
> index 0000000..257b8a4
> --- /dev/null
> +++ b/lib/tst_path_has_mnt_flags.c
> @@ -0,0 +1,99 @@
> +/*
> + * Copyright (c) 2014 Fujitsu Ltd.
> + * Author: Xing Gu <gux.fnst@cn.fujitsu.com>
> + *
> + * 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.
> + *
> + * 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.
> + */
> +
> +#include <unistd.h>
> +#include <mntent.h>
> +#include "test.h"
> +
> +/*
> + * Returns if prefix is prefix of a string and the lenght of prefix.
> + */
> +int strpref(const char *str, const char *pref)
> +{
> + int i;
> +
> + for (i = 0; pref[i] != '\0'; i++) {
> + /* string ended too soon */
> + if (str[i] == 0)
> + return -1;
> +
> + /* string is diferent */
> + if (str[i] != pref[i])
> + return -1;
> + }
> +
> + /* returns lenght of prefix */
> + return i;
> +}
> +
> +/*
> + * Check whether a path is on a filesystem that is mounted with
> + * specified flags.
> + *
> + * Returns:
> + * -1 - an error has occurred
> + * 0 - the filesystem does not have any specified flags
> + * 1 - the filesystem has at least one flag matched
> + */
> +int tst_path_has_mnt_flags(const char *path, const char *flags[])
> +{
> + struct mntent *mnt;
> + int prefix_max = 0, prefix;
> + int has_flags = 0, has_opt;
> + FILE *f;
> + int i;
> +
> + if (path == NULL) {
> + printf("The path is NULL.\n");
> + return -1;
> + }
> +
> + if (access(path, F_OK) == -1) {
> + printf("The path %s doesn't exist.\n", path);
> + return -1;
> + }
> +
> + f = setmntent("/proc/mounts", "r");
> +
> + if (f == NULL) {
> + printf("Couldn't mount /proc/mounts.\n");
> + return -1;
> + }
> +
> + while ((mnt = getmntent(f))) {
> + /* ignore all pseudo fs */
> + if (mnt->mnt_fsname[0] != '/')
> + continue;
> +
> + prefix = strpref(path, mnt->mnt_dir);
Hi,
Can strpref() return anything other than -1 or strlen(mnt->mnt_dir)?
How about using strncmp instead?
size_t prefix_len = strlen(mnt->mnt_dir);
if (strncmp(path, mnt->mnt_dir, prefix_len) == 0 && prefix_len > prefix_max) {
...
}
Regards,
Jan
> +
> + if (prefix > prefix_max) {
> + prefix_max = prefix;
> + has_opt = 0;
> + i = 0;
> + while ((has_opt == 0) && (flags[i] != NULL)) {
> + has_opt = hasmntopt(mnt, flags[i]) != NULL;
> + i++;
> + }
> + has_flags = has_opt;
> + }
> + }
> +
> + endmntent(f);
> +
> + return has_flags;
> +}
> --
> 1.8.3.1
> ------------------------------------------------------------------------------
> _______________________________________________
> Ltp-list mailing list
> Ltp-list@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/ltp-list
>
------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [LTP] [PATCH v2 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags()
2014-04-04 10:01 ` [LTP] [PATCH v2 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags() Jan Stancek
@ 2014-04-09 2:26 ` gux.fnst
0 siblings, 0 replies; 17+ messages in thread
From: gux.fnst @ 2014-04-09 2:26 UTC (permalink / raw)
To: Jan Stancek; +Cc: ltp-list
[-- Attachment #1.1: Type: text/plain, Size: 4876 bytes --]
On 04/04/2014 06:01 PM, Jan Stancek wrote:
>
>
>
> ----- Original Message -----
>> From: "gux fnst" <gux.fnst@cn.fujitsu.com>
>> To: ltp-list@lists.sourceforge.net
>> Sent: Thursday, 3 April, 2014 2:00:44 PM
>> Subject: [LTP] [PATCH v2 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags()
>>
>> Create a function tst_path_has_mnt_flags() for checking whether
>> a path is on a system that is mounted with specified flags.
>>
>> Signed-off-by: Xing Gu <gux.fnst@cn.fujitsu.com>
>> ---
>> include/test.h | 9 ++++
>> lib/tst_path_has_mnt_flags.c | 99
>> ++++++++++++++++++++++++++++++++++++++++++++
>> 2 files changed, 108 insertions(+)
>> create mode 100644 lib/tst_path_has_mnt_flags.c
>>
>> diff --git a/include/test.h b/include/test.h
>> index baa3ef6..39bc5a1 100644
>> --- a/include/test.h
>> +++ b/include/test.h
>> @@ -295,6 +295,15 @@ gid_t tst_get_unused_gid(void);
>> unsigned short tst_get_unused_port(void (cleanup_fn)(void),
>> unsigned short family, int type);
>>
>> +/* lib/tst_path_has_mnt_flags.c
>> + *
>> + * Check whether a path is on a filesystem that is mounted with
>> + * specified flags.
>> + * @path: path to file
>> + * @flags: mount flags
>> + */
>> +int tst_path_has_mnt_flags(const char *path, const char *flags[]);
>> +
>> #ifdef TST_USE_COMPAT16_SYSCALL
>> #define TCID_BIT_SUFFIX "_16"
>> #elif TST_USE_NEWER64_SYSCALL
>> diff --git a/lib/tst_path_has_mnt_flags.c b/lib/tst_path_has_mnt_flags.c
>> new file mode 100644
>> index 0000000..257b8a4
>> --- /dev/null
>> +++ b/lib/tst_path_has_mnt_flags.c
>> @@ -0,0 +1,99 @@
>> +/*
>> + * Copyright (c) 2014 Fujitsu Ltd.
>> + * Author: Xing Gu <gux.fnst@cn.fujitsu.com>
>> + *
>> + * 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.
>> + *
>> + * 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.
>> + */
>> +
>> +#include <unistd.h>
>> +#include <mntent.h>
>> +#include "test.h"
>> +
>> +/*
>> + * Returns if prefix is prefix of a string and the lenght of prefix.
>> + */
>> +int strpref(const char *str, const char *pref)
>> +{
>> + int i;
>> +
>> + for (i = 0; pref[i] != '\0'; i++) {
>> + /* string ended too soon */
>> + if (str[i] == 0)
>> + return -1;
>> +
>> + /* string is diferent */
>> + if (str[i] != pref[i])
>> + return -1;
>> + }
>> +
>> + /* returns lenght of prefix */
>> + return i;
>> +}
>> +
>> +/*
>> + * Check whether a path is on a filesystem that is mounted with
>> + * specified flags.
>> + *
>> + * Returns:
>> + * -1 - an error has occurred
>> + * 0 - the filesystem does not have any specified flags
>> + * 1 - the filesystem has at least one flag matched
>> + */
>> +int tst_path_has_mnt_flags(const char *path, const char *flags[])
>> +{
>> + struct mntent *mnt;
>> + int prefix_max = 0, prefix;
>> + int has_flags = 0, has_opt;
>> + FILE *f;
>> + int i;
>> +
>> + if (path == NULL) {
>> + printf("The path is NULL.\n");
>> + return -1;
>> + }
>> +
>> + if (access(path, F_OK) == -1) {
>> + printf("The path %s doesn't exist.\n", path);
>> + return -1;
>> + }
>> +
>> + f = setmntent("/proc/mounts", "r");
>> +
>> + if (f == NULL) {
>> + printf("Couldn't mount /proc/mounts.\n");
>> + return -1;
>> + }
>> +
>> + while ((mnt = getmntent(f))) {
>> + /* ignore all pseudo fs */
>> + if (mnt->mnt_fsname[0] != '/')
>> + continue;
>> +
>> + prefix = strpref(path, mnt->mnt_dir);
> Hi,
>
> Can strpref() return anything other than -1 or strlen(mnt->mnt_dir)?
> How about using strncmp instead?
>
> size_t prefix_len = strlen(mnt->mnt_dir);
> if (strncmp(path, mnt->mnt_dir, prefix_len) == 0 && prefix_len > prefix_max) {
> ...
> }
Thank you for your comment. The code using strncmp() is better.
Regards,
Xing Gu
> Regards,
> Jan
>
>> +
>> + if (prefix > prefix_max) {
>> + prefix_max = prefix;
>> + has_opt = 0;
>> + i = 0;
>> + while ((has_opt == 0) && (flags[i] != NULL)) {
>> + has_opt = hasmntopt(mnt, flags[i]) != NULL;
>> + i++;
>> + }
>> + has_flags = has_opt;
>> + }
>> + }
>> +
>> + endmntent(f);
>> +
>> + return has_flags;
>> +}
>> --
>> 1.8.3.1
>> ------------------------------------------------------------------------------
>> _______________________________________________
>> Ltp-list mailing list
>> Ltp-list@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/ltp-list
>>
> .
>
[-- Attachment #1.2: Type: text/html, Size: 5859 bytes --]
[-- Attachment #2: Type: text/plain, Size: 298 bytes --]
------------------------------------------------------------------------------
Put Bad Developers to Shame
Dominate Development with Jenkins Continuous Integration
Continuously Automate Build, Test & Deployment
Start a new project now. Try Jenkins in the cloud.
http://p.sf.net/sfu/13600_Cloudbees
[-- Attachment #3: Type: text/plain, Size: 155 bytes --]
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply [flat|nested] 17+ messages in thread
* [LTP] [PATCH v3] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags()
2014-04-03 12:00 [LTP] [PATCH v2 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags() gux.fnst
2014-04-03 12:00 ` [LTP] [PATCH v2 2/2] openat/openat02.c: add a new case to test flags gux.fnst
2014-04-04 10:01 ` [LTP] [PATCH v2 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags() Jan Stancek
@ 2014-04-09 3:46 ` gux.fnst
2014-04-09 10:34 ` Jan Stancek
2014-04-11 7:54 ` [LTP] [PATCH v4 1/2] " gux.fnst
2014-05-08 9:50 ` [LTP] [PATCH v5 " Xing Gu
4 siblings, 1 reply; 17+ messages in thread
From: gux.fnst @ 2014-04-09 3:46 UTC (permalink / raw)
To: ltp-list@lists.sourceforge.net
Create a function tst_path_has_mnt_flags() for checking whether
a path is on a system that is mounted with specified flags.
Signed-off-by: Xing Gu <gux.fnst@cn.fujitsu.com>
---
include/test.h | 9 +++++
lib/tst_path_has_mnt_flags.c | 79 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 88 insertions(+)
create mode 100644 lib/tst_path_has_mnt_flags.c
diff --git a/include/test.h b/include/test.h
index baa3ef6..39bc5a1 100644
--- a/include/test.h
+++ b/include/test.h
@@ -295,6 +295,15 @@ gid_t tst_get_unused_gid(void);
unsigned short tst_get_unused_port(void (cleanup_fn)(void),
unsigned short family, int type);
+/* lib/tst_path_has_mnt_flags.c
+ *
+ * Check whether a path is on a filesystem that is mounted with
+ * specified flags.
+ * @path: path to file
+ * @flags: mount flags
+ */
+int tst_path_has_mnt_flags(const char *path, const char *flags[]);
+
#ifdef TST_USE_COMPAT16_SYSCALL
#define TCID_BIT_SUFFIX "_16"
#elif TST_USE_NEWER64_SYSCALL
diff --git a/lib/tst_path_has_mnt_flags.c b/lib/tst_path_has_mnt_flags.c
new file mode 100644
index 0000000..abd71b0
--- /dev/null
+++ b/lib/tst_path_has_mnt_flags.c
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2014 Fujitsu Ltd.
+ * Author: Xing Gu <gux.fnst@cn.fujitsu.com>
+ *
+ * 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.
+ *
+ * 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.
+ */
+
+#include <unistd.h>
+#include <mntent.h>
+#include "test.h"
+
+/*
+ * Check whether a path is on a filesystem that is mounted with
+ * specified flags.
+ *
+ * Returns:
+ * -1 - an error has occurred
+ * 0 - the filesystem does not have any specified flags
+ * 1 - the filesystem has at least one flag matched
+ */
+int tst_path_has_mnt_flags(const char *path, const char *flags[])
+{
+ struct mntent *mnt;
+ size_t prefix_max = 0, prefix_len;
+ int has_flags = 0, has_opt;
+ FILE *f;
+ int i;
+
+ if (path == NULL) {
+ printf("The path is NULL.\n");
+ return -1;
+ }
+
+ if (access(path, F_OK) == -1) {
+ printf("The path %s doesn't exist.\n", path);
+ return -1;
+ }
+
+ f = setmntent("/proc/mounts", "r");
+
+ if (f == NULL) {
+ printf("Couldn't mount /proc/mounts.\n");
+ return -1;
+ }
+
+ while ((mnt = getmntent(f))) {
+ /* ignore all pseudo fs */
+ if (mnt->mnt_fsname[0] != '/')
+ continue;
+
+ prefix_len = strlen(mnt->mnt_dir);
+
+ if (strncmp(path, mnt->mnt_dir, prefix_len) == 0
+ && prefix_len > prefix_max) {
+ prefix_max = prefix_len;
+ has_opt = 0;
+ i = 0;
+ while ((has_opt == 0) && (flags[i] != NULL)) {
+ has_opt = hasmntopt(mnt, flags[i]) != NULL;
+ i++;
+ }
+ has_flags = has_opt;
+ }
+ }
+
+ endmntent(f);
+
+ return has_flags;
+}
--
1.8.3.1
------------------------------------------------------------------------------
Put Bad Developers to Shame
Dominate Development with Jenkins Continuous Integration
Continuously Automate Build, Test & Deployment
Start a new project now. Try Jenkins in the cloud.
http://p.sf.net/sfu/13600_Cloudbees
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply related [flat|nested] 17+ messages in thread
* Re: [LTP] [PATCH v3] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags()
2014-04-09 3:46 ` [LTP] [PATCH v3] " gux.fnst
@ 2014-04-09 10:34 ` Jan Stancek
2014-04-09 12:52 ` chrubis
2014-04-11 6:22 ` gux.fnst
0 siblings, 2 replies; 17+ messages in thread
From: Jan Stancek @ 2014-04-09 10:34 UTC (permalink / raw)
To: gux fnst; +Cc: ltp-list
----- Original Message -----
> From: "gux fnst" <gux.fnst@cn.fujitsu.com>
> To: ltp-list@lists.sourceforge.net
> Sent: Wednesday, 9 April, 2014 5:46:34 AM
> Subject: [LTP] [PATCH v3] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags()
>
> Create a function tst_path_has_mnt_flags() for checking whether
> a path is on a system that is mounted with specified flags.
>
> Signed-off-by: Xing Gu <gux.fnst@cn.fujitsu.com>
> ---
> include/test.h | 9 +++++
> lib/tst_path_has_mnt_flags.c | 79
> ++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 88 insertions(+)
> create mode 100644 lib/tst_path_has_mnt_flags.c
>
> diff --git a/include/test.h b/include/test.h
> index baa3ef6..39bc5a1 100644
> --- a/include/test.h
> +++ b/include/test.h
> @@ -295,6 +295,15 @@ gid_t tst_get_unused_gid(void);
> unsigned short tst_get_unused_port(void (cleanup_fn)(void),
> unsigned short family, int type);
>
> +/* lib/tst_path_has_mnt_flags.c
> + *
> + * Check whether a path is on a filesystem that is mounted with
> + * specified flags.
> + * @path: path to file
> + * @flags: mount flags
> + */
> +int tst_path_has_mnt_flags(const char *path, const char *flags[]);
> +
> #ifdef TST_USE_COMPAT16_SYSCALL
> #define TCID_BIT_SUFFIX "_16"
> #elif TST_USE_NEWER64_SYSCALL
> diff --git a/lib/tst_path_has_mnt_flags.c b/lib/tst_path_has_mnt_flags.c
> new file mode 100644
> index 0000000..abd71b0
> --- /dev/null
> +++ b/lib/tst_path_has_mnt_flags.c
> @@ -0,0 +1,79 @@
> +/*
> + * Copyright (c) 2014 Fujitsu Ltd.
> + * Author: Xing Gu <gux.fnst@cn.fujitsu.com>
> + *
> + * 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.
> + *
> + * 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.
> + */
> +
> +#include <unistd.h>
> +#include <mntent.h>
> +#include "test.h"
Hi,
Minor nit here, I'd also add includes for printf and strlen:
+#include <stdio.h>
+#include <string.h>
> +
> +/*
> + * Check whether a path is on a filesystem that is mounted with
> + * specified flags.
> + *
> + * Returns:
> + * -1 - an error has occurred
> + * 0 - the filesystem does not have any specified flags
> + * 1 - the filesystem has at least one flag matched
> + */
> +int tst_path_has_mnt_flags(const char *path, const char *flags[])
> +{
> + struct mntent *mnt;
> + size_t prefix_max = 0, prefix_len;
> + int has_flags = 0, has_opt;
> + FILE *f;
> + int i;
> +
> + if (path == NULL) {
> + printf("The path is NULL.\n");
> + return -1;
> + }
> +
> + if (access(path, F_OK) == -1) {
> + printf("The path %s doesn't exist.\n", path);
> + return -1;
> + }
> +
> + f = setmntent("/proc/mounts", "r");
> +
> + if (f == NULL) {
> + printf("Couldn't mount /proc/mounts.\n");
> + return -1;
> + }
> +
> + while ((mnt = getmntent(f))) {
> + /* ignore all pseudo fs */
> + if (mnt->mnt_fsname[0] != '/')
> + continue;
> +
> + prefix_len = strlen(mnt->mnt_dir);
> +
> + if (strncmp(path, mnt->mnt_dir, prefix_len) == 0
> + && prefix_len > prefix_max) {
> + prefix_max = prefix_len;
> + has_opt = 0;
> + i = 0;
> + while ((has_opt == 0) && (flags[i] != NULL)) {
> + has_opt = hasmntopt(mnt, flags[i]) != NULL;
> + i++;
> + }
> + has_flags = has_opt;
> + }
> + }
> +
> + endmntent(f);
> +
> + return has_flags;
> +}
This function looks OK for one testcase that needs to check for
OR logic between flags. I was thinking, whether we should change
this function to return number matched flags. That way we could
also check easily for AND logic (matches all flags) if we need
that in future.
* Returns:
* -1 - an error has occurred
* 0 - the filesystem does not have any specified flags
- * 1 - the filesystem has at least one flag matched
+ * 1..n - number of flags matched
*/
int tst_path_has_mnt_flags(const char *path, const char *flags[])
{
struct mntent *mnt;
size_t prefix_max = 0, prefix_len;
- int has_flags = 0, has_opt;
+ int flags_matched = 0;
FILE *f;
int i;
@@ -63,17 +65,24 @@ int tst_path_has_mnt_flags(const char *path, const char *flags[])
if (strncmp(path, mnt->mnt_dir, prefix_len) == 0
&& prefix_len > prefix_max) {
prefix_max = prefix_len;
- has_opt = 0;
+ flags_matched = 0;
i = 0;
- while ((has_opt == 0) && (flags[i] != NULL)) {
- has_opt = hasmntopt(mnt, flags[i]) != NULL;
+ while (flags[i] != NULL) {
+ if (hasmntopt(mnt, flags[i]) != NULL)
+ flags_matched++;
i++;
}
- has_flags = has_opt;
}
}
endmntent(f);
- return has_flags;
+ return flags_matched;
+}
What do you think?
Regards,
Jan
> --
> 1.8.3.1
> ------------------------------------------------------------------------------
> Put Bad Developers to Shame
> Dominate Development with Jenkins Continuous Integration
> Continuously Automate Build, Test & Deployment
> Start a new project now. Try Jenkins in the cloud.
> http://p.sf.net/sfu/13600_Cloudbees
> _______________________________________________
> Ltp-list mailing list
> Ltp-list@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/ltp-list
>
------------------------------------------------------------------------------
Put Bad Developers to Shame
Dominate Development with Jenkins Continuous Integration
Continuously Automate Build, Test & Deployment
Start a new project now. Try Jenkins in the cloud.
http://p.sf.net/sfu/13600_Cloudbees
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [LTP] [PATCH v3] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags()
2014-04-09 10:34 ` Jan Stancek
@ 2014-04-09 12:52 ` chrubis
2014-04-11 6:22 ` gux.fnst
1 sibling, 0 replies; 17+ messages in thread
From: chrubis @ 2014-04-09 12:52 UTC (permalink / raw)
To: Jan Stancek; +Cc: ltp-list
Hi!
> This function looks OK for one testcase that needs to check for
> OR logic between flags. I was thinking, whether we should change
> this function to return number matched flags. That way we could
> also check easily for AND logic (matches all flags) if we need
> that in future.
I was going to suggest this as well.
--
Cyril Hrubis
chrubis@suse.cz
------------------------------------------------------------------------------
Put Bad Developers to Shame
Dominate Development with Jenkins Continuous Integration
Continuously Automate Build, Test & Deployment
Start a new project now. Try Jenkins in the cloud.
http://p.sf.net/sfu/13600_Cloudbees
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [LTP] [PATCH v3] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags()
2014-04-09 10:34 ` Jan Stancek
2014-04-09 12:52 ` chrubis
@ 2014-04-11 6:22 ` gux.fnst
1 sibling, 0 replies; 17+ messages in thread
From: gux.fnst @ 2014-04-11 6:22 UTC (permalink / raw)
To: Jan Stancek; +Cc: ltp-list
[-- Attachment #1.1: Type: text/plain, Size: 3524 bytes --]
On 04/09/2014 06:34 PM, Jan Stancek wrote:
>
>
>
> ----- Original Message -----
>> From: "gux fnst" <gux.fnst@cn.fujitsu.com>
>> To: ltp-list@lists.sourceforge.net
>> Sent: Wednesday, 9 April, 2014 5:46:34 AM
>> Subject: [LTP] [PATCH v3] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags()
>>
>> Create a function tst_path_has_mnt_flags() for checking whether
>> a path is on a system that is mounted with specified flags.
>>
>> Signed-off-by: Xing Gu <gux.fnst@cn.fujitsu.com>
>> ---
>> include/test.h | 9 +++++
>> lib/tst_path_has_mnt_flags.c | 79
>> ++++++++++++++++++++++++++++++++++++++++++++
>> 2 files changed, 88 insertions(+)
>>
>> +
>> + prefix_len = strlen(mnt->mnt_dir);
>> +
>> + if (strncmp(path, mnt->mnt_dir, prefix_len) == 0
>> + && prefix_len > prefix_max) {
>> + prefix_max = prefix_len;
>> + has_opt = 0;
>> + i = 0;
>> + while ((has_opt == 0) && (flags[i] != NULL)) {
>> + has_opt = hasmntopt(mnt, flags[i]) != NULL;
>> + i++;
>> + }
>> + has_flags = has_opt;
>> + }
>> + }
>> +
>> + endmntent(f);
>> +
>> + return has_flags;
>> +}
> This function looks OK for one testcase that needs to check for
> OR logic between flags. I was thinking, whether we should change
> this function to return number matched flags. That way we could
> also check easily for AND logic (matches all flags) if we need
> that in future.
>
>
> * Returns:
> * -1 - an error has occurred
> * 0 - the filesystem does not have any specified flags
> - * 1 - the filesystem has at least one flag matched
> + * 1..n - number of flags matched
> */
> int tst_path_has_mnt_flags(const char *path, const char *flags[])
> {
> struct mntent *mnt;
> size_t prefix_max = 0, prefix_len;
> - int has_flags = 0, has_opt;
> + int flags_matched = 0;
> FILE *f;
> int i;
>
> @@ -63,17 +65,24 @@ int tst_path_has_mnt_flags(const char *path, const char *flags[])
> if (strncmp(path, mnt->mnt_dir, prefix_len) == 0
> && prefix_len > prefix_max) {
> prefix_max = prefix_len;
> - has_opt = 0;
> + flags_matched = 0;
> i = 0;
> - while ((has_opt == 0) && (flags[i] != NULL)) {
> - has_opt = hasmntopt(mnt, flags[i]) != NULL;
> + while (flags[i] != NULL) {
> + if (hasmntopt(mnt, flags[i]) != NULL)
> + flags_matched++;
> i++;
> }
> - has_flags = has_opt;
> }
> }
>
> endmntent(f);
>
> - return has_flags;
> + return flags_matched;
> +}
>
> What do you think?
Thanks for your suggestion. It is very helpfull.
I will send a new version today.
Regards,
Xing Gu
>
> Regards,
> Jan
>
>> --
>> 1.8.3.1
>> ------------------------------------------------------------------------------
>> Put Bad Developers to Shame
>> Dominate Development with Jenkins Continuous Integration
>> Continuously Automate Build, Test & Deployment
>> Start a new project now. Try Jenkins in the cloud.
>> http://p.sf.net/sfu/13600_Cloudbees
>> _______________________________________________
>> Ltp-list mailing list
>> Ltp-list@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/ltp-list
>>
> .
>
[-- Attachment #1.2: Type: text/html, Size: 4672 bytes --]
[-- Attachment #2: Type: text/plain, Size: 298 bytes --]
------------------------------------------------------------------------------
Put Bad Developers to Shame
Dominate Development with Jenkins Continuous Integration
Continuously Automate Build, Test & Deployment
Start a new project now. Try Jenkins in the cloud.
http://p.sf.net/sfu/13600_Cloudbees
[-- Attachment #3: Type: text/plain, Size: 155 bytes --]
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply [flat|nested] 17+ messages in thread
* [LTP] [PATCH v4 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags()
2014-04-03 12:00 [LTP] [PATCH v2 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags() gux.fnst
` (2 preceding siblings ...)
2014-04-09 3:46 ` [LTP] [PATCH v3] " gux.fnst
@ 2014-04-11 7:54 ` gux.fnst
2014-04-11 7:54 ` [LTP] [PATCH v4 2/2] openat/openat02.c: add a new case to test flags gux.fnst
2014-05-06 17:26 ` [LTP] [PATCH v4 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags() chrubis
2014-05-08 9:50 ` [LTP] [PATCH v5 " Xing Gu
4 siblings, 2 replies; 17+ messages in thread
From: gux.fnst @ 2014-04-11 7:54 UTC (permalink / raw)
To: ltp-list@lists.sourceforge.net
Create a function tst_path_has_mnt_flags() for checking whether
a path is on a system that is mounted with specified flags.
Signed-off-by: Xing Gu <gux.fnst@cn.fujitsu.com>
---
include/test.h | 9 +++++
lib/tst_path_has_mnt_flags.c | 81 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 90 insertions(+)
create mode 100644 lib/tst_path_has_mnt_flags.c
diff --git a/include/test.h b/include/test.h
index f5c602b..a4bee73 100644
--- a/include/test.h
+++ b/include/test.h
@@ -288,6 +288,15 @@ int tst_fill_file(const char *path, char pattern, size_t bs, size_t bcount);
unsigned short tst_get_unused_port(void (cleanup_fn)(void),
unsigned short family, int type);
+/* lib/tst_path_has_mnt_flags.c
+ *
+ * Check whether a path is on a filesystem that is mounted with
+ * specified flags.
+ * @path: path to file
+ * @flags: mount flags
+ */
+int tst_path_has_mnt_flags(const char *path, const char *flags[]);
+
#ifdef TST_USE_COMPAT16_SYSCALL
#define TCID_BIT_SUFFIX "_16"
#elif TST_USE_NEWER64_SYSCALL
diff --git a/lib/tst_path_has_mnt_flags.c b/lib/tst_path_has_mnt_flags.c
new file mode 100644
index 0000000..92c7494
--- /dev/null
+++ b/lib/tst_path_has_mnt_flags.c
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2014 Fujitsu Ltd.
+ * Author: Xing Gu <gux.fnst@cn.fujitsu.com>
+ *
+ * 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.
+ *
+ * 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.
+ */
+
+#include <unistd.h>
+#include <mntent.h>
+#include <stdio.h>
+#include <string.h>
+#include "test.h"
+
+/*
+ * Check whether a path is on a filesystem that is mounted with
+ * specified flags.
+ *
+ * Returns:
+ * -1 - an error has occurred
+ * 0 - the filesystem does not have any specified flags
+ * 1..n - number of flags matched
+ */
+int tst_path_has_mnt_flags(const char *path, const char *flags[])
+{
+ struct mntent *mnt;
+ size_t prefix_max = 0, prefix_len;
+ int flags_matched = 0;
+ FILE *f;
+ int i;
+
+ if (path == NULL) {
+ printf("The path is NULL.\n");
+ return -1;
+ }
+
+ if (access(path, F_OK) == -1) {
+ printf("The path %s doesn't exist.\n", path);
+ return -1;
+ }
+
+ f = setmntent("/proc/mounts", "r");
+
+ if (f == NULL) {
+ printf("Couldn't mount /proc/mounts.\n");
+ return -1;
+ }
+
+ while ((mnt = getmntent(f))) {
+ /* ignore all pseudo fs */
+ if (mnt->mnt_fsname[0] != '/')
+ continue;
+
+ prefix_len = strlen(mnt->mnt_dir);
+
+ if (strncmp(path, mnt->mnt_dir, prefix_len) == 0
+ && prefix_len > prefix_max) {
+ prefix_max = prefix_len;
+ flags_matched = 0;
+ i = 0;
+ while (flags[i] != NULL) {
+ if (hasmntopt(mnt, flags[i]) != NULL)
+ flags_matched++;
+ i++;
+ }
+ }
+ }
+
+ endmntent(f);
+
+ return flags_matched;
+}
--
1.8.3.1
------------------------------------------------------------------------------
Put Bad Developers to Shame
Dominate Development with Jenkins Continuous Integration
Continuously Automate Build, Test & Deployment
Start a new project now. Try Jenkins in the cloud.
http://p.sf.net/sfu/13600_Cloudbees
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [LTP] [PATCH v4 2/2] openat/openat02.c: add a new case to test flags
2014-04-11 7:54 ` [LTP] [PATCH v4 1/2] " gux.fnst
@ 2014-04-11 7:54 ` gux.fnst
2014-05-14 15:40 ` chrubis
2014-05-06 17:26 ` [LTP] [PATCH v4 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags() chrubis
1 sibling, 1 reply; 17+ messages in thread
From: gux.fnst @ 2014-04-11 7:54 UTC (permalink / raw)
To: ltp-list@lists.sourceforge.net
From: "gux.fnst@cn.fujitsu.com" <gux.fnst@cn.fujitsu.com>
create a new case to test flags for openat(2):
O_APPEND
O_CLOEXEC
O_LARGEFILE
O_NOATIME
O_NOFOLLOW
O_TRUNC
Signed-off-by: Xing Gu <gux.fnst@cn.fujitsu.com>
---
configure.ac | 1 +
m4/ltp-openat.m4 | 25 ++
runtest/syscalls | 1 +
testcases/kernel/syscalls/.gitignore | 1 +
testcases/kernel/syscalls/openat/openat.h | 35 +++
testcases/kernel/syscalls/openat/openat01.c | 13 +-
testcases/kernel/syscalls/openat/openat02.c | 310 ++++++++++++++++++++++
testcases/kernel/syscalls/openat/openat02_child.c | 41 +++
8 files changed, 418 insertions(+), 9 deletions(-)
create mode 100644 m4/ltp-openat.m4
create mode 100644 testcases/kernel/syscalls/openat/openat.h
create mode 100644 testcases/kernel/syscalls/openat/openat02.c
create mode 100644 testcases/kernel/syscalls/openat/openat02_child.c
diff --git a/configure.ac b/configure.ac
index 44016e7..4e8f1c6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -175,5 +175,6 @@ LTP_CHECK_FCHOWNAT
LTP_CHECK_MKNODAT
LTP_CHECK_FALLOCATE
LTP_CHECK_SYSCALL_FCNTL
+LTP_CHECK_OPENAT
AC_OUTPUT
diff --git a/m4/ltp-openat.m4 b/m4/ltp-openat.m4
new file mode 100644
index 0000000..aa3b0a3
--- /dev/null
+++ b/m4/ltp-openat.m4
@@ -0,0 +1,25 @@
+dnl
+dnl Copyright (c) Linux Test Project, 2014
+dnl
+dnl This program is free software; you can redistribute it and/or modify
+dnl it under the terms of the GNU General Public License as published by
+dnl the Free Software Foundation; either version 2 of the License, or
+dnl (at your option) any later version.
+dnl
+dnl This program is distributed in the hope that it will be useful,
+dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
+dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
+dnl the GNU General Public License for more details.
+dnl
+dnl You should have received a copy of the GNU General Public License
+dnl along with this program; if not, write to the Free Software
+dnl Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+dnl
+
+dnl
+dnl LTP_CHECK_OPENAT
+dnl ----------------------------
+dnl
+AC_DEFUN([LTP_CHECK_OPENAT],[
+AC_CHECK_FUNCS(openat,,)
+])
diff --git a/runtest/syscalls b/runtest/syscalls
index fb3e59f..a775e52 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -710,6 +710,7 @@ open11 open11
#openat test cases
openat01 openat01
+openat02 openat02
mincore01 mincore01
mincore02 mincore02
diff --git a/testcases/kernel/syscalls/.gitignore b/testcases/kernel/syscalls/.gitignore
index d5c7bac..90a47b7 100644
--- a/testcases/kernel/syscalls/.gitignore
+++ b/testcases/kernel/syscalls/.gitignore
@@ -602,6 +602,7 @@
/open/open10
/open/open11
/openat/openat01
+/openat/openat02
/pathconf/pathconf01
/pause/pause01
/pause/pause02
diff --git a/testcases/kernel/syscalls/openat/openat.h b/testcases/kernel/syscalls/openat/openat.h
new file mode 100644
index 0000000..fce4e3f
--- /dev/null
+++ b/testcases/kernel/syscalls/openat/openat.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) International Business Machines Corp., 2007
+ * Copyright (c) 2014 Fujitsu Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#ifndef OPENAT_H
+#define OPENAT_H
+
+#include <sys/types.h>
+#include "config.h"
+#include "linux_syscall_numbers.h"
+
+#if !defined(HAVE_OPENAT)
+int openat(int dirfd, const char *pathname, int flags, mode_t mode)
+{
+ return ltp_syscall(__NR_openat, dirfd, pathname, flags, mode);
+}
+#endif
+
+#endif /* OPENAT_H */
diff --git a/testcases/kernel/syscalls/openat/openat01.c b/testcases/kernel/syscalls/openat/openat01.c
index d8c8e79..a1ec6bf 100644
--- a/testcases/kernel/syscalls/openat/openat01.c
+++ b/testcases/kernel/syscalls/openat/openat01.c
@@ -55,11 +55,11 @@
#include "test.h"
#include "usctest.h"
#include "linux_syscall_numbers.h"
+#include "lapi/fcntl.h"
+#include "openat.h"
#define TEST_CASES 5
-#ifndef AT_FDCWD
-#define AT_FDCWD -100
-#endif
+
void setup();
void cleanup();
void setup_every_copy();
@@ -75,11 +75,6 @@ int fds[TEST_CASES];
char *filenames[TEST_CASES];
int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, 0 };
-int myopenat(int dirfd, const char *filename, int flags, int mode)
-{
- return ltp_syscall(__NR_openat, dirfd, filename, flags, mode);
-}
-
int main(int ac, char **av)
{
int lc;
@@ -116,7 +111,7 @@ int main(int ac, char **av)
* Call openat
*/
for (i = 0; i < TST_TOTAL; i++) {
- TEST(myopenat
+ TEST(openat
(fds[i], filenames[i], O_CREAT | O_WRONLY, 0600));
/* check return code */
diff --git a/testcases/kernel/syscalls/openat/openat02.c b/testcases/kernel/syscalls/openat/openat02.c
new file mode 100644
index 0000000..e43b980
--- /dev/null
+++ b/testcases/kernel/syscalls/openat/openat02.c
@@ -0,0 +1,310 @@
+/*
+ * Copyright (c) 2014 Fujitsu Ltd.
+ * Author: Xing Gu <gux.fnst@cn.fujitsu.com>
+ *
+ * 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.
+ *
+ * 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.
+ */
+/*
+ * Description:
+ * Verify that,
+ * 1)openat() succeeds to open a file in append mode, when
+ * 'flags' is set to O_APPEND.
+ * 2)openat() succeeds to enable the close-on-exec flag for a
+ * file descriptor, when 'flags' is set to O_CLOEXEC.
+ * 3)openat() succeeds to allow files whose sizes cannot be
+ * represented in an off_t but can be represented in an off64_t
+ * to be opened, when 'flags' is set to O_LARGEFILE.
+ * 4)openat() succeeds to not update the file last access time
+ * (st_atime in the inode) when the file is read, when 'flags'
+ * is set to O_NOATIME.
+ * 5)openat() succeeds to open the file failed if the file is a
+ * symbolic link, when 'flags' is set to O_NOFOLLOW.
+ * 6)openat() succeeds to truncate the file to length 0 if the file
+ * already exists and is a regular file and the open mode allows
+ * writing, when 'flags' is set to O_TRUNC.
+ */
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <mntent.h>
+
+#include "test.h"
+#include "usctest.h"
+#include "safe_macros.h"
+#include "lapi/fcntl.h"
+#include "openat.h"
+
+#define TEST_APP "openat02_child"
+
+#define TEST_FILE "test_file"
+#define SFILE "sfile"
+#define LARGE_FILE "large_file"
+
+#define STR "abcdefg"
+
+static void setup(void);
+static void cleanup(void);
+
+static void testfunc_append(void);
+static void testfunc_cloexec(void);
+static void testfunc_largefile(void);
+static void testfunc_noatime(void);
+static void testfunc_nofollow(void);
+static void testfunc_trunc(void);
+
+static void (*testfunc[])(void) = {
+ testfunc_append,
+ testfunc_cloexec,
+ testfunc_largefile,
+ testfunc_noatime,
+ testfunc_nofollow,
+ testfunc_trunc,
+};
+
+char *TCID = "openat02";
+int TST_TOTAL = ARRAY_SIZE(testfunc);
+
+int main(int ac, char **av)
+{
+ int lc;
+ int i;
+ char *msg;
+
+ msg = parse_opts(ac, av, NULL, NULL);
+ if (msg != NULL)
+ tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
+
+ setup();
+
+ for (lc = 0; TEST_LOOPING(lc); lc++) {
+ tst_count = 0;
+
+ for (i = 0; i < TST_TOTAL; i++)
+ (*testfunc[i])();
+ }
+
+ cleanup();
+ tst_exit();
+}
+
+void setup(void)
+{
+ tst_require_root(NULL);
+
+ TEST_PAUSE;
+
+ tst_sig(FORK, DEF_HANDLER, cleanup);
+
+ tst_tmpdir();
+
+ SAFE_FILE_PRINTF(cleanup, TEST_FILE, "test file");
+
+ SAFE_SYMLINK(cleanup, TEST_FILE, SFILE);
+}
+
+void testfunc_append(void)
+{
+ off_t file_offset;
+
+ SAFE_FILE_PRINTF(cleanup, TEST_FILE, "test file");
+
+ TEST(openat(AT_FDCWD, TEST_FILE, O_APPEND | O_RDWR, 0777));
+
+ if (TEST_RETURN == -1) {
+ tst_resm(TFAIL | TTERRNO, "openat failed");
+ return;
+ }
+
+ SAFE_WRITE(cleanup, 1, TEST_RETURN, STR, sizeof(STR) - 1);
+
+ file_offset = SAFE_LSEEK(cleanup, TEST_RETURN, 0, SEEK_CUR);
+
+ if (file_offset > (off_t)(sizeof(STR) - 1))
+ tst_resm(TPASS, "test O_APPEND for openat success");
+ else
+ tst_resm(TFAIL, "test O_APPEND for openat failed");
+
+ SAFE_CLOSE(cleanup, TEST_RETURN);
+}
+
+void testfunc_cloexec(void)
+{
+ pid_t pid;
+ int status;
+ char buf[20];
+
+ if ((tst_kvercmp(2, 6, 23)) < 0) {
+ tst_resm(TCONF, "test O_CLOEXEC flags for openat "
+ "needs kernel 2.6.23 or higher");
+ return;
+ }
+
+ TEST(openat(AT_FDCWD, TEST_FILE, O_CLOEXEC | O_RDWR, 0777));
+
+ if (TEST_RETURN == -1) {
+ tst_resm(TFAIL | TTERRNO, "openat failed");
+ return;
+ }
+
+ sprintf(buf, "%ld", TEST_RETURN);
+
+ pid = tst_fork();
+
+ if (pid < 0)
+ tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
+
+ if (pid == 0) {
+ if (execlp(TEST_APP, TEST_APP, buf, NULL))
+ exit(1);
+ }
+
+ SAFE_CLOSE(cleanup, TEST_RETURN);
+
+ if (wait(&status) == -1)
+ tst_brkm(TBROK | TERRNO, cleanup, "wait() failed");
+
+ if (WIFEXITED(status)) {
+ switch ((int8_t)WEXITSTATUS(status)) {
+ case -1:
+ tst_resm(TPASS, "test O_CLOEXEC for openat success");
+ break;
+ case 1:
+ tst_brkm(TBROK, cleanup, "execlp() failed");
+ default:
+ tst_resm(TFAIL, "test O_CLOEXEC for openat failed");
+ }
+ } else {
+ tst_brkm(TBROK, cleanup,
+ "openat2_exec exits with unexpected error");
+ }
+}
+
+void testfunc_largefile(void)
+{
+ int fd;
+ off64_t offset;
+
+ fd = SAFE_OPEN(cleanup, LARGE_FILE,
+ O_LARGEFILE | O_RDWR | O_CREAT, 0777);
+
+ offset = lseek64(fd, 4.1*1024*1024*1024, SEEK_SET);
+ if (offset == -1)
+ tst_brkm(TBROK | TERRNO, cleanup, "lseek64 failed");
+
+ SAFE_WRITE(cleanup, 1, fd, STR, sizeof(STR) - 1);
+
+ SAFE_CLOSE(cleanup, fd);
+
+ TEST(openat(AT_FDCWD, LARGE_FILE, O_LARGEFILE | O_RDONLY, 0777));
+
+ if (TEST_RETURN == -1) {
+ tst_resm(TFAIL, "test O_LARGEFILE for openat failed");
+ } else {
+ tst_resm(TPASS, "test O_LARGEFILE for openat success");
+ SAFE_CLOSE(cleanup, TEST_RETURN);
+ }
+}
+
+void testfunc_noatime(void)
+{
+ struct stat file_stat, file_newstat;
+ char buf;
+ const char *flags[] = {"noatime", "relatime", NULL};
+ int ret;
+
+ if ((tst_kvercmp(2, 6, 8)) < 0) {
+ tst_resm(TCONF, "test O_NOATIME flags for openat "
+ "needs kernel 2.6.8 or higher");
+ return;
+ }
+
+ ret = tst_path_has_mnt_flags(TEST_FILE, flags);
+ if (ret > 0) {
+ tst_resm(TCONF, "test O_NOATIME flag for openat needs "
+ "filesystems which are mounted without "
+ "noatime and relatime");
+ return;
+ } else if (ret == -1) {
+ tst_brkm(TBROK, cleanup, "tst_path_has_mnt_flags failed");
+ }
+
+ SAFE_STAT(cleanup, TEST_FILE, &file_stat);
+
+ sleep(2);
+
+ TEST(openat(AT_FDCWD, TEST_FILE, O_NOATIME | O_RDONLY, 0777));
+
+ if (TEST_RETURN == -1) {
+ tst_resm(TFAIL | TTERRNO, "openat failed");
+ return;
+ }
+
+ SAFE_READ(cleanup, 1, TEST_RETURN, &buf, 1);
+
+ SAFE_CLOSE(cleanup, TEST_RETURN);
+
+ SAFE_STAT(cleanup, TEST_FILE, &file_newstat);
+
+ if (file_stat.st_atime == file_newstat.st_atime)
+ tst_resm(TPASS, "test O_NOATIME for openat success");
+ else
+ tst_resm(TFAIL, "test O_NOATIME for openat failed");
+}
+
+void testfunc_nofollow(void)
+{
+ TEST(openat(AT_FDCWD, SFILE, O_NOFOLLOW | O_RDONLY, 0777));
+
+ if (TEST_RETURN == -1 && TEST_ERRNO == ELOOP) {
+ tst_resm(TPASS, "test O_NOFOLLOW for openat success");
+ } else {
+ tst_resm(TFAIL, "test O_NOFOLLOW for openat failed");
+ SAFE_CLOSE(cleanup, TEST_RETURN);
+ }
+}
+
+void testfunc_trunc(void)
+{
+ struct stat file_stat;
+
+ TEST(openat(AT_FDCWD, TEST_FILE, O_TRUNC | O_RDWR, 0777));
+
+ if (TEST_RETURN == -1) {
+ tst_resm(TFAIL | TTERRNO, "openat failed");
+ return;
+ }
+
+ SAFE_FSTAT(cleanup, TEST_RETURN, &file_stat);
+
+ if (file_stat.st_size == 0)
+ tst_resm(TPASS, "test O_TRUNC for openat success");
+ else
+ tst_resm(TFAIL, "test O_TRUNC for openat failed");
+
+ SAFE_CLOSE(cleanup, TEST_RETURN);
+}
+
+void cleanup(void)
+{
+ TEST_CLEANUP;
+
+ tst_rmdir();
+}
diff --git a/testcases/kernel/syscalls/openat/openat02_child.c b/testcases/kernel/syscalls/openat/openat02_child.c
new file mode 100644
index 0000000..d2af36b
--- /dev/null
+++ b/testcases/kernel/syscalls/openat/openat02_child.c
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2014 Fujitsu Ltd.
+ * Author: Xing Gu <gux.fnst@cn.fujitsu.com>
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+
+#define STR "abc"
+
+char *TCID = "openat02_child";
+
+int main(int argc, char **argv)
+{
+ int fd;
+ int ret;
+
+ if (argc != 2) {
+ fprintf(stderr, "%s <fd>\n", argv[0]);
+ exit(1);
+ }
+
+ fd = atoi(argv[1]);
+ ret = write(fd, STR, sizeof(STR) - 1);
+
+ exit(ret);
+}
--
1.8.3.1
------------------------------------------------------------------------------
Put Bad Developers to Shame
Dominate Development with Jenkins Continuous Integration
Continuously Automate Build, Test & Deployment
Start a new project now. Try Jenkins in the cloud.
http://p.sf.net/sfu/13600_Cloudbees
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply related [flat|nested] 17+ messages in thread
* Re: [LTP] [PATCH v4 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags()
2014-04-11 7:54 ` [LTP] [PATCH v4 1/2] " gux.fnst
2014-04-11 7:54 ` [LTP] [PATCH v4 2/2] openat/openat02.c: add a new case to test flags gux.fnst
@ 2014-05-06 17:26 ` chrubis
1 sibling, 0 replies; 17+ messages in thread
From: chrubis @ 2014-05-06 17:26 UTC (permalink / raw)
To: gux.fnst@cn.fujitsu.com; +Cc: ltp-list@lists.sourceforge.net
Hi!
> +/* lib/tst_path_has_mnt_flags.c
> + *
> + * Check whether a path is on a filesystem that is mounted with
> + * specified flags.
> + * @path: path to file
> + * @flags: mount flags
You should describe that flags is NULL terminated array here.
And add a note about the return value (which is number of flags matched).
> + */
> +int tst_path_has_mnt_flags(const char *path, const char *flags[]);
Looking at the function interface, we may add the clenanup callback
parameter and do tst_brkm(TBROK, ) instead of the return -1. What do you
think?
> #ifdef TST_USE_COMPAT16_SYSCALL
> #define TCID_BIT_SUFFIX "_16"
> #elif TST_USE_NEWER64_SYSCALL
> diff --git a/lib/tst_path_has_mnt_flags.c b/lib/tst_path_has_mnt_flags.c
> new file mode 100644
> index 0000000..92c7494
> --- /dev/null
> +++ b/lib/tst_path_has_mnt_flags.c
> @@ -0,0 +1,81 @@
> +/*
> + * Copyright (c) 2014 Fujitsu Ltd.
> + * Author: Xing Gu <gux.fnst@cn.fujitsu.com>
> + *
> + * 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.
> + *
> + * 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.
> + */
> +
> +#include <unistd.h>
> +#include <mntent.h>
> +#include <stdio.h>
> +#include <string.h>
> +#include "test.h"
> +
> +/*
> + * Check whether a path is on a filesystem that is mounted with
> + * specified flags.
> + *
> + * Returns:
> + * -1 - an error has occurred
> + * 0 - the filesystem does not have any specified flags
> + * 1..n - number of flags matched
> + */
I would omit this comment, it's more important to have this description
in the header file (which is where people look) than here.
> +int tst_path_has_mnt_flags(const char *path, const char *flags[])
> +{
> + struct mntent *mnt;
> + size_t prefix_max = 0, prefix_len;
> + int flags_matched = 0;
> + FILE *f;
> + int i;
> +
> + if (path == NULL) {
> + printf("The path is NULL.\n");
> + return -1;
> + }
> +
> + if (access(path, F_OK) == -1) {
> + printf("The path %s doesn't exist.\n", path);
> + return -1;
> + }
> +
> + f = setmntent("/proc/mounts", "r");
> +
> + if (f == NULL) {
> + printf("Couldn't mount /proc/mounts.\n");
^
open
> + return -1;
> + }
> +
> + while ((mnt = getmntent(f))) {
> + /* ignore all pseudo fs */
> + if (mnt->mnt_fsname[0] != '/')
> + continue;
> +
> + prefix_len = strlen(mnt->mnt_dir);
> +
> + if (strncmp(path, mnt->mnt_dir, prefix_len) == 0
> + && prefix_len > prefix_max) {
> + prefix_max = prefix_len;
> + flags_matched = 0;
> + i = 0;
> + while (flags[i] != NULL) {
> + if (hasmntopt(mnt, flags[i]) != NULL)
> + flags_matched++;
> + i++;
> + }
> + }
> + }
> +
> + endmntent(f);
> +
> + return flags_matched;
> +}
--
Cyril Hrubis
chrubis@suse.cz
------------------------------------------------------------------------------
Is your legacy SCM system holding you back? Join Perforce May 7 to find out:
• 3 signs your SCM is hindering your productivity
• Requirements for releasing software faster
• Expert tips and advice for migrating your SCM now
http://p.sf.net/sfu/perforce
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply [flat|nested] 17+ messages in thread
* [LTP] [PATCH v5 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags()
2014-04-03 12:00 [LTP] [PATCH v2 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags() gux.fnst
` (3 preceding siblings ...)
2014-04-11 7:54 ` [LTP] [PATCH v4 1/2] " gux.fnst
@ 2014-05-08 9:50 ` Xing Gu
2014-05-08 9:50 ` [LTP] [PATCH v5 2/2] openat/openat02.c: add a new case to test flags Xing Gu
2014-05-14 13:15 ` [LTP] [PATCH v5 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags() chrubis
4 siblings, 2 replies; 17+ messages in thread
From: Xing Gu @ 2014-05-08 9:50 UTC (permalink / raw)
To: ltp-list
From: "gux.fnst@cn.fujitsu.com" <gux.fnst@cn.fujitsu.com>
Create a function tst_path_has_mnt_flags() for checking whether
a path is on a system that is mounted with specified flags.
Signed-off-by: Xing Gu <gux.fnst@cn.fujitsu.com>
---
include/test.h | 12 ++++++++
lib/tst_path_has_mnt_flags.c | 71 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 83 insertions(+)
create mode 100644 lib/tst_path_has_mnt_flags.c
diff --git a/include/test.h b/include/test.h
index e32481c..be46674 100644
--- a/include/test.h
+++ b/include/test.h
@@ -302,6 +302,18 @@ int tst_fill_file(const char *path, char pattern, size_t bs, size_t bcount);
unsigned short tst_get_unused_port(void (cleanup_fn)(void),
unsigned short family, int type);
+/* lib/tst_path_has_mnt_flags.c
+ *
+ * Check whether a path is on a filesystem that is mounted with
+ * specified flags
+ * @path: path to file
+ * @flags: NULL or NULL terminated array of mount flags
+ *
+ * Return: 0..n - number of flags matched
+ */
+int tst_path_has_mnt_flags(void (cleanup_fn)(void),
+ const char *path, const char *flags[]);
+
#ifdef TST_USE_COMPAT16_SYSCALL
#define TCID_BIT_SUFFIX "_16"
#elif TST_USE_NEWER64_SYSCALL
diff --git a/lib/tst_path_has_mnt_flags.c b/lib/tst_path_has_mnt_flags.c
new file mode 100644
index 0000000..031e225
--- /dev/null
+++ b/lib/tst_path_has_mnt_flags.c
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2014 Fujitsu Ltd.
+ * Author: Xing Gu <gux.fnst@cn.fujitsu.com>
+ *
+ * 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.
+ *
+ * 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.
+ */
+
+#include <unistd.h>
+#include <mntent.h>
+#include <stdio.h>
+#include <string.h>
+#include "test.h"
+
+/*
+ * Check whether a path is on a filesystem that is mounted with
+ * specified flags.
+ */
+int tst_path_has_mnt_flags(void (cleanup_fn)(void),
+ const char *path, const char *flags[])
+{
+ struct mntent *mnt;
+ size_t prefix_max = 0, prefix_len;
+ int flags_matched = 0;
+ FILE *f;
+ int i;
+
+ if (access(path, F_OK) == -1) {
+ tst_brkm(TBROK | TERRNO, cleanup_fn,
+ "tst_path_has_mnt_flags: path %s doesn't exist", path);
+ }
+
+ f = setmntent("/proc/mounts", "r");
+ if (f == NULL) {
+ tst_brkm(TBROK | TERRNO, cleanup_fn,
+ "tst_path_has_mnt_flags: failed to open /proc/mounts");
+ }
+
+ while ((mnt = getmntent(f))) {
+ /* ignore all pseudo fs */
+ if (mnt->mnt_fsname[0] != '/')
+ continue;
+
+ prefix_len = strlen(mnt->mnt_dir);
+
+ if (strncmp(path, mnt->mnt_dir, prefix_len) == 0
+ && prefix_len > prefix_max) {
+ prefix_max = prefix_len;
+ flags_matched = 0;
+ i = 0;
+ while (flags[i] != NULL) {
+ if (hasmntopt(mnt, flags[i]) != NULL)
+ flags_matched++;
+ i++;
+ }
+ }
+ }
+
+ endmntent(f);
+
+ return flags_matched;
+}
--
1.9.0
------------------------------------------------------------------------------
Is your legacy SCM system holding you back? Join Perforce May 7 to find out:
• 3 signs your SCM is hindering your productivity
• Requirements for releasing software faster
• Expert tips and advice for migrating your SCM now
http://p.sf.net/sfu/perforce
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [LTP] [PATCH v5 2/2] openat/openat02.c: add a new case to test flags
2014-05-08 9:50 ` [LTP] [PATCH v5 " Xing Gu
@ 2014-05-08 9:50 ` Xing Gu
2014-05-14 13:15 ` [LTP] [PATCH v5 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags() chrubis
1 sibling, 0 replies; 17+ messages in thread
From: Xing Gu @ 2014-05-08 9:50 UTC (permalink / raw)
To: ltp-list
From: "gux.fnst@cn.fujitsu.com" <gux.fnst@cn.fujitsu.com>
create a new case to test flags for openat(2):
O_APPEND
O_CLOEXEC
O_LARGEFILE
O_NOATIME
O_NOFOLLOW
O_TRUNC
Signed-off-by: Xing Gu <gux.fnst@cn.fujitsu.com>
---
configure.ac | 1 +
m4/ltp-openat.m4 | 25 ++
runtest/syscalls | 1 +
testcases/kernel/syscalls/.gitignore | 1 +
testcases/kernel/syscalls/openat/openat.h | 35 +++
testcases/kernel/syscalls/openat/openat01.c | 13 +-
testcases/kernel/syscalls/openat/openat02.c | 308 ++++++++++++++++++++++
testcases/kernel/syscalls/openat/openat02_child.c | 41 +++
8 files changed, 416 insertions(+), 9 deletions(-)
create mode 100644 m4/ltp-openat.m4
create mode 100644 testcases/kernel/syscalls/openat/openat.h
create mode 100644 testcases/kernel/syscalls/openat/openat02.c
create mode 100644 testcases/kernel/syscalls/openat/openat02_child.c
diff --git a/configure.ac b/configure.ac
index 8081b06..b509e26 100644
--- a/configure.ac
+++ b/configure.ac
@@ -180,5 +180,6 @@ LTP_CHECK_READLINKAT
LTP_CHECK_FALLOCATE
LTP_CHECK_SYSCALL_FCNTL
LTP_CHECK_TIRPC
+LTP_CHECK_OPENAT
AC_OUTPUT
diff --git a/m4/ltp-openat.m4 b/m4/ltp-openat.m4
new file mode 100644
index 0000000..aa3b0a3
--- /dev/null
+++ b/m4/ltp-openat.m4
@@ -0,0 +1,25 @@
+dnl
+dnl Copyright (c) Linux Test Project, 2014
+dnl
+dnl This program is free software; you can redistribute it and/or modify
+dnl it under the terms of the GNU General Public License as published by
+dnl the Free Software Foundation; either version 2 of the License, or
+dnl (at your option) any later version.
+dnl
+dnl This program is distributed in the hope that it will be useful,
+dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
+dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
+dnl the GNU General Public License for more details.
+dnl
+dnl You should have received a copy of the GNU General Public License
+dnl along with this program; if not, write to the Free Software
+dnl Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+dnl
+
+dnl
+dnl LTP_CHECK_OPENAT
+dnl ----------------------------
+dnl
+AC_DEFUN([LTP_CHECK_OPENAT],[
+AC_CHECK_FUNCS(openat,,)
+])
diff --git a/runtest/syscalls b/runtest/syscalls
index 7a067cd..52d63ba 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -710,6 +710,7 @@ open11 open11
#openat test cases
openat01 openat01
+openat02 openat02
mincore01 mincore01
mincore02 mincore02
diff --git a/testcases/kernel/syscalls/.gitignore b/testcases/kernel/syscalls/.gitignore
index 0a16f74..b1c46de 100644
--- a/testcases/kernel/syscalls/.gitignore
+++ b/testcases/kernel/syscalls/.gitignore
@@ -602,6 +602,7 @@
/open/open10
/open/open11
/openat/openat01
+/openat/openat02
/pathconf/pathconf01
/pause/pause01
/pause/pause02
diff --git a/testcases/kernel/syscalls/openat/openat.h b/testcases/kernel/syscalls/openat/openat.h
new file mode 100644
index 0000000..fce4e3f
--- /dev/null
+++ b/testcases/kernel/syscalls/openat/openat.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) International Business Machines Corp., 2007
+ * Copyright (c) 2014 Fujitsu Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#ifndef OPENAT_H
+#define OPENAT_H
+
+#include <sys/types.h>
+#include "config.h"
+#include "linux_syscall_numbers.h"
+
+#if !defined(HAVE_OPENAT)
+int openat(int dirfd, const char *pathname, int flags, mode_t mode)
+{
+ return ltp_syscall(__NR_openat, dirfd, pathname, flags, mode);
+}
+#endif
+
+#endif /* OPENAT_H */
diff --git a/testcases/kernel/syscalls/openat/openat01.c b/testcases/kernel/syscalls/openat/openat01.c
index d8c8e79..a1ec6bf 100644
--- a/testcases/kernel/syscalls/openat/openat01.c
+++ b/testcases/kernel/syscalls/openat/openat01.c
@@ -55,11 +55,11 @@
#include "test.h"
#include "usctest.h"
#include "linux_syscall_numbers.h"
+#include "lapi/fcntl.h"
+#include "openat.h"
#define TEST_CASES 5
-#ifndef AT_FDCWD
-#define AT_FDCWD -100
-#endif
+
void setup();
void cleanup();
void setup_every_copy();
@@ -75,11 +75,6 @@ int fds[TEST_CASES];
char *filenames[TEST_CASES];
int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, 0 };
-int myopenat(int dirfd, const char *filename, int flags, int mode)
-{
- return ltp_syscall(__NR_openat, dirfd, filename, flags, mode);
-}
-
int main(int ac, char **av)
{
int lc;
@@ -116,7 +111,7 @@ int main(int ac, char **av)
* Call openat
*/
for (i = 0; i < TST_TOTAL; i++) {
- TEST(myopenat
+ TEST(openat
(fds[i], filenames[i], O_CREAT | O_WRONLY, 0600));
/* check return code */
diff --git a/testcases/kernel/syscalls/openat/openat02.c b/testcases/kernel/syscalls/openat/openat02.c
new file mode 100644
index 0000000..68e0879
--- /dev/null
+++ b/testcases/kernel/syscalls/openat/openat02.c
@@ -0,0 +1,308 @@
+/*
+ * Copyright (c) 2014 Fujitsu Ltd.
+ * Author: Xing Gu <gux.fnst@cn.fujitsu.com>
+ *
+ * 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.
+ *
+ * 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.
+ */
+/*
+ * Description:
+ * Verify that,
+ * 1)openat() succeeds to open a file in append mode, when
+ * 'flags' is set to O_APPEND.
+ * 2)openat() succeeds to enable the close-on-exec flag for a
+ * file descriptor, when 'flags' is set to O_CLOEXEC.
+ * 3)openat() succeeds to allow files whose sizes cannot be
+ * represented in an off_t but can be represented in an off64_t
+ * to be opened, when 'flags' is set to O_LARGEFILE.
+ * 4)openat() succeeds to not update the file last access time
+ * (st_atime in the inode) when the file is read, when 'flags'
+ * is set to O_NOATIME.
+ * 5)openat() succeeds to open the file failed if the file is a
+ * symbolic link, when 'flags' is set to O_NOFOLLOW.
+ * 6)openat() succeeds to truncate the file to length 0 if the file
+ * already exists and is a regular file and the open mode allows
+ * writing, when 'flags' is set to O_TRUNC.
+ */
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <mntent.h>
+
+#include "test.h"
+#include "usctest.h"
+#include "safe_macros.h"
+#include "lapi/fcntl.h"
+#include "openat.h"
+
+#define TEST_APP "openat02_child"
+
+#define TEST_FILE "test_file"
+#define SFILE "sfile"
+#define LARGE_FILE "large_file"
+
+#define STR "abcdefg"
+
+static void setup(void);
+static void cleanup(void);
+
+static void testfunc_append(void);
+static void testfunc_cloexec(void);
+static void testfunc_largefile(void);
+static void testfunc_noatime(void);
+static void testfunc_nofollow(void);
+static void testfunc_trunc(void);
+
+static void (*testfunc[])(void) = {
+ testfunc_append,
+ testfunc_cloexec,
+ testfunc_largefile,
+ testfunc_noatime,
+ testfunc_nofollow,
+ testfunc_trunc,
+};
+
+char *TCID = "openat02";
+int TST_TOTAL = ARRAY_SIZE(testfunc);
+
+int main(int ac, char **av)
+{
+ int lc;
+ int i;
+ char *msg;
+
+ msg = parse_opts(ac, av, NULL, NULL);
+ if (msg != NULL)
+ tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
+
+ setup();
+
+ for (lc = 0; TEST_LOOPING(lc); lc++) {
+ tst_count = 0;
+
+ for (i = 0; i < TST_TOTAL; i++)
+ (*testfunc[i])();
+ }
+
+ cleanup();
+ tst_exit();
+}
+
+void setup(void)
+{
+ tst_require_root(NULL);
+
+ TEST_PAUSE;
+
+ tst_sig(FORK, DEF_HANDLER, cleanup);
+
+ tst_tmpdir();
+
+ SAFE_FILE_PRINTF(cleanup, TEST_FILE, "test file");
+
+ SAFE_SYMLINK(cleanup, TEST_FILE, SFILE);
+}
+
+void testfunc_append(void)
+{
+ off_t file_offset;
+
+ SAFE_FILE_PRINTF(cleanup, TEST_FILE, "test file");
+
+ TEST(openat(AT_FDCWD, TEST_FILE, O_APPEND | O_RDWR, 0777));
+
+ if (TEST_RETURN == -1) {
+ tst_resm(TFAIL | TTERRNO, "openat failed");
+ return;
+ }
+
+ SAFE_WRITE(cleanup, 1, TEST_RETURN, STR, sizeof(STR) - 1);
+
+ file_offset = SAFE_LSEEK(cleanup, TEST_RETURN, 0, SEEK_CUR);
+
+ if (file_offset > (off_t)(sizeof(STR) - 1))
+ tst_resm(TPASS, "test O_APPEND for openat success");
+ else
+ tst_resm(TFAIL, "test O_APPEND for openat failed");
+
+ SAFE_CLOSE(cleanup, TEST_RETURN);
+}
+
+void testfunc_cloexec(void)
+{
+ pid_t pid;
+ int status;
+ char buf[20];
+
+ if ((tst_kvercmp(2, 6, 23)) < 0) {
+ tst_resm(TCONF, "test O_CLOEXEC flags for openat "
+ "needs kernel 2.6.23 or higher");
+ return;
+ }
+
+ TEST(openat(AT_FDCWD, TEST_FILE, O_CLOEXEC | O_RDWR, 0777));
+
+ if (TEST_RETURN == -1) {
+ tst_resm(TFAIL | TTERRNO, "openat failed");
+ return;
+ }
+
+ sprintf(buf, "%ld", TEST_RETURN);
+
+ pid = tst_fork();
+
+ if (pid < 0)
+ tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
+
+ if (pid == 0) {
+ if (execlp(TEST_APP, TEST_APP, buf, NULL))
+ exit(1);
+ }
+
+ SAFE_CLOSE(cleanup, TEST_RETURN);
+
+ if (wait(&status) == -1)
+ tst_brkm(TBROK | TERRNO, cleanup, "wait() failed");
+
+ if (WIFEXITED(status)) {
+ switch ((int8_t)WEXITSTATUS(status)) {
+ case -1:
+ tst_resm(TPASS, "test O_CLOEXEC for openat success");
+ break;
+ case 1:
+ tst_brkm(TBROK, cleanup, "execlp() failed");
+ default:
+ tst_resm(TFAIL, "test O_CLOEXEC for openat failed");
+ }
+ } else {
+ tst_brkm(TBROK, cleanup,
+ "openat2_exec exits with unexpected error");
+ }
+}
+
+void testfunc_largefile(void)
+{
+ int fd;
+ off64_t offset;
+
+ fd = SAFE_OPEN(cleanup, LARGE_FILE,
+ O_LARGEFILE | O_RDWR | O_CREAT, 0777);
+
+ offset = lseek64(fd, 4.1*1024*1024*1024, SEEK_SET);
+ if (offset == -1)
+ tst_brkm(TBROK | TERRNO, cleanup, "lseek64 failed");
+
+ SAFE_WRITE(cleanup, 1, fd, STR, sizeof(STR) - 1);
+
+ SAFE_CLOSE(cleanup, fd);
+
+ TEST(openat(AT_FDCWD, LARGE_FILE, O_LARGEFILE | O_RDONLY, 0777));
+
+ if (TEST_RETURN == -1) {
+ tst_resm(TFAIL, "test O_LARGEFILE for openat failed");
+ } else {
+ tst_resm(TPASS, "test O_LARGEFILE for openat success");
+ SAFE_CLOSE(cleanup, TEST_RETURN);
+ }
+}
+
+void testfunc_noatime(void)
+{
+ struct stat file_stat, file_newstat;
+ char buf;
+ const char *flags[] = {"noatime", "relatime", NULL};
+ int ret;
+
+ if ((tst_kvercmp(2, 6, 8)) < 0) {
+ tst_resm(TCONF, "test O_NOATIME flags for openat "
+ "needs kernel 2.6.8 or higher");
+ return;
+ }
+
+ ret = tst_path_has_mnt_flags(cleanup, TEST_FILE, flags);
+ if (ret > 0) {
+ tst_resm(TCONF, "test O_NOATIME flag for openat needs "
+ "filesystems which are mounted without "
+ "noatime and relatime");
+ return;
+ }
+
+ SAFE_STAT(cleanup, TEST_FILE, &file_stat);
+
+ sleep(2);
+
+ TEST(openat(AT_FDCWD, TEST_FILE, O_NOATIME | O_RDONLY, 0777));
+
+ if (TEST_RETURN == -1) {
+ tst_resm(TFAIL | TTERRNO, "openat failed");
+ return;
+ }
+
+ SAFE_READ(cleanup, 1, TEST_RETURN, &buf, 1);
+
+ SAFE_CLOSE(cleanup, TEST_RETURN);
+
+ SAFE_STAT(cleanup, TEST_FILE, &file_newstat);
+
+ if (file_stat.st_atime == file_newstat.st_atime)
+ tst_resm(TPASS, "test O_NOATIME for openat success");
+ else
+ tst_resm(TFAIL, "test O_NOATIME for openat failed");
+}
+
+void testfunc_nofollow(void)
+{
+ TEST(openat(AT_FDCWD, SFILE, O_NOFOLLOW | O_RDONLY, 0777));
+
+ if (TEST_RETURN == -1 && TEST_ERRNO == ELOOP) {
+ tst_resm(TPASS, "test O_NOFOLLOW for openat success");
+ } else {
+ tst_resm(TFAIL, "test O_NOFOLLOW for openat failed");
+ SAFE_CLOSE(cleanup, TEST_RETURN);
+ }
+}
+
+void testfunc_trunc(void)
+{
+ struct stat file_stat;
+
+ TEST(openat(AT_FDCWD, TEST_FILE, O_TRUNC | O_RDWR, 0777));
+
+ if (TEST_RETURN == -1) {
+ tst_resm(TFAIL | TTERRNO, "openat failed");
+ return;
+ }
+
+ SAFE_FSTAT(cleanup, TEST_RETURN, &file_stat);
+
+ if (file_stat.st_size == 0)
+ tst_resm(TPASS, "test O_TRUNC for openat success");
+ else
+ tst_resm(TFAIL, "test O_TRUNC for openat failed");
+
+ SAFE_CLOSE(cleanup, TEST_RETURN);
+}
+
+void cleanup(void)
+{
+ TEST_CLEANUP;
+
+ tst_rmdir();
+}
diff --git a/testcases/kernel/syscalls/openat/openat02_child.c b/testcases/kernel/syscalls/openat/openat02_child.c
new file mode 100644
index 0000000..d2af36b
--- /dev/null
+++ b/testcases/kernel/syscalls/openat/openat02_child.c
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2014 Fujitsu Ltd.
+ * Author: Xing Gu <gux.fnst@cn.fujitsu.com>
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+
+#define STR "abc"
+
+char *TCID = "openat02_child";
+
+int main(int argc, char **argv)
+{
+ int fd;
+ int ret;
+
+ if (argc != 2) {
+ fprintf(stderr, "%s <fd>\n", argv[0]);
+ exit(1);
+ }
+
+ fd = atoi(argv[1]);
+ ret = write(fd, STR, sizeof(STR) - 1);
+
+ exit(ret);
+}
--
1.9.0
------------------------------------------------------------------------------
Is your legacy SCM system holding you back? Join Perforce May 7 to find out:
• 3 signs your SCM is hindering your productivity
• Requirements for releasing software faster
• Expert tips and advice for migrating your SCM now
http://p.sf.net/sfu/perforce
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply related [flat|nested] 17+ messages in thread
* Re: [LTP] [PATCH v5 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags()
2014-05-08 9:50 ` [LTP] [PATCH v5 " Xing Gu
2014-05-08 9:50 ` [LTP] [PATCH v5 2/2] openat/openat02.c: add a new case to test flags Xing Gu
@ 2014-05-14 13:15 ` chrubis
2014-05-14 13:35 ` chrubis
1 sibling, 1 reply; 17+ messages in thread
From: chrubis @ 2014-05-14 13:15 UTC (permalink / raw)
To: Xing Gu; +Cc: ltp-list
Hi!
> + while ((mnt = getmntent(f))) {
> + /* ignore all pseudo fs */
> + if (mnt->mnt_fsname[0] != '/')
> + continue;
I've removed this if () because otherwise this code will ignore
tmpfs (which is common for /tmp) and nfs mounts (that starts with ip
address).
Frankly I have no idea why I added this check to the code in the first
place (and I will fix the similar code in open posix testsuite too).
Pushed, thanks.
--
Cyril Hrubis
chrubis@suse.cz
------------------------------------------------------------------------------
"Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
Instantly run your Selenium tests across 300+ browser/OS combos.
Get unparalleled scalability from the best Selenium testing platform available
Simple to use. Nothing to install. Get started now for free."
http://p.sf.net/sfu/SauceLabs
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [LTP] [PATCH v5 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags()
2014-05-14 13:15 ` [LTP] [PATCH v5 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags() chrubis
@ 2014-05-14 13:35 ` chrubis
2014-05-14 13:46 ` chrubis
0 siblings, 1 reply; 17+ messages in thread
From: chrubis @ 2014-05-14 13:35 UTC (permalink / raw)
To: Xing Gu; +Cc: ltp-list
Hi!
> I've removed this if () because otherwise this code will ignore
> tmpfs (which is common for /tmp) and nfs mounts (that starts with ip
> address).
>
> Frankly I have no idea why I added this check to the code in the first
> place (and I will fix the similar code in open posix testsuite too).
And I've remebered, that because there are two records for root fs / in
/proc/mounts and the one that starts with /dev/xyz device has correct
mount flags.
I will change the code to ignore only records with 'rootfs' mnt_fsname.
--
Cyril Hrubis
chrubis@suse.cz
------------------------------------------------------------------------------
"Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
Instantly run your Selenium tests across 300+ browser/OS combos.
Get unparalleled scalability from the best Selenium testing platform available
Simple to use. Nothing to install. Get started now for free."
http://p.sf.net/sfu/SauceLabs
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [LTP] [PATCH v5 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags()
2014-05-14 13:35 ` chrubis
@ 2014-05-14 13:46 ` chrubis
0 siblings, 0 replies; 17+ messages in thread
From: chrubis @ 2014-05-14 13:46 UTC (permalink / raw)
To: Xing Gu; +Cc: ltp-list
Hi!
> > I've removed this if () because otherwise this code will ignore
> > tmpfs (which is common for /tmp) and nfs mounts (that starts with ip
> > address).
> >
> > Frankly I have no idea why I added this check to the code in the first
> > place (and I will fix the similar code in open posix testsuite too).
>
> And I've remebered, that because there are two records for root fs / in
> /proc/mounts and the one that starts with /dev/xyz device has correct
> mount flags.
>
> I will change the code to ignore only records with 'rootfs' mnt_fsname.
I've tested and pushed the fix.
--
Cyril Hrubis
chrubis@suse.cz
------------------------------------------------------------------------------
"Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
Instantly run your Selenium tests across 300+ browser/OS combos.
Get unparalleled scalability from the best Selenium testing platform available
Simple to use. Nothing to install. Get started now for free."
http://p.sf.net/sfu/SauceLabs
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [LTP] [PATCH v4 2/2] openat/openat02.c: add a new case to test flags
2014-04-11 7:54 ` [LTP] [PATCH v4 2/2] openat/openat02.c: add a new case to test flags gux.fnst
@ 2014-05-14 15:40 ` chrubis
0 siblings, 0 replies; 17+ messages in thread
From: chrubis @ 2014-05-14 15:40 UTC (permalink / raw)
To: gux.fnst@cn.fujitsu.com; +Cc: ltp-list@lists.sourceforge.net
Hi!
Pushed with folling chnages (mostly the same as for the open12
testcase), thanks.
diff --git a/testcases/kernel/syscalls/openat/openat02.c b/testcases/kernel/syscalls/openat/openat02.c
index 68e0879..edf71d7 100644
--- a/testcases/kernel/syscalls/openat/openat02.c
+++ b/testcases/kernel/syscalls/openat/openat02.c
@@ -173,7 +173,7 @@ void testfunc_cloexec(void)
if (pid == 0) {
if (execlp(TEST_APP, TEST_APP, buf, NULL))
- exit(1);
+ exit(2);
}
SAFE_CLOSE(cleanup, TEST_RETURN);
@@ -183,13 +183,14 @@ void testfunc_cloexec(void)
if (WIFEXITED(status)) {
switch ((int8_t)WEXITSTATUS(status)) {
- case -1:
+ case 0:
tst_resm(TPASS, "test O_CLOEXEC for openat success");
- break;
+ break;
case 1:
- tst_brkm(TBROK, cleanup, "execlp() failed");
- default:
tst_resm(TFAIL, "test O_CLOEXEC for openat failed");
+ break;
+ default:
+ tst_brkm(TBROK, cleanup, "execlp() failed");
}
} else {
tst_brkm(TBROK, cleanup,
@@ -236,7 +237,7 @@ void testfunc_noatime(void)
return;
}
- ret = tst_path_has_mnt_flags(cleanup, TEST_FILE, flags);
+ ret = tst_path_has_mnt_flags(cleanup, NULL, flags);
if (ret > 0) {
tst_resm(TCONF, "test O_NOATIME flag for openat needs "
"filesystems which are mounted without "
@@ -246,7 +247,7 @@ void testfunc_noatime(void)
SAFE_STAT(cleanup, TEST_FILE, &file_stat);
- sleep(2);
+ sleep(1);
TEST(openat(AT_FDCWD, TEST_FILE, O_NOATIME | O_RDONLY, 0777));
diff --git a/testcases/kernel/syscalls/openat/openat02_child.c b/testcases/kernel/syscalls/openat/openat02_child.c
index d2af36b..34c4b42 100644
--- a/testcases/kernel/syscalls/openat/openat02_child.c
+++ b/testcases/kernel/syscalls/openat/openat02_child.c
@@ -37,5 +37,5 @@ int main(int argc, char **argv)
fd = atoi(argv[1]);
ret = write(fd, STR, sizeof(STR) - 1);
- exit(ret);
+ return ret != -1;
}
--
Cyril Hrubis
chrubis@suse.cz
------------------------------------------------------------------------------
"Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
Instantly run your Selenium tests across 300+ browser/OS combos.
Get unparalleled scalability from the best Selenium testing platform available
Simple to use. Nothing to install. Get started now for free."
http://p.sf.net/sfu/SauceLabs
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply related [flat|nested] 17+ messages in thread
end of thread, other threads:[~2014-05-14 15:40 UTC | newest]
Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-04-03 12:00 [LTP] [PATCH v2 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags() gux.fnst
2014-04-03 12:00 ` [LTP] [PATCH v2 2/2] openat/openat02.c: add a new case to test flags gux.fnst
2014-04-04 10:01 ` [LTP] [PATCH v2 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags() Jan Stancek
2014-04-09 2:26 ` gux.fnst
2014-04-09 3:46 ` [LTP] [PATCH v3] " gux.fnst
2014-04-09 10:34 ` Jan Stancek
2014-04-09 12:52 ` chrubis
2014-04-11 6:22 ` gux.fnst
2014-04-11 7:54 ` [LTP] [PATCH v4 1/2] " gux.fnst
2014-04-11 7:54 ` [LTP] [PATCH v4 2/2] openat/openat02.c: add a new case to test flags gux.fnst
2014-05-14 15:40 ` chrubis
2014-05-06 17:26 ` [LTP] [PATCH v4 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags() chrubis
2014-05-08 9:50 ` [LTP] [PATCH v5 " Xing Gu
2014-05-08 9:50 ` [LTP] [PATCH v5 2/2] openat/openat02.c: add a new case to test flags Xing Gu
2014-05-14 13:15 ` [LTP] [PATCH v5 1/2] lib/tst_path_has_mnt_flags.c: create a function tst_path_has_mnt_flags() chrubis
2014-05-14 13:35 ` chrubis
2014-05-14 13:46 ` chrubis
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox