Package: bazel-bootstrap / 4.2.3+ds-11

rename-daemonize.patch Patch series | 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
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
444
Description: Rename daemonize.c to daemonize.cc
 The .c extension was preventing it from receiving build hardening flags.
Author: Yun Peng <pcloudy@google.com>
Origin: upstream, https:\\salsa.debian.org/bazel-team/bazel-bootstrap/-/commit/844f15bc72f5f9ce64a36c4d1ad8bda42def74a5
Forwarded: not-needed
Last-Update: 2020-08-12

--- a/src/main/tools/daemonize.c
+++ /dev/null
@@ -1,210 +0,0 @@
-// Copyright 2019 The Bazel Authors. All rights reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//    http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-// daemonize [-a] -l log_path -p pid_path -- binary_path binary_name [args]
-//
-// daemonize spawns a program as a daemon, redirecting all of its output to the
-// given log_path and writing the daemon's PID to pid_path.  binary_path
-// specifies the full location of the program to execute and binary_name
-// indicates its display name (aka argv[0], so the optional args do not have to
-// specify it again).  log_path is created/truncated unless the -a (append) flag
-// is specified.  Also note that pid_path is guaranteed to exists when this
-// program terminates successfully.
-//
-// Some important details about the implementation of this program:
-//
-// * No threads to ensure the use of fork below does not cause trouble.
-//
-// * Pure C, no C++. This is intentional to keep the program low overhead
-//   and to avoid the accidental introduction of heavy dependencies that
-//   could spawn threads.
-//
-// * Error handling is extensive but there is no error propagation.  Given
-//   that the goal of this program is just to spawn another one as a daemon,
-//   we take the freedom to immediatey exit from anywhere as soon as we
-//   hit an error.
-
-#include <sys/types.h>
-
-#include <assert.h>
-#include <err.h>
-#include <fcntl.h>
-#include <getopt.h>
-#include <inttypes.h>
-#include <signal.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-// Configures std{in,out,err} of the current process to serve as a daemon.
-//
-// stdin is configured to read from /dev/null.
-//
-// stdout and stderr are configured to write to log_path, which is created and
-// truncated unless log_append is set to true, in which case it is open for
-// append if it exists.
-static void SetupStdio(const char* log_path, bool log_append) {
-  close(STDIN_FILENO);
-  int fd = open("/dev/null", O_RDONLY);
-  if (fd == -1) {
-    err(EXIT_FAILURE, "Failed to open /dev/null");
-  }
-  assert(fd == STDIN_FILENO);
-
-  close(STDOUT_FILENO);
-  int flags = O_WRONLY | O_CREAT | (log_append ? O_APPEND : O_TRUNC);
-  fd = open(log_path, flags, 0666);
-  if (fd == -1) {
-    err(EXIT_FAILURE, "Failed to create log file %s", log_path);
-  }
-  assert(fd == STDOUT_FILENO);
-
-  close(STDERR_FILENO);
-  fd = dup(STDOUT_FILENO);
-  if (fd == -1) {
-    err(EXIT_FAILURE, "dup failed");
-  }
-  assert(fd == STDERR_FILENO);
-}
-
-// Writes the given pid to a new file at pid_path.
-//
-// Once the pid file has been created, this notifies pid_done_fd by writing a
-// dummy character to it and closing it.
-static void WritePidFile(pid_t pid, const char* pid_path, int pid_done_fd) {
-  FILE* pid_file = fopen(pid_path, "w");
-  if (pid_file == NULL) {
-    err(EXIT_FAILURE, "Failed to create %s", pid_path);
-  }
-  fprintf(pid_file, "%" PRIdMAX, (intmax_t) pid);
-  fclose(pid_file);
-
-  char dummy = '\0';
-  write(pid_done_fd, &dummy, sizeof(dummy));
-  close(pid_done_fd);
-}
-
-static void ExecAsDaemon(const char* log_path, bool log_append, int pid_done_fd,
-                         const char* exe, char** argv)
-    __attribute__((noreturn));
-
-// Executes the requested binary configuring it to behave as a daemon.
-//
-// The stdout and stderr of the current process are redirected to the given
-// log_path.  See SetupStdio for details on how this is handled.
-//
-// This blocks execution until pid_done_fd receives a write.  We do this
-// because the Bazel server process (which is what we start with this helper
-// binary) requires the PID file to be present at startup time so we must
-// wait until the parent process has created it.
-//
-// This function never returns.
-static void ExecAsDaemon(const char* log_path, bool log_append, int pid_done_fd,
-                         const char* exe, char** argv) {
-  char dummy;
-  if (read(pid_done_fd, &dummy, sizeof(dummy)) == -1) {
-    err(EXIT_FAILURE, "Failed to wait for pid file creation");
-  }
-  close(pid_done_fd);
-
-  if (signal(SIGHUP, SIG_IGN) == SIG_ERR) {
-    err(EXIT_FAILURE, "Failed to install SIGHUP handler");
-  }
-
-  if (setsid() == -1) {
-    err(EXIT_FAILURE, "setsid failed");
-  }
-
-  SetupStdio(log_path, log_append);
-
-  execv(exe, argv);
-  err(EXIT_FAILURE, "Failed to execute %s", exe);
-}
-
-// Starts the given process as a daemon.
-//
-// This spawns a subprocess that will be configured to run the desired program
-// as a daemon.  The program to run is supplied in exe and the arguments to it
-// are given in the NULL-terminated argv.  argv[0] must be present and
-// contain the program name (which may or may not match the basename of exe).
-static void Daemonize(const char* log_path, bool log_append,
-                      const char* pid_path, const char* exe, char** argv) {
-  assert(argv[0] != NULL);
-
-  int pid_done_fds[2];
-  if (pipe(pid_done_fds) == -1) {
-    err(EXIT_FAILURE, "pipe failed");
-  }
-
-  pid_t pid = fork();
-  if (pid == -1) {
-    err(EXIT_FAILURE, "fork failed");
-  } else if (pid == 0) {
-    close(pid_done_fds[1]);
-    ExecAsDaemon(log_path, log_append, pid_done_fds[0], exe, argv);
-    abort();  // NOLINT Unreachable.
-  }
-  close(pid_done_fds[0]);
-
-  WritePidFile(pid, pid_path, pid_done_fds[1]);
-}
-
-// Program entry point.
-//
-// The primary responsibility of this function is to parse program options.
-// Once that is done, delegates all work to Daemonize.
-int main(int argc, char** argv) {
-  bool log_append = false;
-  const char* log_path = NULL;
-  const char* pid_path = NULL;
-  int opt;
-  while ((opt = getopt(argc, argv, ":al:p:")) != -1) {
-    switch (opt) {
-      case 'a':
-        log_append = true;
-        break;
-
-      case 'l':
-        log_path = optarg;
-        break;
-
-      case 'p':
-        pid_path = optarg;
-        break;
-
-      case ':':
-        errx(EXIT_FAILURE, "Option -%c requires an argument", optopt);
-
-      case '?':
-      default:
-        errx(EXIT_FAILURE, "Unknown option -%c", optopt);
-    }
-  }
-  argc -= optind;
-  argv += optind;
-
-  if (log_path == NULL) {
-    errx(EXIT_FAILURE, "Must specify a log file with -l");
-  }
-  if (pid_path == NULL) {
-    errx(EXIT_FAILURE, "Must specify a pid file with -p");
-  }
-
-  if (argc < 2) {
-    errx(EXIT_FAILURE, "Must provide at least an executable name and arg0");
-  }
-  Daemonize(log_path, log_append, pid_path, argv[0], argv + 1);
-  return EXIT_SUCCESS;
-}
--- /dev/null
+++ b/src/main/tools/daemonize.cc
@@ -0,0 +1,210 @@
+// Copyright 2019 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// daemonize [-a] -l log_path -p pid_path -- binary_path binary_name [args]
+//
+// daemonize spawns a program as a daemon, redirecting all of its output to the
+// given log_path and writing the daemon's PID to pid_path.  binary_path
+// specifies the full location of the program to execute and binary_name
+// indicates its display name (aka argv[0], so the optional args do not have to
+// specify it again).  log_path is created/truncated unless the -a (append) flag
+// is specified.  Also note that pid_path is guaranteed to exists when this
+// program terminates successfully.
+//
+// Some important details about the implementation of this program:
+//
+// * No threads to ensure the use of fork below does not cause trouble.
+//
+// * Pure C, no C++. This is intentional to keep the program low overhead
+//   and to avoid the accidental introduction of heavy dependencies that
+//   could spawn threads.
+//
+// * Error handling is extensive but there is no error propagation.  Given
+//   that the goal of this program is just to spawn another one as a daemon,
+//   we take the freedom to immediatey exit from anywhere as soon as we
+//   hit an error.
+
+#include <sys/types.h>
+
+#include <assert.h>
+#include <err.h>
+#include <fcntl.h>
+#include <getopt.h>
+#include <inttypes.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+// Configures std{in,out,err} of the current process to serve as a daemon.
+//
+// stdin is configured to read from /dev/null.
+//
+// stdout and stderr are configured to write to log_path, which is created and
+// truncated unless log_append is set to true, in which case it is open for
+// append if it exists.
+static void SetupStdio(const char* log_path, bool log_append) {
+  close(STDIN_FILENO);
+  int fd = open("/dev/null", O_RDONLY);
+  if (fd == -1) {
+    err(EXIT_FAILURE, "Failed to open /dev/null");
+  }
+  assert(fd == STDIN_FILENO);
+
+  close(STDOUT_FILENO);
+  int flags = O_WRONLY | O_CREAT | (log_append ? O_APPEND : O_TRUNC);
+  fd = open(log_path, flags, 0666);
+  if (fd == -1) {
+    err(EXIT_FAILURE, "Failed to create log file %s", log_path);
+  }
+  assert(fd == STDOUT_FILENO);
+
+  close(STDERR_FILENO);
+  fd = dup(STDOUT_FILENO);
+  if (fd == -1) {
+    err(EXIT_FAILURE, "dup failed");
+  }
+  assert(fd == STDERR_FILENO);
+}
+
+// Writes the given pid to a new file at pid_path.
+//
+// Once the pid file has been created, this notifies pid_done_fd by writing a
+// dummy character to it and closing it.
+static void WritePidFile(pid_t pid, const char* pid_path, int pid_done_fd) {
+  FILE* pid_file = fopen(pid_path, "w");
+  if (pid_file == NULL) {
+    err(EXIT_FAILURE, "Failed to create %s", pid_path);
+  }
+  fprintf(pid_file, "%" PRIdMAX, (intmax_t) pid);
+  fclose(pid_file);
+
+  char dummy = '\0';
+  write(pid_done_fd, &dummy, sizeof(dummy));
+  close(pid_done_fd);
+}
+
+static void ExecAsDaemon(const char* log_path, bool log_append, int pid_done_fd,
+                         const char* exe, char** argv)
+    __attribute__((noreturn));
+
+// Executes the requested binary configuring it to behave as a daemon.
+//
+// The stdout and stderr of the current process are redirected to the given
+// log_path.  See SetupStdio for details on how this is handled.
+//
+// This blocks execution until pid_done_fd receives a write.  We do this
+// because the Bazel server process (which is what we start with this helper
+// binary) requires the PID file to be present at startup time so we must
+// wait until the parent process has created it.
+//
+// This function never returns.
+static void ExecAsDaemon(const char* log_path, bool log_append, int pid_done_fd,
+                         const char* exe, char** argv) {
+  char dummy;
+  if (read(pid_done_fd, &dummy, sizeof(dummy)) == -1) {
+    err(EXIT_FAILURE, "Failed to wait for pid file creation");
+  }
+  close(pid_done_fd);
+
+  if (signal(SIGHUP, SIG_IGN) == SIG_ERR) {
+    err(EXIT_FAILURE, "Failed to install SIGHUP handler");
+  }
+
+  if (setsid() == -1) {
+    err(EXIT_FAILURE, "setsid failed");
+  }
+
+  SetupStdio(log_path, log_append);
+
+  execv(exe, argv);
+  err(EXIT_FAILURE, "Failed to execute %s", exe);
+}
+
+// Starts the given process as a daemon.
+//
+// This spawns a subprocess that will be configured to run the desired program
+// as a daemon.  The program to run is supplied in exe and the arguments to it
+// are given in the NULL-terminated argv.  argv[0] must be present and
+// contain the program name (which may or may not match the basename of exe).
+static void Daemonize(const char* log_path, bool log_append,
+                      const char* pid_path, const char* exe, char** argv) {
+  assert(argv[0] != NULL);
+
+  int pid_done_fds[2];
+  if (pipe(pid_done_fds) == -1) {
+    err(EXIT_FAILURE, "pipe failed");
+  }
+
+  pid_t pid = fork();
+  if (pid == -1) {
+    err(EXIT_FAILURE, "fork failed");
+  } else if (pid == 0) {
+    close(pid_done_fds[1]);
+    ExecAsDaemon(log_path, log_append, pid_done_fds[0], exe, argv);
+    abort();  // NOLINT Unreachable.
+  }
+  close(pid_done_fds[0]);
+
+  WritePidFile(pid, pid_path, pid_done_fds[1]);
+}
+
+// Program entry point.
+//
+// The primary responsibility of this function is to parse program options.
+// Once that is done, delegates all work to Daemonize.
+int main(int argc, char** argv) {
+  bool log_append = false;
+  const char* log_path = NULL;
+  const char* pid_path = NULL;
+  int opt;
+  while ((opt = getopt(argc, argv, ":al:p:")) != -1) {
+    switch (opt) {
+      case 'a':
+        log_append = true;
+        break;
+
+      case 'l':
+        log_path = optarg;
+        break;
+
+      case 'p':
+        pid_path = optarg;
+        break;
+
+      case ':':
+        errx(EXIT_FAILURE, "Option -%c requires an argument", optopt);
+
+      case '?':
+      default:
+        errx(EXIT_FAILURE, "Unknown option -%c", optopt);
+    }
+  }
+  argc -= optind;
+  argv += optind;
+
+  if (log_path == NULL) {
+    errx(EXIT_FAILURE, "Must specify a log file with -l");
+  }
+  if (pid_path == NULL) {
+    errx(EXIT_FAILURE, "Must specify a pid file with -p");
+  }
+
+  if (argc < 2) {
+    errx(EXIT_FAILURE, "Must provide at least an executable name and arg0");
+  }
+  Daemonize(log_path, log_append, pid_path, argv[0], argv + 1);
+  return EXIT_SUCCESS;
+}
--- a/src/main/tools/BUILD
+++ b/src/main/tools/BUILD
@@ -4,7 +4,7 @@
 
 cc_binary(
     name = "daemonize",
-    srcs = ["daemonize.c"],
+    srcs = ["daemonize.cc"],
 )
 
 cc_library(