Changeset 197 for trunk/src/sh_unix.c


Ignore:
Timestamp:
Nov 21, 2008, 10:33:04 PM (16 years ago)
Author:
katerina
Message:

Rewrite of code for conditionals in configuration file, supports more tests now (ticket #129).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/sh_unix.c

    r194 r197  
    20272027
    20282028  SL_RETURN((0),_("sh_unix_init"));
     2029}
     2030
     2031/* --- run a command, securely --- */
     2032
     2033int sh_unix_run_command (const char * str)
     2034{
     2035  pid_t  pid;
     2036  char * arg[4];
     2037  char * env[5];
     2038  char * path = sh_util_strdup(_("/bin/sh"));
     2039
     2040  int  status = -1;
     2041
     2042  arg[0] = sh_util_strdup(_("/bin/sh"));
     2043  arg[1] = sh_util_strdup(_("-c"));
     2044  arg[2] = sh_util_strdup(str);
     2045  arg[3] = NULL;
     2046
     2047  env[0] = sh_util_strdup(_("PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/ucb"));
     2048  env[1] = sh_util_strdup(_("SHELL=/bin/sh"));
     2049  env[2] = sh_util_strdup(_("IFS= \t\n"));
     2050  if (getenv("TZ")) {
     2051    char * tz = sh_util_strdup(getenv("TZ"));
     2052    if (SL_TRUE == sl_ok_adds (4, strlen(tz))) {
     2053        env[3] = SH_ALLOC(4+strlen(tz));
     2054        sl_strlcpy(env[3], "TZ=", 4);
     2055        sl_strlcat(env[3], tz   , 4+strlen(tz));
     2056    } else {
     2057      env[3] = NULL;
     2058    }
     2059  } else {
     2060    env[3] = NULL;
     2061  }
     2062  env[4] = NULL;
     2063
     2064  pid = fork();
     2065
     2066  if (pid == (pid_t)(-1))
     2067    {
     2068      return -1;
     2069    }
     2070
     2071  else if (pid == 0) /* child */
     2072    {
     2073      memset(skey, 0, sizeof(sh_key_t));
     2074      (void) umask(S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH);
     2075      sh_unix_closeall (3, -1, SL_TRUE); /* in child process */
     2076      execve(path, arg, env);
     2077      _exit(EXIT_FAILURE);
     2078    }
     2079
     2080  else /* parent */
     2081    {
     2082      int r;
     2083
     2084      while((r = waitpid(pid, &status, WCONTINUED|WUNTRACED)) != pid && r != -1) ;
     2085
     2086      if (r == -1 || !WIFEXITED(status))
     2087        {
     2088          status = -1;
     2089        }
     2090      else
     2091        {
     2092          status = WEXITSTATUS(status);
     2093        }
     2094     }
     2095
     2096  return status;
    20292097}
    20302098
Note: See TracChangeset for help on using the changeset viewer.