<commit_before>/*
 *  exec.c
 *  eINIT
 *
 *  Created by <NAME> on 23/11/2006.
 *  Copyright 2006, 2007 <NAME>. All rights reserved.
 *
 */

/*
Copyright (c) 2006, 2007, <NAME>
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
	  this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
	  this list of conditions and the following disclaimer in the documentation
	  and/or other materials provided with the distribution.
    * Neither the name of the project nor the names of its contributors may be
	  used to endorse or promote products derived from this software without
	  specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <dirent.h>
#include <string.h>
#include <einit/module.h>
#include <einit/config.h>
#include <einit/bitch.h>
#include <einit/utility.h>
#include <errno.h>
#include <signal.h>
#include <pthread.h>
#include <einit-modules/exec.h>
#include <einit-modules/scheduler.h>
#include <einit-modules/process.h>
#include <ctype.h>
#include <sys/stat.h> 

#include <einit-modules/parse-sh.h>

#ifdef POSIXREGEX
#include <regex.h>
#endif

#ifdef LINUX
#include <sys/syscall.h>
#endif

int einit_exec_configure (struct lmodule *);

#if defined(EINIT_MODULE) || defined(EINIT_MODULE_HEADER)

const struct smodule einit_exec_self = {
 .eiversion = EINIT_VERSION,
 .eibuild   = BUILDNUMBER,
 .version   = 1,
 .mode      = 0,
 .name      = "pexec/dexec library module",
 .rid       = "einit-exec",
 .si        = {
  .provides = NULL,
  .requires = NULL,
  .after    = NULL,
  .before   = NULL
 },
 .configure = einit_exec_configure
};

module_register(einit_exec_self);

#endif

// char hasslash = strchr(key, '/') ? 1 : 0;

/* variables */
struct daemonst * running = NULL;

char **shell = NULL;
char *dshell[] = {"/bin/sh", "-c", NULL};
char *envfilebase = "/etc/econf.d/";

char *safe_environment[] = { "PATH=/bin:/sbin:/usr/bin:/usr/sbin", "TERM=dumb", NULL };

extern char shutting_down;

pthread_mutex_t running_mutex = PTHREAD_MUTEX_INITIALIZER;
int spawn_timeout = 5;
char kill_timeout_primary = 20, kill_timeout_secondary = 20;

char **check_variables_f (const char *, const char **, FILE *);
char *apply_envfile_f (char *command, const char **environment);
int pexec_f (const char *command, const char **variables, uid_t uid, gid_t gid, const char *user, const char *group, char **local_environment, struct einit_event *status);
int start_daemon_f (struct dexecinfo *shellcmd, struct einit_event *status);
int stop_daemon_f (struct dexecinfo *shellcmd, struct einit_event *status);
char **create_environment_f (char **environment, const char **variables);
void einit_exec_ipc_event_handler (struct einit_event *);
void einit_exec_einit_event_handler (struct einit_event *);
void einit_exec_process_event_handler (struct einit_event *);

void *dexec_watcher (struct spidcb *spid);

int einit_exec_cleanup (struct lmodule *irr) {
 if (shell && (shell != dshell)) free (shell);
 exec_cleanup (irr);

 function_unregister ("einit-execute-command", 1, pexec_f);
 function_unregister ("einit-execute-daemon", 1, start_daemon_f);
 function_unregister ("einit-stop-daemon", 1, stop_daemon_f);
 function_unregister ("einit-create-environment", 1, create_environment_f);
 function_unregister ("einit-check-variables", 1, check_variables_f);
 function_unregister ("einit-apply-envfile", 1, apply_envfile_f);

 event_ignore (einit_event_subsystem_ipc, einit_exec_ipc_event_handler);
 event_ignore (einit_event_subsystem_process, einit_exec_process_event_handler);
 event_ignore (einit_event_subsystem_core, einit_exec_einit_event_handler);

 sched_cleanup(irr);

 return 0;
}

void einit_exec_einit_event_handler (struct einit_event *ev) {
 if (ev->type == einit_core_configuration_update) {
  if (!(envfilebase = cfg_getpath("configuration-system-exec-envfile-base")))
   envfilebase = estrdup("/etc/econf.d/");
 }
}

void einit_exec_update_daemons_from_pidfiles() {
 emutex_lock (&running_mutex);
 struct daemonst *cur = running;

 while (cur) {
  struct dexecinfo *dx = cur->dx;

  if (dx) {
   struct stat st;
   if (dx->pidfile && !stat (dx->pidfile, &st)) {
    if (st.st_mtime > dx->pidfiles_last_update) {
     char *contents = readfile (dx->pidfile);
     if (contents) {
      pid_t daemon_pid = parse_integer (contents);

      cur->pid = daemon_pid;
      dx->pidfiles_last_update = st.st_mtime;

      free (contents);

      if (cur->module && cur->module->module && cur->module->module->rid) {
       notice (2, "exec: modules %s updated and is now known with pid %i.", cur->module->module->rid, cur->pid);
      } else {
       notice (2, "exec: anonymous daemon updated and is now known with pid %i.", cur->pid);
      }
     }
    }
   }
  }

  cur = cur->next;
 }
 emutex_unlock (&running_mutex);
}

void einit_exec_process_event_handler (struct einit_event *ev) {
 if (ev->type == einit_process_died) {
  einit_exec_update_daemons_from_pidfiles();

/* something needs to be done right here */
  struct spidcb *spid = ecalloc (1, sizeof (struct spidcb));
  spid->pid = ev->integer;
  spid->status = ev->status;

//  pthread_t th;

//  ethread_create (&th, &thread_attribute_detached, dexec_watcher, spid);
  dexec_watcher(spid);

  free (spid);
 }
}

void einit_exec_ipc_event_handler (struct einit_event *ev) {
 if (ev && ev->argv && ev->argv[0] && ev->argv[1] && strmatch(ev->argv[0], "exec")) {
  struct einit_event ee = evstaticinit (einit_feedback_module_status);
  ee.para = (void *)thismodule;

  pexec_f (ev->command, NULL, 0, 0, NULL, NULL, NULL, &ee);
  evstaticdestroy(ee);

  ev->implemented = 1;
 }
}

char *apply_envfile_f (char *command, const char **environment) {
 uint32_t i = 0;
 char **envfiles = NULL;
 char **variables = NULL;

 if (environment) {
  for (; environment[i]; i++) {
   if (strstr(environment[i], "envfile=") == environment[i]) {
    if (envfiles) free (envfiles);

    if (envfiles) free (envfiles);
    envfiles = str2set (':', environment[i]+8);
   } else if (strstr(environment[i], "services=") == environment[i]) {
    if (!envfiles) envfiles = str2set (':', environment[i]+9);
   } else {
    char *r = estrdup (environment[i]);
    char *n = strchr (r, '=');

    if (n) {
     *n = 0;
     n++;

     if (*n && !inset ((const void **)variables, r, SET_TYPE_STRING)) {
      variables = (char **)setadd ((void **)variables, r, SET_TYPE_STRING);
      variables = (char **)setadd ((void **)variables, n, SET_TYPE_STRING);
     }
    }

    free (r);
   }
  }
 }

 if (envfiles) {
  for (i = 0; envfiles[i]; i++) {
   char buffer[BUFFERSIZE];
   char *file = NULL;
   struct stat st;

   if (envfiles[i][0] == '/')
    file = envfiles[i];
   else {
    file = emalloc (strlen (envfilebase)+strlen(envfiles[i]) + 1);
    *file = 0;
    strcat (file, envfilebase);
    strcat (file, envfiles[i]);
   }

   if (file && !stat (file, &st)) {
    char *oc = command;

    esprintf (buffer, BUFFERSIZE, " . %s;\n", file);

    command = emalloc (strlen(buffer) + strlen (oc) + 1);
    *command = 0;
    strcat (command, buffer);
    strcat (command, oc);
   }
  }
 }

 if (variables) {
  command = apply_variables (command, (const char **)variables);

  free (variables);
 }

 return command;
}

char **check_variables_f (const char *id, const char **variables, FILE *output) {
 uint32_t u = 0;
 if (!variables) return (char **)variables;
 for (u = 0; variables[u]; u++) {
  char *e = estrdup (variables[u]), *ep = strchr (e, '/');
  char *x[] = { e, NULL, NULL };
  char node_found = 1;
  uint32_t variable_matches = 0;

  if (ep) {
   *ep = 0;
   x[0] = estrdup (e);
   *ep = '/';

   ep++;
   x[1] = ep;
  }

#ifndef POSIXREGEX
  if (!cfg_getnode (x[0], NULL)) {
   node_found = 0;
  } else if (cfg_getstring (e, NULL)) {
   variable_matches++;
  }
#else
  struct cfgnode *n;

  if (!(n = cfg_getnode (x[0], NULL))) {
   node_found = 0;
  } else if (x[1] && n->arbattrs) {
   regex_t pattern;
   if (!eregcomp(&pattern, x[1])) {
    uint32_t v = 0;
    for (v = 0; n->arbattrs[v]; v+=2) {
     if (!regexec (&pattern, n->arbattrs[v], 0, NULL, 0)) {
      variable_matches++;
     }
    }

    regfree (&pattern);
   }
  } else if (cfg_getstring (x[0], NULL)) {
   variable_matches++;
  }
#endif

  if (!node_found) {
   eprintf (output, " * module: %s: undefined node: %s\n", id, x[0]);
  } else if (!variable_matches) {
   eprintf (output, " * module: %s: undefined variable: %s\n", id, e);
  }

  if (x[0] != e) free (x[0]);
  free (e);
 }

 return (char **)variables;
}

char **create_environment_f (char **environment, const char **variables) {
 int i = 0;
 char *variablevalue = NULL;
 if (variables) for (i = 0; variables[i]; i++) {
#ifdef POSIXREGEX
  if ((variablevalue = strchr (variables[i], '/'))) {
/* special treatment if we have an attribue specifier in the variable name */
   char *name = NULL, *filter = variablevalue+1;
   struct cfgnode *node;
   *variablevalue = 0;
   name = estrdup(variables[i]);
   *variablevalue = '/';

   if ((node = cfg_getnode (name, NULL)) && node->arbattrs) {
    size_t bkeylen = strlen (name)+2, pvlen = 1;
    char *key = emalloc (bkeylen);
    char *pvalue = NULL;
    regex_t pattern;

    if (!eregcomp(&pattern, filter)) {
     int y = 0;
     *key = 0;
     strcat (key, name);
     *(key+bkeylen-2) = '/';
     *(key+bkeylen-1) = 0;

     for (y = 0; node->arbattrs[y]; y+=2) if (!regexec (&pattern, node->arbattrs[y], 0, NULL, 0)) {
      size_t attrlen = strlen (node->arbattrs[y])+1;
      char *subkey = emalloc (bkeylen+attrlen);
      *subkey = 0;
      strcat (subkey, key);
      strcat (subkey, node->arbattrs[y]);
      environment = straddtoenviron (environment, subkey, node->arbattrs[y+1]);
      free (subkey);

      if (pvalue) {
       pvalue=erealloc (pvalue, pvlen+attrlen);
       *(pvalue+pvlen-2) = ' ';
       *(pvalue+pvlen-1) = 0;
       strcat (pvalue, node->arbattrs[y]);
       pvlen += attrlen;
      } else {
       pvalue=emalloc (pvlen+attrlen);
       *pvalue = 0;
       strcat (pvalue, node->arbattrs[y]);
       pvlen += attrlen;
      }
     }

     regfree (&pattern);
    }

    if (pvalue)  {
     uint32_t txi = 0;
     for (; pvalue[txi]; txi++) {
      if (!isalnum (pvalue[txi]) && (pvalue[txi] != ' ')) pvalue[txi] = '_';
     }
     *(key+bkeylen-2) = 0;
     environment = straddtoenviron (environment, key, pvalue);

     free (pvalue);
    }
    free (key);
    free (name);
   }
  } else {
/* else: just add it */
   char *variablevalue = cfg_getstring (variables[i], NULL);
   if (variablevalue)
    environment = straddtoenviron (environment, variables[i], variablevalue);
  }
#else
  char *variablevalue = cfg_getstring (variables[i], NULL);
  if (variablevalue)
   environment = straddtoenviron (environment, variables[i], variablevalue);
#endif
 }

/*  if (variables) {
   int i = 0;
   for (; variables [i]; i++) {
    char *variablevalue = cfg_getstring (variables [i], NULL);
    if (variablevalue) {
     exec_environment = straddtoenviron (exec_environment, variables [i], variablevalue);
    }
   }
  }*/

 return environment;
}

struct exec_parser_data {
 int commands;
 char **command;
 char forkflag;
};

void exec_callback (char **data, enum einit_sh_parser_pa status, struct exec_parser_data *pd) {
 switch (status) {
  case pa_end_of_file: break;

  case pa_new_context:
  case pa_new_context_fork:
   if (pd->command) {
    free (pd->command);
   }

   pd->command = (char **)setdup((const void **)data, SET_TYPE_STRING);
   pd->commands++;
   pd->forkflag = (status == pa_new_context_fork);
   break;

  default: break;
 }
}

void exec_run_sh (const char *command, enum pexec_options options, char **exec_environment) {
 struct exec_parser_data pd;

 memset (&pd, 0, sizeof (pd));

 parse_sh_ud (command, (void (*)(const char **, enum einit_sh_parser_pa, void *))exec_callback, &pd);

 if ((pd.commands == 1) && pd.command) {
  int forkres = 0;

  if (!pd.forkflag || ((forkres = fork()) == 0)) {
   char **r = which (pd.command[0]);

   if (r && r[0]) {
    pd.command[0] = r[0];
   }

   char *cmdtx = set2str (',', (const char **)pd.command);
   if (cmdtx) {
    fprintf (stdout, "einit/sh: running command: (%s)\n", cmdtx);
    free (cmdtx);

//    sleep (1);
   }

   if (options & pexec_option_safe_environment) {
    execve (pd.command[0], pd.command, safe_environment);
   } else {
    execve (pd.command[0], pd.command, exec_environment);
   }

   perror (pd.command[0]);

   if (r) free (r);
  }

  if (forkres == -1) {<commit_msg>not gonna have this one in for now<commit_after>/*
 *  exec.c
 *  eINIT
 *
 *  Created by <NAME> on 23/11/2006.
 *  Copyright 2006, 2007 <NAME>. All rights reserved.
 *
 */

/*
Copyright (c) 2006, 2007, <NAME>
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
	  this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
	  this list of conditions and the following disclaimer in the documentation
	  and/or other materials provided with the distribution.
    * Neither the name of the project nor the names of its contributors may be
	  used to endorse or promote products derived from this software without
	  specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <dirent.h>
#include <string.h>
#include <einit/module.h>
#include <einit/config.h>
#include <einit/bitch.h>
#include <einit/utility.h>
#include <errno.h>
#include <signal.h>
#include <pthread.h>
#include <einit-modules/exec.h>
#include <einit-modules/scheduler.h>
#include <einit-modules/process.h>
#include <ctype.h>
#include <sys/stat.h> 

#include <einit-modules/parse-sh.h>

#ifdef POSIXREGEX
#include <regex.h>
#endif

#ifdef LINUX
#include <sys/syscall.h>
#endif

int einit_exec_configure (struct lmodule *);

#if defined(EINIT_MODULE) || defined(EINIT_MODULE_HEADER)

const struct smodule einit_exec_self = {
 .eiversion = EINIT_VERSION,
 .eibuild   = BUILDNUMBER,
 .version   = 1,
 .mode      = 0,
 .name      = "pexec/dexec library module",
 .rid       = "einit-exec",
 .si        = {
  .provides = NULL,
  .requires = NULL,
  .after    = NULL,
  .before   = NULL
 },
 .configure = einit_exec_configure
};

module_register(einit_exec_self);

#endif

// char hasslash = strchr(key, '/') ? 1 : 0;

/* variables */
struct daemonst * running = NULL;

char **shell = NULL;
char *dshell[] = {"/bin/sh", "-c", NULL};
char *envfilebase = "/etc/econf.d/";

char *safe_environment[] = { "PATH=/bin:/sbin:/usr/bin:/usr/sbin", "TERM=dumb", NULL };

extern char shutting_down;

pthread_mutex_t running_mutex = PTHREAD_MUTEX_INITIALIZER;
int spawn_timeout = 5;
char kill_timeout_primary = 20, kill_timeout_secondary = 20;

char **check_variables_f (const char *, const char **, FILE *);
char *apply_envfile_f (char *command, const char **environment);
int pexec_f (const char *command, const char **variables, uid_t uid, gid_t gid, const char *user, const char *group, char **local_environment, struct einit_event *status);
int start_daemon_f (struct dexecinfo *shellcmd, struct einit_event *status);
int stop_daemon_f (struct dexecinfo *shellcmd, struct einit_event *status);
char **create_environment_f (char **environment, const char **variables);
void einit_exec_ipc_event_handler (struct einit_event *);
void einit_exec_einit_event_handler (struct einit_event *);
void einit_exec_process_event_handler (struct einit_event *);

void *dexec_watcher (struct spidcb *spid);

int einit_exec_cleanup (struct lmodule *irr) {
 if (shell && (shell != dshell)) free (shell);
 exec_cleanup (irr);

 function_unregister ("einit-execute-command", 1, pexec_f);
 function_unregister ("einit-execute-daemon", 1, start_daemon_f);
 function_unregister ("einit-stop-daemon", 1, stop_daemon_f);
 function_unregister ("einit-create-environment", 1, create_environment_f);
 function_unregister ("einit-check-variables", 1, check_variables_f);
 function_unregister ("einit-apply-envfile", 1, apply_envfile_f);

 event_ignore (einit_event_subsystem_ipc, einit_exec_ipc_event_handler);
 event_ignore (einit_event_subsystem_process, einit_exec_process_event_handler);
 event_ignore (einit_event_subsystem_core, einit_exec_einit_event_handler);

 sched_cleanup(irr);

 return 0;
}

void einit_exec_einit_event_handler (struct einit_event *ev) {
 if (ev->type == einit_core_configuration_update) {
  if (!(envfilebase = cfg_getpath("configuration-system-exec-envfile-base")))
   envfilebase = estrdup("/etc/econf.d/");
 }
}

void einit_exec_update_daemons_from_pidfiles() {
 emutex_lock (&running_mutex);
 struct daemonst *cur = running;

 while (cur) {
  struct dexecinfo *dx = cur->dx;

  if (dx) {
   struct stat st;
   if (dx->pidfile && !stat (dx->pidfile, &st)) {
    if (st.st_mtime > dx->pidfiles_last_update) {
     char *contents = readfile (dx->pidfile);
     if (contents) {
      pid_t daemon_pid = parse_integer (contents);

      cur->pid = daemon_pid;
      dx->pidfiles_last_update = st.st_mtime;

      free (contents);

      if (cur->module && cur->module->module && cur->module->module->rid) {
       notice (2, "exec: modules %s updated and is now known with pid %i.", cur->module->module->rid, cur->pid);
      } else {
       notice (2, "exec: anonymous daemon updated and is now known with pid %i.", cur->pid);
      }
     }
    }
   }
  }

  cur = cur->next;
 }
 emutex_unlock (&running_mutex);
}

void einit_exec_process_event_handler (struct einit_event *ev) {
 if (ev->type == einit_process_died) {
  einit_exec_update_daemons_from_pidfiles();

/* something needs to be done right here */
  struct spidcb *spid = ecalloc (1, sizeof (struct spidcb));
  spid->pid = ev->integer;
  spid->status = ev->status;

//  pthread_t th;

//  ethread_create (&th, &thread_attribute_detached, dexec_watcher, spid);
  dexec_watcher(spid);

  free (spid);
 }
}

void einit_exec_ipc_event_handler (struct einit_event *ev) {
 if (ev && ev->argv && ev->argv[0] && ev->argv[1] && strmatch(ev->argv[0], "exec")) {
  struct einit_event ee = evstaticinit (einit_feedback_module_status);
  ee.para = (void *)thismodule;

  pexec_f (ev->command, NULL, 0, 0, NULL, NULL, NULL, &ee);
  evstaticdestroy(ee);

  ev->implemented = 1;
 }
}

char *apply_envfile_f (char *command, const char **environment) {
 uint32_t i = 0;
 char **envfiles = NULL;
 char **variables = NULL;

 if (environment) {
  for (; environment[i]; i++) {
   if (strstr(environment[i], "envfile=") == environment[i]) {
    if (envfiles) free (envfiles);

    if (envfiles) free (envfiles);
    envfiles = str2set (':', environment[i]+8);
   } else if (strstr(environment[i], "services=") == environment[i]) {
    if (!envfiles) envfiles = str2set (':', environment[i]+9);
   } else {
    char *r = estrdup (environment[i]);
    char *n = strchr (r, '=');

    if (n) {
     *n = 0;
     n++;

     if (*n && !inset ((const void **)variables, r, SET_TYPE_STRING)) {
      variables = (char **)setadd ((void **)variables, r, SET_TYPE_STRING);
      variables = (char **)setadd ((void **)variables, n, SET_TYPE_STRING);
     }
    }

    free (r);
   }
  }
 }

 if (envfiles) {
  for (i = 0; envfiles[i]; i++) {
   char buffer[BUFFERSIZE];
   char *file = NULL;
   struct stat st;

   if (envfiles[i][0] == '/')
    file = envfiles[i];
   else {
    file = emalloc (strlen (envfilebase)+strlen(envfiles[i]) + 1);
    *file = 0;
    strcat (file, envfilebase);
    strcat (file, envfiles[i]);
   }

   if (file && !stat (file, &st)) {
    char *oc = command;

    esprintf (buffer, BUFFERSIZE, " . %s;\n", file);

    command = emalloc (strlen(buffer) + strlen (oc) + 1);
    *command = 0;
    strcat (command, buffer);
    strcat (command, oc);
   }
  }
 }

 if (variables) {
  command = apply_variables (command, (const char **)variables);

  free (variables);
 }

 return command;
}

char **check_variables_f (const char *id, const char **variables, FILE *output) {
 uint32_t u = 0;
 if (!variables) return (char **)variables;
 for (u = 0; variables[u]; u++) {
  char *e = estrdup (variables[u]), *ep = strchr (e, '/');
  char *x[] = { e, NULL, NULL };
  char node_found = 1;
  uint32_t variable_matches = 0;

  if (ep) {
   *ep = 0;
   x[0] = estrdup (e);
   *ep = '/';

   ep++;
   x[1] = ep;
  }

#ifndef POSIXREGEX
  if (!cfg_getnode (x[0], NULL)) {
   node_found = 0;
  } else if (cfg_getstring (e, NULL)) {
   variable_matches++;
  }
#else
  struct cfgnode *n;

  if (!(n = cfg_getnode (x[0], NULL))) {
   node_found = 0;
  } else if (x[1] && n->arbattrs) {
   regex_t pattern;
   if (!eregcomp(&pattern, x[1])) {
    uint32_t v = 0;
    for (v = 0; n->arbattrs[v]; v+=2) {
     if (!regexec (&pattern, n->arbattrs[v], 0, NULL, 0)) {
      variable_matches++;
     }
    }

    regfree (&pattern);
   }
  } else if (cfg_getstring (x[0], NULL)) {
   variable_matches++;
  }
#endif

  if (!node_found) {
   eprintf (output, " * module: %s: undefined node: %s\n", id, x[0]);
  } else if (!variable_matches) {
   eprintf (output, " * module: %s: undefined variable: %s\n", id, e);
  }

  if (x[0] != e) free (x[0]);
  free (e);
 }

 return (char **)variables;
}

char **create_environment_f (char **environment, const char **variables) {
 int i = 0;
 char *variablevalue = NULL;
 if (variables) for (i = 0; variables[i]; i++) {
#ifdef POSIXREGEX
  if ((variablevalue = strchr (variables[i], '/'))) {
/* special treatment if we have an attribue specifier in the variable name */
   char *name = NULL, *filter = variablevalue+1;
   struct cfgnode *node;
   *variablevalue = 0;
   name = estrdup(variables[i]);
   *variablevalue = '/';

   if ((node = cfg_getnode (name, NULL)) && node->arbattrs) {
    size_t bkeylen = strlen (name)+2, pvlen = 1;
    char *key = emalloc (bkeylen);
    char *pvalue = NULL;
    regex_t pattern;

    if (!eregcomp(&pattern, filter)) {
     int y = 0;
     *key = 0;
     strcat (key, name);
     *(key+bkeylen-2) = '/';
     *(key+bkeylen-1) = 0;

     for (y = 0; node->arbattrs[y]; y+=2) if (!regexec (&pattern, node->arbattrs[y], 0, NULL, 0)) {
      size_t attrlen = strlen (node->arbattrs[y])+1;
      char *subkey = emalloc (bkeylen+attrlen);
      *subkey = 0;
      strcat (subkey, key);
      strcat (subkey, node->arbattrs[y]);
      environment = straddtoenviron (environment, subkey, node->arbattrs[y+1]);
      free (subkey);

      if (pvalue) {
       pvalue=erealloc (pvalue, pvlen+attrlen);
       *(pvalue+pvlen-2) = ' ';
       *(pvalue+pvlen-1) = 0;
       strcat (pvalue, node->arbattrs[y]);
       pvlen += attrlen;
      } else {
       pvalue=emalloc (pvlen+attrlen);
       *pvalue = 0;
       strcat (pvalue, node->arbattrs[y]);
       pvlen += attrlen;
      }
     }

     regfree (&pattern);
    }

    if (pvalue)  {
     uint32_t txi = 0;
     for (; pvalue[txi]; txi++) {
      if (!isalnum (pvalue[txi]) && (pvalue[txi] != ' ')) pvalue[txi] = '_';
     }
     *(key+bkeylen-2) = 0;
     environment = straddtoenviron (environment, key, pvalue);

     free (pvalue);
    }
    free (key);
    free (name);
   }
  } else {
/* else: just add it */
   char *variablevalue = cfg_getstring (variables[i], NULL);
   if (variablevalue)
    environment = straddtoenviron (environment, variables[i], variablevalue);
  }
#else
  char *variablevalue = cfg_getstring (variables[i], NULL);
  if (variablevalue)
   environment = straddtoenviron (environment, variables[i], variablevalue);
#endif
 }

/*  if (variables) {
   int i = 0;
   for (; variables [i]; i++) {
    char *variablevalue = cfg_getstring (variables [i], NULL);
    if (variablevalue) {
     exec_environment = straddtoenviron (exec_environment, variables [i], variablevalue);
    }
   }
  }*/

 return environment;
}

struct exec_parser_data {
 int commands;
 char **command;
 char forkflag;
};

void exec_callback (char **data, enum einit_sh_parser_pa status, struct exec_parser_data *pd) {
 switch (status) {
  case pa_end_of_file: break;

  case pa_new_context:
  case pa_new_context_fork:
   if (pd->command) {
    free (pd->command);
   }

   pd->command = (char **)setdup((const void **)data, SET_TYPE_STRING);
   pd->commands++;
   pd->forkflag = (status == pa_new_context_fork);
   break;

  default: break;
 }
}

void exec_run_sh (const char *command, enum pexec_options options, char **exec_environment) {
 struct exec_parser_data pd;

 memset (&pd, 0, sizeof (pd));

// parse_sh_ud (command, (void (*)(const char **, enum einit_sh_parser_pa, void *))exec_callback, &pd);

 if ((pd.commands == 1) && pd.command) {
  int forkres = 0;

  if (!pd.forkflag || ((forkres = fork()) == 0)) {
   char **r = which (pd.command[0]);

   if (r && r[0]) {
    pd.command[0] = r[0];
   }

   char *cmdtx = set2str (',', (const char **)pd.command);
   if (cmdtx) {
    fprintf (stdout, "einit/sh: running command: (%s)\n", cmdtx);
    free (cmdtx);

//    sleep (1);
   }

   if (options & pexec_option_safe_environment) {
    execve (pd.command[0], pd.command, safe_environment);
   } else {
    execve (pd.command[0], pd.command, exec_environment);
   }

   perror (pd.command[0]);

   if (r) free (r);
  }

  if (forkres == -1) {
{'fraction_non_alphanumeric': 0.14831989989326855, 'fraction_numerical': 0.009789849471863383, 'mean_word_length': 3.6623198352779682, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 151, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4976987', 'n_tokens_mistral': 9871, 'n_tokens_neox': 9475, 'n_words': 2640}