File: execute.c

package info (click to toggle)
cdebootstrap 0.3.4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 816 kB
  • ctags: 308
  • sloc: sh: 3,408; ansic: 2,522; makefile: 243
file content (103 lines) | stat: -rw-r--r-- 3,189 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * exec.c
 *
 * Copyright (C) 2004 Bastian Blank <waldi@debian.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * $LastChangedBy: bastian $
 * $LastChangedDate: 2004-09-19 12:03:45 +0200 (Sun, 19 Sep 2004) $
 * $LastChangedRevision: 628 $
 */

#include <config.h>

#include <debian-installer.h>

#include "execute.h"
#include "install.h"
#include "log.h"

const char *environment[] =
{
  "DEBIAN_FRONTEND=noninteractive",
  "PATH=",
  NULL
};

static int execute_io_handler (const char *buf, size_t n __attribute__ ((unused)), void *user_data __attribute__ ((unused)))
{ 
  log_text (DI_LOG_LEVEL_OUTPUT, "%s", buf);
  return 0;
}

static int execute_real (const char *const command, di_io_handler *stdout_handler, di_io_handler *stderr_handler, void *io_user_data, di_process_handler *child_prepare_handler, void *child_prepare_user_data)
{
  const char *const argv[] = { "sh", "-c", command, NULL };
  int ret = di_exec_env_full ("/bin/sh", argv, environment, stdout_handler, stderr_handler, io_user_data, NULL, NULL, child_prepare_handler, child_prepare_user_data);
  log_text (DI_LOG_LEVEL_DEBUG, "Return code: %d", ret);
  return ret;
}

int execute (const char *const command)
{
  return execute_full (command, execute_io_handler, execute_io_handler, NULL, NULL, NULL);
}

int execute_full (const char *const command, di_io_handler *stdout_handler, di_io_handler *stderr_handler, void *io_user_data, di_process_handler *child_prepare_handler, void *child_prepare_user_data)
{
  log_text (DI_LOG_LEVEL_DEBUG, "Execute \"%s\"", command);
  return execute_real (command, stdout_handler, stderr_handler, io_user_data, child_prepare_handler, child_prepare_user_data);
}

int execute_target (const char *command)
{
  return execute_target_full (command, execute_io_handler, execute_io_handler, NULL);
}

int execute_target_full (const char *const command, di_io_handler *stdout_handler, di_io_handler *stderr_handler, void *io_user_data)
{
  log_text (DI_LOG_LEVEL_DEBUG, "Execute \"%s\" in chroot", command);
  return execute_real (command, stdout_handler, stderr_handler, io_user_data, di_exec_prepare_chroot, install_root);
}

int execute_init (char *const *envp)
{
  const char **t1;
  char *const *t2;

  t1 = environment;
  while (*t1)
  {
    size_t l = strlen (*t1);
    if ((*t1)[l - 1] == '=')
    {
      t2 = envp;
      while (*t2)
      {
        if (strncmp (*t1, *t2, l) == 0)
        {
          *t1 = *t2;
          break;
        }
        t2++;
      }
    }
    t1++;
  }

  return 0;
}