File: posix_spawn.cpp

package info (click to toggle)
watchman 4.9.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,992 kB
  • sloc: cpp: 27,459; python: 6,538; java: 3,404; php: 3,257; ansic: 2,803; javascript: 1,116; makefile: 671; ruby: 364; sh: 124; xml: 102; lisp: 4
file content (443 lines) | stat: -rw-r--r-- 11,255 bytes parent folder | download | duplicates (3)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/* Copyright 2014-present Facebook, Inc.
 * Licensed under the Apache License, Version 2.0 */
#include "watchman.h"
#include "watchman_synchronized.h"

// Maps pid => process handle
// This is so that we can wait/poll/query the termination status
static watchman::Synchronized<std::unordered_map<DWORD, HANDLE>> child_procs;

pid_t waitpid(pid_t pid, int* status, int options) {
  HANDLE h;

  {
    auto rlock = child_procs.rlock();
    auto it = rlock->find(pid);
    if (it == rlock->end()) {
      errno = ESRCH;
      return -1;
    }
    h = it->second;
  }

  auto res = WaitForSingleObject(h, options == WNOHANG ? 0 : INFINITE);

  switch (res) {
    case WAIT_OBJECT_0:
    case WAIT_ABANDONED_0:
      *status = 0;
      child_procs.wlock()->erase(pid);
      return pid;
    case WAIT_TIMEOUT:
      return 0;
    default:
      errno = EINVAL;
      return -1;
  }
}

int posix_spawnattr_init(posix_spawnattr_t *attrp) {
  memset(attrp, 0, sizeof(*attrp));
  return 0;
}

int posix_spawnattr_setflags(posix_spawnattr_t* attrp, short flags) {
  attrp->flags = flags;
  return 0;
}

int posix_spawnattr_getflags(posix_spawnattr_t* attrp, short* flags) {
  *flags = attrp->flags;
  return 0;
}

int posix_spawnattr_setcwd_np(posix_spawnattr_t *attrp, const char *path) {
  char *path_dup = NULL;

  if (path) {
    path_dup = strdup(path);
    if (!path_dup) {
      return ENOMEM;
    }
  }

  free(attrp->working_dir);
  attrp->working_dir = path_dup;
  return 0;
}

int posix_spawnattr_destroy(posix_spawnattr_t *attrp) {
  free(attrp->working_dir);
  return 0;
}

int posix_spawn_file_actions_init(posix_spawn_file_actions_t *actions) {
  memset(actions, 0, sizeof(*actions));
  return 0;
}

int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *actions,
    int fd, int target_fd) {
  struct _posix_spawn_file_action *acts, *act;
  acts = (_posix_spawn_file_action *)realloc(
      actions->acts, (actions->nacts + 1) * sizeof(*acts));
  if (!acts) {
    return ENOMEM;
  }
  act = &acts[actions->nacts];
  act->action = _posix_spawn_file_action::dup_fd;
  act->u.source_fd = fd;
  act->target_fd = target_fd;
  actions->acts = acts;
  actions->nacts++;
  return 0;
}

int posix_spawn_file_actions_adddup2_handle_np(
    posix_spawn_file_actions_t* actions,
    intptr_t handle,
    int target_fd) {
  struct _posix_spawn_file_action *acts, *act;
  acts = (_posix_spawn_file_action *)realloc(
      actions->acts, (actions->nacts + 1) * sizeof(*acts));
  if (!acts) {
    return ENOMEM;
  }
  act = &acts[actions->nacts];
  act->action = _posix_spawn_file_action::dup_handle;
  act->u.dup_local_handle = handle;
  act->target_fd = target_fd;
  actions->acts = acts;
  actions->nacts++;
  return 0;
}

int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t *actions,
    int target_fd, const char *name, int flags, int mode) {
  struct _posix_spawn_file_action *acts, *act;
  char *name_dup = strdup(name);
  if (!name_dup) {
    return ENOMEM;
  }
  acts = (_posix_spawn_file_action *)realloc(
      actions->acts, (actions->nacts + 1) * sizeof(*acts));
  if (!acts) {
    free(name_dup);
    return ENOMEM;
  }
  act = &acts[actions->nacts];
  act->action = _posix_spawn_file_action::open_file;
  act->target_fd = target_fd;
  act->u.open_info.name = name_dup;
  act->u.open_info.flags = flags;
  act->u.open_info.mode = mode;

  actions->acts = acts;
  actions->nacts++;
  return 0;
}

int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t *actions) {
  int i;
  for (i = 0; i < actions->nacts; i++) {
    if (actions->acts[i].action != _posix_spawn_file_action::open_file) {
      continue;
    }
    free(actions->acts[i].u.open_info.name);
  }
  free(actions->acts);
  return 0;
}

#define CMD_EXE_PREFIX "cmd.exe /c \""
static char *build_command_line(char *const argv[]) {
  int argc = 0, i = 0;
  size_t size = 0;
  char *cmdbuf = NULL, *cur = NULL;

  // Note: includes trailing NUL which we count as the closing quote
  size = sizeof(CMD_EXE_PREFIX);

  for (argc = 0; argv[argc] != NULL; argc++) {
    size += 4 * (strlen(argv[argc]) + 1);
  }

  cmdbuf = (char*)malloc(size);
  if (!cmdbuf) {
    return NULL;
  }

  // Here be dragons.  More gory details in http://stackoverflow.com/q/4094699
  // Surely not complete here by any means

  strcpy_s(cmdbuf, size, CMD_EXE_PREFIX);
  cur = cmdbuf + strlen(CMD_EXE_PREFIX);
  for (i = 0; i < argc; i++) {
    int j;
    char *arg = argv[i];

    // Space separated
    if (i > 0) {
      *cur = ' ';
      cur++;
    }

    *cur = '"';
    cur++;

    // FIXME: multibyte
    for (j = 0; arg[j]; j++) {
      switch (arg[j]) {
        case '"':
          strcpy(cur, "\"\"\"");
          cur += 3;
          break;
        default:
          *cur = arg[j];
          cur++;
      }
    }
    *cur = '"';
    cur++;
  }
  *cur = '"';
  cur++;
  *cur = 0;
  return cmdbuf;
}

static char *make_env_block(char *const envp[]) {
  int i;
  size_t total_len = 1; /* for final NUL */
  char *block = NULL;
  char *target = NULL;

  for (i = 0; envp[i]; i++) {
    total_len += strlen(envp[i]) + 1;
  }

  block = (char*)malloc(total_len);
  if (!block) {
    return NULL;
  }

  target = block;

  for (i = 0; envp[i]; i++) {
    size_t len = strlen(envp[i]);
    // Also copy the NULL
    memcpy(target, envp[i], len + 1);
    target += len + 1;
  }

  // Final NUL terminator
  *target = 0;

  return block;
}

static int posix_spawn_common(
    bool search_path,
    pid_t *pid, const char *path,
    const posix_spawn_file_actions_t *file_actions,
    const posix_spawnattr_t *attrp,
    char *const argv[], char *const envp[]) {
  STARTUPINFOEX sinfo;
  SECURITY_ATTRIBUTES sec;
  PROCESS_INFORMATION pinfo;
  char *cmdbuf;
  char *env_block;
  DWORD create_flags = CREATE_NO_WINDOW|EXTENDED_STARTUPINFO_PRESENT;
  int ret;
  int i;
  HANDLE inherited_handles[3] = {0, 0, 0};

  cmdbuf = build_command_line(argv);
  if (!cmdbuf) {
    return ENOMEM;
  }

  env_block = make_env_block(envp);
  if (!env_block) {
    free(cmdbuf);
    return ENOMEM;
  }

  memset(&sinfo, 0, sizeof(sinfo));
  sinfo.StartupInfo.cb = sizeof(sinfo);
  sinfo.StartupInfo.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
  sinfo.StartupInfo.wShowWindow = SW_HIDE;

  memset(&sec, 0, sizeof(sec));
  sec.nLength = sizeof(sec);
  sec.bInheritHandle = TRUE;

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

  if (attrp->flags & POSIX_SPAWN_SETPGROUP) {
    create_flags |= CREATE_NEW_PROCESS_GROUP;
  }

  for (i = 0; i < file_actions->nacts; i++) {
    struct _posix_spawn_file_action *act = &file_actions->acts[i];
    HANDLE *target = NULL;

    switch (act->target_fd) {
      case 0:
        target = &sinfo.StartupInfo.hStdInput;
        break;
      case 1:
        target = &sinfo.StartupInfo.hStdOutput;
        break;
      case 2:
        target = &sinfo.StartupInfo.hStdError;
        break;
    }

    if (!target) {
      w_log(W_LOG_ERR, "posix_spawn: can't target fd outside range [0-2]\n");
      ret = ENOSYS;
      goto done;
    }

    if (act->action != _posix_spawn_file_action::open_file) {
      // Process a dup(2) action
      DWORD err;

      if (*target) {
        CloseHandle(*target);
        *target = INVALID_HANDLE_VALUE;
      }

      if (act->action == _posix_spawn_file_action::dup_fd) {
        HANDLE src = NULL;
        switch (act->u.source_fd) {
          case 0:
            src = sinfo.StartupInfo.hStdInput;
            break;
          case 1:
            src = sinfo.StartupInfo.hStdOutput;
            break;
          case 2:
            src = sinfo.StartupInfo.hStdError;
            break;
        }
        if (!src) {
          src = (HANDLE)_get_osfhandle(act->u.source_fd);
        }
        act->u.dup_local_handle = intptr_t(src);
      }

      if (!DuplicateHandle(
              GetCurrentProcess(),
              (HANDLE)act->u.dup_local_handle,
              GetCurrentProcess(),
              target,
              0,
              TRUE,
              DUPLICATE_SAME_ACCESS)) {
        err = GetLastError();
        w_log(W_LOG_ERR, "posix_spawn: failed to duplicate handle: %s\n",
            win32_strerror(err));
        ret = map_win32_err(err);
        goto done;
      }
    } else {
      // Process an open(2) action

      auto h = w_handle_open(
          act->u.open_info.name, act->u.open_info.flags & ~O_CLOEXEC);
      if (!h) {
        ret = errno;
        w_log(W_LOG_ERR, "posix_spawn: failed to open %s:\n",
            act->u.open_info.name);
        goto done;
      }

      if (*target) {
        CloseHandle(*target);
      }
      *target = (HANDLE)h.release();
    }
  }

  if (!sinfo.StartupInfo.hStdInput) {
    sinfo.StartupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
  }
  if (!sinfo.StartupInfo.hStdOutput) {
    sinfo.StartupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  }
  if (!sinfo.StartupInfo.hStdError) {
    sinfo.StartupInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
  }

  // Ensure that we only pass the stdio handles to the child.
  {
    SIZE_T size = 0;

    inherited_handles[0] = sinfo.StartupInfo.hStdInput;
    inherited_handles[1] = sinfo.StartupInfo.hStdOutput;
    inherited_handles[2] = sinfo.StartupInfo.hStdError;
    sinfo.lpAttributeList = NULL;

    InitializeProcThreadAttributeList(NULL, 1, 0, &size);
    sinfo.lpAttributeList = (LPPROC_THREAD_ATTRIBUTE_LIST)malloc(size);
    InitializeProcThreadAttributeList(sinfo.lpAttributeList, 1, 0, &size);
    UpdateProcThreadAttribute(sinfo.lpAttributeList, 0,
        PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
        inherited_handles, 3 * sizeof(HANDLE),
        NULL, NULL);
  }

  if (!CreateProcess(search_path ? NULL : path,
        cmdbuf, &sec, &sec, TRUE, create_flags, env_block,
        attrp->working_dir, &sinfo.StartupInfo, &pinfo)) {
    w_log(W_LOG_ERR, "CreateProcess: `%s`: (cwd=%s) %s\n", cmdbuf,
        attrp->working_dir ? attrp->working_dir : "<process cwd>",
        win32_strerror(GetLastError()));
    ret = EACCES;
  } else {
    *pid = (pid_t)pinfo.dwProcessId;

    // Record the pid -> handle mapping for later wait/reap
    child_procs.wlock()->emplace(pinfo.dwProcessId, pinfo.hProcess);

    CloseHandle(pinfo.hThread);
    ret = 0;
  }

  free(sinfo.lpAttributeList);

done:
  free(cmdbuf);
  free(env_block);

  // If we manufactured any handles, close them out now
  if (sinfo.StartupInfo.hStdInput != GetStdHandle(STD_INPUT_HANDLE)) {
    CloseHandle(sinfo.StartupInfo.hStdInput);
  }
  if (sinfo.StartupInfo.hStdOutput != GetStdHandle(STD_OUTPUT_HANDLE)) {
    CloseHandle(sinfo.StartupInfo.hStdOutput);
  }
  if (sinfo.StartupInfo.hStdError != GetStdHandle(STD_ERROR_HANDLE)) {
    CloseHandle(sinfo.StartupInfo.hStdError);
  }

  return ret;
}

int posix_spawn(pid_t *pid, const char *path,
    const posix_spawn_file_actions_t *file_actions,
    const posix_spawnattr_t *attrp,
    char *const argv[], char *const envp[]) {

  return posix_spawn_common(false, pid, path, file_actions, attrp, argv, envp);
}

int posix_spawnp(pid_t *pid, const char *file,
    const posix_spawn_file_actions_t *file_actions,
    const posix_spawnattr_t *attrp,
    char *const argv[], char *const envp[]) {

  return posix_spawn_common(true, pid, file, file_actions, attrp, argv, envp);
}