qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: aladjev.andrew@gmail.com
To: qemu-devel@nongnu.org
Cc: Andrew Aladjev <aladjev.andrew@gmail.com>
Subject: [PATCH 4/4] linux user: added tests for proc myself
Date: Thu, 25 Feb 2021 23:54:48 +0300	[thread overview]
Message-ID: <20210225205448.10624-4-aladjev.andrew@gmail.com> (raw)
In-Reply-To: <20210225205448.10624-1-aladjev.andrew@gmail.com>

From: Andrew Aladjev <aladjev.andrew@gmail.com>

---
 tests/tcg/multiarch/linux-test.c | 85 ++++++++++++++++++++++++++++++++
 1 file changed, 85 insertions(+)

diff --git a/tests/tcg/multiarch/linux-test.c b/tests/tcg/multiarch/linux-test.c
index 96bbad5..4918d45 100644
--- a/tests/tcg/multiarch/linux-test.c
+++ b/tests/tcg/multiarch/linux-test.c
@@ -517,6 +517,89 @@ static void test_shm(void)
     chk_error(shmdt(ptr));
 }
 
+static void test_proc_myself_file(void)
+{
+    int fd1, fd2;
+    char link1[PATH_MAX], link2[PATH_MAX];
+    char buf1[PATH_MAX], buf2[PATH_MAX];
+    int buf1_length, buf2_length;
+
+    /* We can open any file that will always exist. */
+    const char *file_path = "/proc/self/comm";
+
+    char file_realpath[PATH_MAX];
+    if (realpath(file_path, file_realpath) == NULL) {
+        error("proc myself: invalid file");
+    }
+
+    fd1 = chk_error(open(file_path, O_RDONLY));
+    sprintf(link1, "/proc/self/fd/%d", fd1);
+
+    /* Lets try to open same file by first link. */
+    fd2 = chk_error(open(link1, O_RDONLY));
+    sprintf(link2, "/proc/self/fd/%d", fd2);
+
+    /* Two links should point to the same file path. */
+    buf1_length = chk_error(readlink(link1, buf1, PATH_MAX));
+    if (strlen(file_realpath) != buf1_length ||
+        strncmp(file_realpath, buf1, buf1_length) != 0) {
+        error("proc myself: invalid link");
+    }
+    buf2_length = chk_error(readlink(link2, buf2, PATH_MAX));
+    if (strlen(file_realpath) != buf2_length ||
+        strncmp(file_realpath, buf2, buf2_length) != 0) {
+        error("proc myself: invalid link");
+    }
+
+    /* We should be able to read same data from each fd. */
+    buf1_length = chk_error(read(fd1, buf1, PATH_MAX));
+    buf2_length = chk_error(read(fd2, buf2, PATH_MAX));
+    if (buf1_length == 0 || buf1_length != buf2_length ||
+        strncmp(buf1, buf2, buf2_length) != 0) {
+        error("proc myself: invalid file content");
+    }
+
+    chk_error(close(fd2));
+    chk_error(close(fd1));
+}
+
+static void test_proc_myself_exe(void)
+{
+    int fd1, fd2;
+    char link1[PATH_MAX], link2[PATH_MAX];
+    char buf1[PATH_MAX], buf2[PATH_MAX];
+    int buf1_length, buf2_length;
+
+    const char *exe_path = "/proc/self/exe";
+
+    char exe_realpath[PATH_MAX];
+    if (realpath(exe_path, exe_realpath) == NULL) {
+        error("proc myself: invalid exe");
+    }
+
+    fd1 = chk_error(open(exe_path, O_RDONLY));
+    sprintf(link1, "/proc/self/fd/%d", fd1);
+
+    /* Lets try to open link once again. */
+    fd2 = chk_error(open(link1, O_RDONLY));
+    sprintf(link2, "/proc/self/fd/%d", fd2);
+
+    /* Two links should point to the same exe path. */
+    buf1_length = chk_error(readlink(link1, buf1, PATH_MAX));
+    if (strlen(exe_realpath) != buf1_length ||
+        strncmp(exe_realpath, buf1, buf1_length) != 0) {
+        error("proc myself: invalid link");
+    }
+    buf2_length = chk_error(readlink(link2, buf2, PATH_MAX));
+    if (strlen(exe_realpath) != buf2_length ||
+        strncmp(exe_realpath, buf2, buf2_length) != 0) {
+        error("proc myself: invalid link");
+    }
+
+    chk_error(close(fd2));
+    chk_error(close(fd1));
+}
+
 int main(int argc, char **argv)
 {
     test_file();
@@ -532,5 +615,7 @@ int main(int argc, char **argv)
 
     test_signal();
     test_shm();
+    test_proc_myself_file();
+    test_proc_myself_exe();
     return 0;
 }
-- 
2.26.2



      parent reply	other threads:[~2021-02-25 20:59 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-25 20:54 [PATCH 1/4] linux user: make execfd global (like exec path) and keep it open aladjev.andrew
2021-02-25 20:54 ` [PATCH 2/4] linux user: moved is proc functions to separate file aladjev.andrew
2021-02-25 20:54 ` [PATCH 3/4] linux user: refactored is proc myself, added support for fd/<execfd> aladjev.andrew
2021-02-25 20:54 ` aladjev.andrew [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210225205448.10624-4-aladjev.andrew@gmail.com \
    --to=aladjev.andrew@gmail.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).