LCOV - code coverage report
Current view: top level - app/test - process.h (source / functions) Hit Total Coverage
Test: Code coverage Lines: 20 52 38.5 %
Date: 2024-02-14 00:53:57 Functions: 2 2 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 9 40 22.5 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2014 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #ifndef _PROCESS_H_
       6                 :            : #define _PROCESS_H_
       7                 :            : 
       8                 :            : #include <errno.h>  /* errno */
       9                 :            : #include <limits.h> /* PATH_MAX */
      10                 :            : #ifndef RTE_EXEC_ENV_WINDOWS
      11                 :            : #include <libgen.h> /* basename et al */
      12                 :            : #include <sys/wait.h>
      13                 :            : #endif
      14                 :            : #include <stdlib.h> /* NULL */
      15                 :            : #include <string.h> /* strerror */
      16                 :            : #include <unistd.h> /* readlink */
      17                 :            : #include <dirent.h>
      18                 :            : 
      19                 :            : #include <rte_string_fns.h> /* strlcpy */
      20                 :            : 
      21                 :            : #ifdef RTE_EXEC_ENV_FREEBSD
      22                 :            : #define self "curproc"
      23                 :            : #define exe "file"
      24                 :            : #else
      25                 :            : #define self "self"
      26                 :            : #define exe "exe"
      27                 :            : #endif
      28                 :            : 
      29                 :            : #ifdef RTE_LIB_PDUMP
      30                 :            : #ifdef RTE_NET_RING
      31                 :            : #include <rte_thread.h>
      32                 :            : extern uint32_t send_pkts(void *empty);
      33                 :            : extern uint16_t flag_for_send_pkts;
      34                 :            : #endif
      35                 :            : #endif
      36                 :            : 
      37                 :            : /*
      38                 :            :  * launches a second copy of the test process using the given argv parameters,
      39                 :            :  * which should include argv[0] as the process name. To identify in the
      40                 :            :  * subprocess the source of the call, the env_value parameter is set in the
      41                 :            :  * environment as $RTE_TEST
      42                 :            :  */
      43                 :            : static inline int
      44                 :        120 : process_dup(const char *const argv[], int numargs, const char *env_value)
      45                 :        120 : {
      46                 :            :         int num;
      47                 :        120 :         char *argv_cpy[numargs + 1];
      48                 :            :         int i, status;
      49                 :            :         char path[32];
      50                 :            : #ifdef RTE_LIB_PDUMP
      51                 :            : #ifdef RTE_NET_RING
      52                 :            :         rte_thread_t thread;
      53                 :            :         int rc;
      54                 :            : #endif
      55                 :            : #endif
      56                 :            : 
      57                 :        120 :         pid_t pid = fork();
      58         [ +  - ]:        120 :         if (pid < 0)
      59                 :            :                 return -1;
      60         [ -  + ]:        120 :         else if (pid == 0) {
      61                 :            :                 /* make a copy of the arguments to be passed to exec */
      62         [ #  # ]:          0 :                 for (i = 0; i < numargs; i++)
      63                 :          0 :                         argv_cpy[i] = strdup(argv[i]);
      64                 :          0 :                 argv_cpy[i] = NULL;
      65                 :            :                 num = numargs;
      66                 :            : 
      67                 :            : #ifdef RTE_EXEC_ENV_LINUX
      68                 :            :                 {
      69                 :            :                         const char *procdir = "/proc/" self "/fd/";
      70                 :            :                         struct dirent *dirent;
      71                 :            :                         char *endptr;
      72                 :            :                         int fd, fdir;
      73                 :            :                         DIR *dir;
      74                 :            : 
      75                 :            :                         /* close all open file descriptors, check /proc/self/fd
      76                 :            :                          * to only call close on open fds. Exclude fds 0, 1 and
      77                 :            :                          * 2
      78                 :            :                          */
      79                 :          0 :                         dir = opendir(procdir);
      80         [ #  # ]:          0 :                         if (dir == NULL) {
      81                 :          0 :                                 rte_panic("Error opening %s: %s\n", procdir,
      82                 :            :                                                 strerror(errno));
      83                 :            :                         }
      84                 :            : 
      85                 :          0 :                         fdir = dirfd(dir);
      86         [ #  # ]:          0 :                         if (fdir < 0) {
      87                 :          0 :                                 status = errno;
      88                 :          0 :                                 closedir(dir);
      89                 :          0 :                                 rte_panic("Error %d obtaining fd for dir %s: %s\n",
      90                 :            :                                                 fdir, procdir,
      91                 :            :                                                 strerror(status));
      92                 :            :                         }
      93                 :            : 
      94         [ #  # ]:          0 :                         while ((dirent = readdir(dir)) != NULL) {
      95                 :            : 
      96         [ #  # ]:          0 :                                 if (strcmp(dirent->d_name, ".") == 0 ||
      97         [ #  # ]:          0 :                                         strcmp(dirent->d_name, "..") == 0)
      98                 :          0 :                                         continue;
      99                 :            : 
     100                 :          0 :                                 errno = 0;
     101                 :          0 :                                 fd = strtol(dirent->d_name, &endptr, 10);
     102   [ #  #  #  # ]:          0 :                                 if (errno != 0 || endptr[0] != '\0') {
     103                 :            :                                         printf("Error converting name fd %d %s:\n",
     104                 :            :                                                 fd, dirent->d_name);
     105                 :          0 :                                         continue;
     106                 :            :                                 }
     107                 :            : 
     108         [ #  # ]:          0 :                                 if (fd == fdir || fd <= 2)
     109                 :          0 :                                         continue;
     110                 :            : 
     111                 :          0 :                                 close(fd);
     112                 :            :                         }
     113                 :          0 :                         closedir(dir);
     114                 :            :                 }
     115                 :            : #endif
     116                 :            :                 printf("Running binary with argv[]:");
     117         [ #  # ]:          0 :                 for (i = 0; i < num; i++)
     118                 :          0 :                         printf("'%s' ", argv_cpy[i]);
     119                 :            :                 printf("\n");
     120                 :          0 :                 fflush(stdout);
     121                 :            : 
     122                 :            :                 /* set the environment variable */
     123         [ #  # ]:          0 :                 if (setenv(RECURSIVE_ENV_VAR, env_value, 1) != 0)
     124                 :          0 :                         rte_panic("Cannot export environment variable\n");
     125                 :            : 
     126                 :            :                 strlcpy(path, "/proc/" self "/" exe, sizeof(path));
     127         [ #  # ]:          0 :                 if (execv(path, argv_cpy) < 0) {
     128         [ #  # ]:          0 :                         if (errno == ENOENT) {
     129                 :            :                                 printf("Could not find '%s', is procfs mounted?\n",
     130                 :            :                                                 path);
     131                 :            :                         }
     132                 :          0 :                         rte_panic("Cannot exec: %s\n", strerror(errno));
     133                 :            :                 }
     134                 :            :         }
     135                 :            :         /* parent process does a wait */
     136                 :            : #ifdef RTE_LIB_PDUMP
     137                 :            : #ifdef RTE_NET_RING
     138         [ +  + ]:        120 :         if ((strcmp(env_value, "run_pdump_server_tests") == 0)) {
     139                 :          1 :                 rc = rte_thread_create(&thread, NULL, send_pkts, NULL);
     140         [ -  + ]:          1 :                 if (rc != 0) {
     141                 :          0 :                         rte_panic("Cannot start send pkts thread: %s\n",
     142                 :            :                                   strerror(rc));
     143                 :            :                 }
     144                 :            :         }
     145                 :            : #endif
     146                 :            : #endif
     147                 :            : 
     148         [ -  + ]:        120 :         while (wait(&status) != pid)
     149                 :            :                 ;
     150                 :            : #ifdef RTE_LIB_PDUMP
     151                 :            : #ifdef RTE_NET_RING
     152         [ +  + ]:        120 :         if ((strcmp(env_value, "run_pdump_server_tests") == 0)) {
     153                 :          1 :                 flag_for_send_pkts = 0;
     154                 :          1 :                 rte_thread_join(thread, NULL);
     155                 :            :         }
     156                 :            : #endif
     157                 :            : #endif
     158                 :        120 :         return status;
     159                 :            : }
     160                 :            : 
     161                 :            : /* FreeBSD doesn't support file prefixes, so force compile failures for any
     162                 :            :  * tests attempting to use this function on FreeBSD.
     163                 :            :  */
     164                 :            : #ifdef RTE_EXEC_ENV_LINUX
     165                 :            : static char *
     166                 :         12 : get_current_prefix(char *prefix, int size)
     167                 :            : {
     168                 :         12 :         char path[PATH_MAX] = {0};
     169                 :         12 :         char buf[PATH_MAX] = {0};
     170                 :            : 
     171                 :            :         /* get file for config (fd is always 3) */
     172                 :            :         snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
     173                 :            : 
     174                 :            :         /* return NULL on error */
     175         [ +  - ]:         12 :         if (readlink(path, buf, sizeof(buf)) == -1)
     176                 :            :                 return NULL;
     177                 :            : 
     178                 :            :         /* get the prefix */
     179                 :         12 :         snprintf(prefix, size, "%s", basename(dirname(buf)));
     180                 :            : 
     181                 :         12 :         return prefix;
     182                 :            : }
     183                 :            : #endif
     184                 :            : 
     185                 :            : #endif /* _PROCESS_H_ */

Generated by: LCOV version 1.14