From patchwork Fri Sep 21 17:23:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 29499 Received: (qmail 116923 invoked by alias); 21 Sep 2018 17:23:36 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 116907 invoked by uid 89); 21 Sep 2018 17:23:35 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS, UNSUBSCRIBE_BODY, URIBL_DBL_ABUSE_MALW autolearn=ham version=3.3.2 spammy=H*r:sk:b97-v6s, sanity, name1, HX-Received:3a2 X-HELO: mail-pl1-f194.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=subject:to:cc:references:from:openpgp:autocrypt:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=I0VA9lW+atVeQ6yv9PitDHewBWo/wkBK2tDJ99xMSjs=; b=ITrDyVE4DxWwlQPJ8ZfoCajTMTOAOIkK7ZkCX6w5JgBfRjW69duciLLQv3tp24LtAX ut8xEwZEtNYqcjcCLwAMdMsFKxopX8EdmlBsXptyhw1/YyG7vflIP0jMV7FkuTZoG0zO DTVPdrvi7MN7HPgHKDuaS9uTh6SNgMt4faib0= Return-Path: Subject: Re: [PATCH 1/2] Use libsupport for tst-spawn.c To: Florian Weimer Cc: libc-alpha@sourceware.org References: <20180919224624.3920-1-adhemerval.zanella@linaro.org> <87efdokapo.fsf@oldenburg.str.redhat.com> <492d339d-384c-8f0a-3816-f5aa96cb41b3@linaro.org> <871s9nf1qb.fsf@oldenburg.str.redhat.com> From: Adhemerval Zanella Openpgp: preference=signencrypt Message-ID: <465629d2-8f70-a082-7f87-049d2aa42f9c@linaro.org> Date: Fri, 21 Sep 2018 10:23:28 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <871s9nf1qb.fsf@oldenburg.str.redhat.com> On 21/09/2018 05:56, Florian Weimer wrote: > * Adhemerval Zanella: > >> No worries, I need to catch up with recent libsupport functionalities. >> Below it is an updated patch, which I think it replaces with better >> libsupport interfaces: > > Thanks. > >> + TEST_COMPARE (lseek (fd2, 0, SEEK_CUR), strlen (fd2string)); > > I assume you could use xlseek here (and in the following code). > >> + TEST_COMPARE_BLOB (fd2string, strlen (fd2string), buf, strlen (buf)); > > Warning: This should use strlen (fd2string), not strlen (buf). As far > as I can tell, buf is not necessarily null-terminated. > >> + TEST_COMPARE_BLOB (fd3string, strlen (fd3string), buf, strlen (buf)); > >> + TEST_COMPARE_BLOB (fd1string, strlen (fd1string), buf, strlen (buf)); > > Same warning applies. > >> + TEST_VERIFY_EXIT ((fd1 = open (name, O_RDONLY)) != -1); > > This is likely a case for xopen. > >> + /* Write something in the files. */ >> + TEST_COMPARE (write (temp_fd1, fd1string, strlen (fd1string)), >> + strlen (fd1string)); >> + TEST_COMPARE (write (temp_fd2, fd2string, strlen (fd2string)), >> + strlen (fd2string)); >> + TEST_COMPARE (write (temp_fd3, fd3string, strlen (fd3string)), >> + strlen (fd3string)); > > You can replace that with xwrite. > >> + /* We want to open the third file. */ >> + TEST_VERIFY_EXIT ((name3_copy = strdup (name3)) != NULL); > > xstrdup. > > We are getting close! Thanks again for the review, below a patch with the points you raised raised: diff --git a/posix/tst-spawn.c b/posix/tst-spawn.c index 7c785c430c..73a8cdc057 100644 --- a/posix/tst-spawn.c +++ b/posix/tst-spawn.c @@ -17,16 +17,20 @@ License along with the GNU C Library; if not, see . */ +#include +#include #include #include #include #include #include #include -#include #include + #include #include +#include +#include /* Nonzero if the program gets called via `exec'. */ @@ -36,16 +40,6 @@ static int restart; #define CMDLINE_OPTIONS \ { "restart", no_argument, &restart, 1 }, -/* Prototype for our test function. */ -extern void do_prepare (int argc, char *argv[]); -extern int do_test (int argc, char *argv[]); - -/* We have a preparation function. */ -#define PREPARE do_prepare - -#include "../test-skeleton.c" - - /* Name of the temporary files. */ static char *name1; static char *name2; @@ -63,19 +57,18 @@ static const char fd3string[] = "This file will be opened"; /* We have a preparation function. */ -void +static void do_prepare (int argc, char *argv[]) { /* We must not open any files in the restart case. */ if (restart) return; - temp_fd1 = create_temp_file ("spawn", &name1); - temp_fd2 = create_temp_file ("spawn", &name2); - temp_fd3 = create_temp_file ("spawn", &name3); - if (temp_fd1 < 0 || temp_fd2 < 0 || temp_fd3 < 0) - exit (1); + TEST_VERIFY_EXIT ((temp_fd1 = create_temp_file ("spawn", &name1)) != -1); + TEST_VERIFY_EXIT ((temp_fd2 = create_temp_file ("spawn", &name2)) != -1); + TEST_VERIFY_EXIT ((temp_fd3 = create_temp_file ("spawn", &name3)) != -1); } +#define PREPARE do_prepare static int @@ -95,64 +88,45 @@ handle_restart (const char *fd1s, const char *fd2s, const char *fd3s, fd4 = atol (fd4s); /* Sanity check. */ - if (fd1 == fd2) - error (EXIT_FAILURE, 0, "value of fd1 and fd2 is the same"); - if (fd1 == fd3) - error (EXIT_FAILURE, 0, "value of fd1 and fd3 is the same"); - if (fd1 == fd4) - error (EXIT_FAILURE, 0, "value of fd1 and fd4 is the same"); - if (fd2 == fd3) - error (EXIT_FAILURE, 0, "value of fd2 and fd3 is the same"); - if (fd2 == fd4) - error (EXIT_FAILURE, 0, "value of fd2 and fd4 is the same"); - if (fd3 == fd4) - error (EXIT_FAILURE, 0, "value of fd3 and fd4 is the same"); + TEST_VERIFY_EXIT (fd1 != fd2); + TEST_VERIFY_EXIT (fd1 != fd3); + TEST_VERIFY_EXIT (fd1 != fd4); + TEST_VERIFY_EXIT (fd2 != fd3); + TEST_VERIFY_EXIT (fd2 != fd4); + TEST_VERIFY_EXIT (fd3 != fd4); /* First the easy part: read from the file descriptor which is supposed to be open. */ - if (lseek (fd2, 0, SEEK_CUR) != strlen (fd2string)) - error (EXIT_FAILURE, errno, "file 2 not in right position"); + TEST_COMPARE (xlseek (fd2, 0, SEEK_CUR), strlen (fd2string)); /* The duped descriptor must have the same position. */ - if (lseek (fd4, 0, SEEK_CUR) != strlen (fd2string)) - error (EXIT_FAILURE, errno, "file 4 not in right position"); - if (lseek (fd2, 0, SEEK_SET) != 0) - error (EXIT_FAILURE, 0, "cannot reset position in file 2"); - if (lseek (fd4, 0, SEEK_CUR) != 0) - error (EXIT_FAILURE, errno, "file 4 not set back, too"); - if (read (fd2, buf, sizeof buf) != strlen (fd2string)) - error (EXIT_FAILURE, 0, "cannot read file 2"); - if (memcmp (fd2string, buf, strlen (fd2string)) != 0) - error (EXIT_FAILURE, 0, "file 2 does not match"); + TEST_COMPARE (xlseek (fd4, 0, SEEK_CUR), strlen (fd2string)); + TEST_COMPARE (xlseek (fd2, 0, SEEK_SET), 0); + TEST_COMPARE (xlseek (fd4, 0, SEEK_CUR), 0); + TEST_COMPARE (read (fd2, buf, sizeof buf), strlen (fd2string)); + TEST_COMPARE_BLOB (fd2string, strlen (fd2string), buf, strlen (fd2string)); /* Now read from the third file. */ - if (read (fd3, buf, sizeof buf) != strlen (fd3string)) - error (EXIT_FAILURE, 0, "cannot read file 3"); - if (memcmp (fd3string, buf, strlen (fd3string)) != 0) - error (EXIT_FAILURE, 0, "file 3 does not match"); + TEST_COMPARE (read (fd3, buf, sizeof buf), strlen (fd3string)); + TEST_COMPARE_BLOB (fd3string, strlen (fd3string), buf, strlen (fd3string)); /* Try to write to the file. This should not be allowed. */ - if (write (fd3, "boo!", 4) != -1 || errno != EBADF) - error (EXIT_FAILURE, 0, "file 3 is writable"); + xwrite (fd3, "boo!", 4); + TEST_COMPARE (errno, EBADF); /* Now try to read the first file. First make sure it is not opened. */ - if (lseek (fd1, 0, SEEK_CUR) != (off_t) -1 || errno != EBADF) - error (EXIT_FAILURE, 0, "file 1 (%d) is not closed", fd1); + TEST_COMPARE (xlseek (fd1, 0, SEEK_CUR), (off_t) -1); + TEST_COMPARE (errno, EBADF); /* Now open the file and read it. */ - fd1 = open (name, O_RDONLY); - if (fd1 == -1) - error (EXIT_FAILURE, errno, - "cannot open first file \"%s\" for verification", name); + fd1 = xopen (name, O_RDONLY, 0600); - if (read (fd1, buf, sizeof buf) != strlen (fd1string)) - error (EXIT_FAILURE, errno, "cannot read file 1"); - if (memcmp (fd1string, buf, strlen (fd1string)) != 0) - error (EXIT_FAILURE, 0, "file 1 does not match"); + TEST_COMPARE (read (fd1, buf, sizeof buf), strlen (fd1string)); + TEST_COMPARE_BLOB (fd1string, strlen (fd1string), buf, strlen (fd2string)); return 0; } -int +static int do_test (int argc, char *argv[]) { pid_t pid; @@ -181,7 +155,7 @@ do_test (int argc, char *argv[]) + the name of the closed descriptor */ if (argc != (restart ? 6 : 2) && argc != (restart ? 6 : 5)) - error (EXIT_FAILURE, 0, "wrong number of arguments (%d)", argc); + FAIL_EXIT1 ("wrong number of arguments (%d)", argc); if (restart) return handle_restart (argv[1], argv[2], argv[3], argv[4], argv[5]); @@ -189,77 +163,73 @@ do_test (int argc, char *argv[]) /* Prepare the test. We are creating two files: one which file descriptor will be marked with FD_CLOEXEC, another which is not. */ - /* Write something in the files. */ - if (write (temp_fd1, fd1string, strlen (fd1string)) != strlen (fd1string)) - error (EXIT_FAILURE, errno, "cannot write to first file"); - if (write (temp_fd2, fd2string, strlen (fd2string)) != strlen (fd2string)) - error (EXIT_FAILURE, errno, "cannot write to second file"); - if (write (temp_fd3, fd3string, strlen (fd3string)) != strlen (fd3string)) - error (EXIT_FAILURE, errno, "cannot write to third file"); - - /* Close the third file. It'll be opened by `spawn'. */ - close (temp_fd3); - - /* Tell `spawn' what to do. */ - if (posix_spawn_file_actions_init (&actions) != 0) - error (EXIT_FAILURE, errno, "posix_spawn_file_actions_init"); - /* Close `temp_fd1'. */ - if (posix_spawn_file_actions_addclose (&actions, temp_fd1) != 0) - error (EXIT_FAILURE, errno, "posix_spawn_file_actions_addclose"); - /* We want to open the third file. */ - name3_copy = strdup (name3); - if (name3_copy == NULL) - error (EXIT_FAILURE, errno, "strdup"); - if (posix_spawn_file_actions_addopen (&actions, temp_fd3, name3_copy, - O_RDONLY, 0666) != 0) - error (EXIT_FAILURE, errno, "posix_spawn_file_actions_addopen"); - /* Overwrite the name to check that a copy has been made. */ - memset (name3_copy, 'X', strlen (name3_copy)); - - /* We dup the second descriptor. */ - fd4 = MAX (2, MAX (temp_fd1, MAX (temp_fd2, temp_fd3))) + 1; - if (posix_spawn_file_actions_adddup2 (&actions, temp_fd2, fd4) != 0) - error (EXIT_FAILURE, errno, "posix_spawn_file_actions_adddup2"); - - /* Now spawn the process. */ - snprintf (fd1name, sizeof fd1name, "%d", temp_fd1); - snprintf (fd2name, sizeof fd2name, "%d", temp_fd2); - snprintf (fd3name, sizeof fd3name, "%d", temp_fd3); - snprintf (fd4name, sizeof fd4name, "%d", fd4); - - for (i = 0; i < (argc == (restart ? 6 : 5) ? 4 : 1); i++) - spargv[i] = argv[i + 1]; - spargv[i++] = (char *) "--direct"; - spargv[i++] = (char *) "--restart"; - spargv[i++] = fd1name; - spargv[i++] = fd2name; - spargv[i++] = fd3name; - spargv[i++] = fd4name; - spargv[i++] = name1; - spargv[i] = NULL; - - if (posix_spawn (&pid, argv[1], &actions, NULL, spargv, environ) != 0) - error (EXIT_FAILURE, errno, "posix_spawn"); - - /* Same test but with a NULL pid argument. */ - if (posix_spawn (NULL, argv[1], &actions, NULL, spargv, environ) != 0) - error (EXIT_FAILURE, errno, "posix_spawn"); - - /* Cleanup. */ - if (posix_spawn_file_actions_destroy (&actions) != 0) - error (EXIT_FAILURE, errno, "posix_spawn_file_actions_destroy"); - free (name3_copy); + /* Write something in the files. */ + xwrite (temp_fd1, fd1string, strlen (fd1string)); + xwrite (temp_fd2, fd2string, strlen (fd2string)); + xwrite (temp_fd3, fd3string, strlen (fd3string)); + + /* Close the third file. It'll be opened by `spawn'. */ + close (temp_fd3); + + /* Tell `spawn' what to do. */ + TEST_COMPARE (posix_spawn_file_actions_init (&actions), 0); + /* Close `temp_fd1'. */ + TEST_COMPARE (posix_spawn_file_actions_addclose (&actions, temp_fd1), 0); + /* We want to open the third file. */ + name3_copy = xstrdup (name3); + TEST_COMPARE (posix_spawn_file_actions_addopen (&actions, temp_fd3, + name3_copy, + O_RDONLY, 0666), + 0); + /* Overwrite the name to check that a copy has been made. */ + memset (name3_copy, 'X', strlen (name3_copy)); + + /* We dup the second descriptor. */ + fd4 = MAX (2, MAX (temp_fd1, MAX (temp_fd2, temp_fd3))) + 1; + TEST_COMPARE (posix_spawn_file_actions_adddup2 (&actions, temp_fd2, fd4), + 0); + + /* Now spawn the process. */ + snprintf (fd1name, sizeof fd1name, "%d", temp_fd1); + snprintf (fd2name, sizeof fd2name, "%d", temp_fd2); + snprintf (fd3name, sizeof fd3name, "%d", temp_fd3); + snprintf (fd4name, sizeof fd4name, "%d", fd4); + + for (i = 0; i < (argc == (restart ? 6 : 5) ? 4 : 1); i++) + spargv[i] = argv[i + 1]; + spargv[i++] = (char *) "--direct"; + spargv[i++] = (char *) "--restart"; + spargv[i++] = fd1name; + spargv[i++] = fd2name; + spargv[i++] = fd3name; + spargv[i++] = fd4name; + spargv[i++] = name1; + spargv[i] = NULL; + + TEST_COMPARE (posix_spawn (&pid, argv[1], &actions, NULL, spargv, environ), + 0); + + /* Same test but with a NULL pid argument. */ + TEST_COMPARE (posix_spawn (NULL, argv[1], &actions, NULL, spargv, environ), + 0); + + /* Cleanup. */ + TEST_COMPARE (posix_spawn_file_actions_destroy (&actions), 0); + free (name3_copy); /* Wait for the children. */ - TEST_VERIFY (xwaitpid (pid, &status, 0) == pid); + TEST_COMPARE (xwaitpid (pid, &status, 0), pid); TEST_VERIFY (WIFEXITED (status)); TEST_VERIFY (!WIFSIGNALED (status)); - TEST_VERIFY (WEXITSTATUS (status) == 0); + TEST_COMPARE (WEXITSTATUS (status), 0); xwaitpid (-1, &status, 0); TEST_VERIFY (WIFEXITED (status)); TEST_VERIFY (!WIFSIGNALED (status)); - TEST_VERIFY (WEXITSTATUS (status) == 0); + TEST_COMPARE (WEXITSTATUS (status), 0); return 0; } + +#define TEST_FUNCTION_ARGV do_test +#include