aboutsummaryrefslogtreecommitdiff
path: root/winsup/cygwin/exec.cc
blob: 54d2c9577a80edf4336cbab075283aba45a9f047 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* exec.cc: exec system call support.

   Copyright 1996, 1997, 1998, 2000 Cygnus Solutions.

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <process.h>
#include "winsup.h"

/* This is called _execve and not execve because the real execve is defined
   in libc/posix/execve.c.  It calls us.  */

extern "C"
pid_t
_execve (const char *path, const char *const argv[], const char *const envp[])
{
  static char *const empty_env[] = { 0 };
  MALLOC_CHECK;
  if (!envp)
    envp = empty_env;
  return _spawnve (NULL, _P_OVERLAY, path, argv, envp);
}

extern "C"
int
execl (const char *path, const char *arg0, ...)
{
  int i;
  va_list args;
  const char *argv[1024];

  va_start (args, arg0);
  argv[0] = arg0;
  i = 1;
  do
      argv[i] = va_arg (args, const char *);
  while (argv[i++] != NULL);
  va_end (args);
  MALLOC_CHECK;
  return _execve (path, (char * const  *) argv, *user_data->envptr);
}

extern "C"
int
execv (const char *path, char * const *argv)
{
  MALLOC_CHECK;
  return _execve (path, (char * const *) argv, *user_data->envptr);
}

/* the same as a standard exec() calls family, but with NT security support */

extern "C"
pid_t
sexecve (HANDLE hToken, const char *path, const char *const argv[],
	 const char *const envp[])
{
  _spawnve (hToken, _P_OVERLAY, path, argv, envp);
  return -1;
}

extern "C"
int
sexecl (HANDLE hToken, const char *path, const char *arg0, ...)
{
  int i;
  va_list args;
  const char *argv[1024];

  va_start (args, arg0);
  argv[0] = arg0;
  i = 1;

  do
      argv[i] = va_arg (args, const char *);
  while (argv[i++] != NULL);

  va_end (args);

  MALLOC_CHECK;
  return sexecve (hToken, path, (char * const *) argv, *user_data->envptr);
}

extern "C"
int
sexecle (HANDLE hToken, const char *path, const char *arg0, ...)
{
  int i;
  va_list args;
  const char * const *envp;
  const char *argv[1024];

  va_start (args, arg0);
  argv[0] = arg0;
  i = 1;

  do
    argv[i] = va_arg (args, const char *);
  while (argv[i++] != NULL);

  envp = va_arg (args, const char * const *);
  va_end (args);

  MALLOC_CHECK;
  return sexecve(hToken, path, (char * const *) argv, (char * const *) envp);
}

extern "C"
int
sexeclp (HANDLE hToken, const char *path, const char *arg0, ...)
{
  int i;
  va_list args;
  const char *argv[1024];

  va_start (args, arg0);
  argv[0] = arg0;
  i = 1;

  do
      argv[i] = va_arg (args, const char *);
  while (argv[i++] != NULL);

  va_end (args);

  MALLOC_CHECK;
  return sexecvpe (hToken, path, (const char * const *) argv,
					      *user_data->envptr);
}

extern "C"
int
sexeclpe (HANDLE hToken, const char *path, const char *arg0, ...)
{
  int i;
  va_list args;
  const char * const *envp;
  const char *argv[1024];

  va_start (args, arg0);
  argv[0] = arg0;
  i = 1;

  do
    argv[i] = va_arg (args, const char *);
  while (argv[i++] != NULL);

  envp = va_arg (args, const char * const *);
  va_end (args);

  MALLOC_CHECK;
  return sexecvpe (hToken, path, argv, envp);
}

extern "C"
int
sexecv (HANDLE hToken, const char *path, const char * const *argv)
{
  MALLOC_CHECK;
  return sexecve (hToken, path, argv, *user_data->envptr);
}

extern "C"
int
sexecp (HANDLE hToken, const char *path, const char * const *argv)
{
  MALLOC_CHECK;
  return sexecvpe (hToken, path, argv, *user_data->envptr);
}

/*
 * Copy string, until c or <nul> is encountered.
 * NUL-terminate the destination string (s1).
 * Return pointer to terminating byte in dst string.
 */

char * __stdcall
strccpy (char *s1, const char **s2, char c)
{
  while (**s2 && **s2 != c)
    *s1++ = *((*s2)++);
  *s1 = 0;

  MALLOC_CHECK;
  return s1;
}

extern "C"
int
sexecvpe (HANDLE hToken, const char *file, const char * const *argv,
	  const char *const *envp)
{
  char buf[MAXNAMLEN];
  MALLOC_CHECK;
  return sexecve (hToken, find_exec (file, buf), argv, envp);
}