aboutsummaryrefslogtreecommitdiff
path: root/src/proc.c
blob: 5c6fe83770400ac1c8e04e81cbecbacec7f7fd94 (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
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello <[email protected]>

#include <assert.h>
#include <fcntl.h>
#include <paths.h>
#include <spawn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>

#include "dbx.h"

extern char **environ;

static const char *get_path(void) {
  const char *path = getenv("PATH");
  if (path == NULL || path[0] == '\0') {
    // In case `PATH` is not specified, use the default search path.
    return _PATH_DEFPATH;
  }
  return path;
}

static const char *next_path_segment(char **path) {
  char *segment = *path;
  if (segment != NULL) {
    *path = strchr(segment, ':');
    if (*path != NULL) {
      *((*path)++) = '\0';
    }

    if (segment[0] == '\0') {
      // POSIX defines an empty PATH segment to mean "current directory"
      segment = ".";
    }
  }

  return segment;
}

bool dbx_proc_find(const char *name, char file[PATH_MAX]) {
  assert(name != NULL && name[0] != '\0' && strchr(name, '/') == NULL &&
         file != NULL);

  char *path = strdup(get_path());
  if (path == NULL) {
    dbx_perror("strdup", ENOMEM);
    return false;
  }

  char *pos = path;
  bool found = false;
  for (const char *dir = next_path_segment(&pos); dir != NULL;
       dir = next_path_segment(&pos)) {
    char candidate[PATH_MAX];
    int n = snprintf(candidate, sizeof(candidate), "%s/%s", dir, name);
    if (n < 0 || n >= (int)sizeof(candidate)) {
      continue;
    }

    struct stat st;
    if (realpath(candidate, file) != NULL && access(file, X_OK) == 0 &&
        stat(file, &st) == 0 && S_ISREG(st.st_mode)) {
      found = true;
      break;
    }
  }

  free(path);
  return found;
}

int dbx_proc_run(const char *const command[], enum dbx_proc_output out) {
  assert(command != NULL && command[0] != NULL);

  int result;
  posix_spawn_file_actions_t file_actions;
  if ((result = posix_spawn_file_actions_init(&file_actions)) != 0) {
    dbx_perror("spawn_file_actions_init", result);
    return -1;
  }

  int exit_code = -1;
  result = posix_spawn_file_actions_addclose(&file_actions, STDIN_FILENO);
  if (result != 0) {
    dbx_perror("posix_spawn_file_actions_addclose(stdin)", result);
    goto cleanup;
  }

  if ((out & DBXP_STDOUT) == 0) {
    result = posix_spawn_file_actions_addopen(&file_actions, STDOUT_FILENO,
                                              _PATH_DEVNULL, O_WRONLY, 0);
    if (result != 0) {
      dbx_perror("posix_spawn_file_actions_addopen(stdout)", result);
      goto cleanup;
    }
  }

  if ((out & DBXP_STDERR) == 0) {
    result = posix_spawn_file_actions_addopen(&file_actions, STDERR_FILENO,
                                              _PATH_DEVNULL, O_WRONLY, 0);
    if (result != 0) {
      dbx_perror("posix_spawn_file_actions_addopen(stderr)", result);
      goto cleanup;
    }
  }

  // POSIX guarantees that the pointer is not modified, so the cast is safe.
  char *const *argv = (char *const *)command;

  pid_t pid;
  result = posix_spawnp(&pid, command[0], &file_actions, NULL, argv, environ);
  if (result != 0) {
    dbx_perror("posix_spawnp", result);
    goto cleanup;
  }

  int status;
  if (waitpid(pid, &status, 0) != pid) {
    dbx_perror("waitpid", errno);
    goto cleanup;
  }

  if (WIFEXITED(status)) {
    exit_code = WEXITSTATUS(status);
  } else if (WIFSIGNALED(status)) {
    exit_code = 128 + WTERMSIG(status);
  }

cleanup:
  result = posix_spawn_file_actions_destroy(&file_actions);
  if (result != 0) {
    dbx_perror("posix_spawn_file_actions_destroy", result);
    // Don't change the exit code on cleanup failure: issues cleaning up do not
    // change the status of the child process's execution.
  }
  return exit_code;
}

bool dbx_proc_exec(const char *const command[]) {
  assert(command != NULL && command[0] != NULL);

  // POSIX guarantees that the pointer is not modified, so the cast is safe.
  char *const *argv = (char *const *)command;

  execvp(command[0], argv);

  // execvp only ever returns if there was an error.
  dbx_perror("execv", errno);
  return false;
}