/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8                               :vi │
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2020 Justine Alexandra Roberts Tunney                              │
│                                                                              │
│ Permission to use, copy, modify, and/or distribute this software for         │
│ any purpose with or without fee is hereby granted, provided that the         │
│ above copyright notice and this permission notice appear in all copies.      │
│                                                                              │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL                │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED                │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE             │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL         │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR        │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER               │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR             │
│ PERFORMANCE OF THIS SOFTWARE.                                                │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/sig.internal.h"
#include "libc/calls/struct/sigset.internal.h"
#include "libc/calls/syscall-nt.internal.h"
#include "libc/errno.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/fds.h"
#include "libc/mem/mem.h"
#include "libc/nt/enum/processaccess.h"
#include "libc/nt/enum/startf.h"
#include "libc/nt/errors.h"
#include "libc/nt/files.h"
#include "libc/nt/process.h"
#include "libc/nt/runtime.h"
#include "libc/nt/struct/processinformation.h"
#include "libc/nt/struct/startupinfo.h"
#include "libc/nt/thunk/msabi.h"
#include "libc/proc/describefds.internal.h"
#include "libc/proc/ntspawn.h"
#include "libc/runtime/internal.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/at.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/errfuns.h"
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/thread.h"
#ifdef __x86_64__

__msabi extern typeof(TerminateProcess) *const __imp_TerminateProcess;

extern pthread_mutex_t __sig_worker_lock;

static void sys_execve_nt_abort(sigset_t sigmask) {
  _pthread_unlock();
  _pthread_mutex_unlock(&__sig_worker_lock);
  __sig_unblock(sigmask);
}

textwindows int sys_execve_nt(const char *program, char *const argv[],
                              char *const envp[]) {

  // execve() needs to be @asyncsignalsafe
  sigset_t sigmask = __sig_block();
  _pthread_mutex_lock(&__sig_worker_lock);  // order matters
  _pthread_lock();                          // order matters

  // new process should be a child of our parent
  int64_t hParentProcess;
  int ppid = sys_getppid_nt();
  if (!(hParentProcess = OpenProcess(
            kNtProcessDupHandle | kNtProcessCreateProcess, false, ppid))) {
    sys_execve_nt_abort(sigmask);
    return -1;
  }

  // inherit pid
  char pidvar[11 + 21];
  FormatUint64(stpcpy(pidvar, "_COSMO_PID="), __pid);

  // inherit signal mask
  char maskvar[6 + 21];
  FormatUint64(stpcpy(maskvar, "_MASK="), sigmask);

  // define stdio handles for the spawned subprocess
  struct NtStartupInfo si = {
      .cb = sizeof(struct NtStartupInfo),
      .dwFlags = kNtStartfUsestdhandles,
  };
  for (int fd = 0; fd < 3; ++fd) {
    if (!__is_cloexec(g_fds.p + fd)) {
      si.stdiofds[fd] = g_fds.p[fd].handle;
    } else {
      si.stdiofds[fd] = -1;
    }
  }

  // pass serialized file descriptor table in environment
  char *fdspec;
  int64_t *lpExplicitHandles;
  uint32_t dwExplicitHandleCount;
  if (!(fdspec = __describe_fds(g_fds.p, g_fds.n, &si, hParentProcess,
                                &lpExplicitHandles, &dwExplicitHandleCount))) {
    CloseHandle(hParentProcess);
    sys_execve_nt_abort(sigmask);
    return -1;
  }

  // inherit pending signals
  atomic_fetch_or_explicit(
      __sig.process,
      atomic_load_explicit(&__get_tls()->tib_sigpending, memory_order_acquire),
      memory_order_release);

  // launch the process
  struct NtProcessInformation pi;
  int rc = ntspawn(&(struct NtSpawnArgs){
      AT_FDCWD, program, argv, envp, (char *[]){fdspec, maskvar, pidvar, 0}, 0,
      0, hParentProcess, lpExplicitHandles, dwExplicitHandleCount, &si, &pi});
  __undescribe_fds(hParentProcess, lpExplicitHandles, dwExplicitHandleCount);
  if (rc == -1) {
    free(fdspec);
    CloseHandle(hParentProcess);
    sys_execve_nt_abort(sigmask);
    if (GetLastError() == kNtErrorSharingViolation) {
      return etxtbsy();
    } else {
      return -1;
    }
  }

  // give child to libc/proc/proc.c worker thread in parent
  int64_t handle;
  if (DuplicateHandle(GetCurrentProcess(), pi.hProcess, hParentProcess, &handle,
                      0, false, kNtDuplicateSameAccess)) {
    unassert(!(handle & 0xFFFFFFFFFF000000));
    __imp_TerminateProcess(-1, 0x23000000u | handle);
  } else {
    // TODO(jart): Why does `make loc` print this?
    // kprintf("DuplicateHandle failed w/ %d\n", GetLastError());
    __imp_TerminateProcess(-1, ECHILD);
  }
  __builtin_unreachable();
}

#endif /* __x86_64__ */
