forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
627 lines (542 loc) · 14.6 KB
/
main.c
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/* Copyright 2012-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "watchman.h"
#include <poll.h>
static int show_help = 0;
static int show_version = 0;
static enum w_pdu_type server_pdu = is_json_compact;
static enum w_pdu_type output_pdu = is_json_pretty;
static char *server_encoding = NULL;
static char *output_encoding = NULL;
static char *sock_name = NULL;
static char *log_name = NULL;
#ifdef USE_GIMLI
static char *pid_file = NULL;
#endif
char *watchman_state_file = NULL;
static char **daemon_argv = NULL;
const char *watchman_tmp_dir = NULL;
static int persistent = 0;
int dont_save_state = 0;
static int foreground = 0;
static int no_pretty = 0;
static int no_spawn = 0;
static int no_local = 0;
static struct sockaddr_un un;
static int json_input_arg = 0;
#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif
#if defined(USE_GIMLI) || defined(__APPLE__)
# define SPAWN_VIA_LAUNCHER 1
#endif
static void run_service(void)
{
int fd;
bool res;
// redirect std{in,out,err}
fd = open("/dev/null", O_RDONLY);
if (fd != -1) {
dup2(fd, STDIN_FILENO);
close(fd);
}
fd = open(log_name, O_WRONLY|O_APPEND|O_CREAT, 0600);
if (fd != -1) {
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
close(fd);
}
/* we are the child, let's set things up */
ignore_result(chdir("/"));
watchman_watcher_init();
res = w_start_listener(sock_name);
watchman_watcher_dtor();
if (res) {
exit(0);
}
exit(1);
}
#ifndef USE_GIMLI
static void daemonize(void)
{
// the double-fork-and-setsid trick establishes a
// child process that runs in its own process group
// with its own session and that won't get killed
// off when your shell exits (for example).
if (fork()) {
// The parent of the first fork is the client
// process that is being run by the user, and
// we want to allow that to continue.
return;
}
setsid();
if (fork()) {
// The parent of the second fork has served its
// purpose, so we simply exit here, otherwise
// we'll duplicate the effort of either the
// client or the server depending on if we
// return or not.
_exit(0);
}
// we are the child, let's set things up
run_service();
}
#endif
#ifdef SPAWN_VIA_LAUNCHER
#define MAX_DAEMON_ARGS 64
static void append_argv(char **argv, char *item)
{
int i;
for (i = 0; argv[i]; i++) {
;
}
if (i + 1 >= MAX_DAEMON_ARGS) {
abort();
}
argv[i] = item;
argv[i+1] = NULL;
}
#ifdef USE_GIMLI
static void spawn_via_gimli(void)
{
char *argv[MAX_DAEMON_ARGS] = {
GIMLI_MONITOR_PATH,
#ifdef WATCHMAN_STATE_DIR
"--trace-dir=" WATCHMAN_STATE_DIR "/traces",
#endif
"--pidfile", pid_file,
"watchman",
"--foreground",
NULL
};
posix_spawn_file_actions_t actions;
posix_spawnattr_t attr;
pid_t pid;
int i;
for (i = 0; daemon_argv[i]; i++) {
append_argv(argv, daemon_argv[i]);
}
posix_spawnattr_init(&attr);
posix_spawn_file_actions_init(&actions);
posix_spawn_file_actions_addopen(&actions,
STDOUT_FILENO, log_name, O_WRONLY|O_CREAT|O_APPEND, 0600);
posix_spawn_file_actions_adddup2(&actions,
STDOUT_FILENO, STDERR_FILENO);
posix_spawnp(&pid, argv[0], &actions, &attr, argv, environ);
posix_spawnattr_destroy(&attr);
posix_spawn_file_actions_destroy(&actions);
}
#endif
#ifdef __APPLE__
static void spawn_via_launchd(void)
{
char watchman_path[WATCHMAN_NAME_MAX];
uint32_t size = sizeof(watchman_path);
char plist_path[WATCHMAN_NAME_MAX];
FILE *fp;
struct passwd *pw;
uid_t uid;
char *argv[MAX_DAEMON_ARGS] = {
"/bin/launchctl",
"load",
"-F",
NULL
};
posix_spawnattr_t attr;
pid_t pid;
int res;
if (_NSGetExecutablePath(watchman_path, &size) == -1) {
w_log(W_LOG_ERR, "_NSGetExecutablePath: path too long; size %u\n", size);
abort();
}
uid = getuid();
pw = getpwuid(uid);
if (!pw) {
w_log(W_LOG_ERR, "getpwuid(%d) failed: %s. I don't know who you are\n",
uid, strerror(errno));
abort();
}
snprintf(plist_path, sizeof(plist_path),
"%s/Library/LaunchAgents", pw->pw_dir);
// Best effort attempt to ensure that the agents dir exists. We'll detect
// and report the failure in the fopen call below.
mkdir(plist_path, 0755);
snprintf(plist_path, sizeof(plist_path),
"%s/Library/LaunchAgents/com.github.facebook.watchman.plist", pw->pw_dir);
if (access(plist_path, R_OK) == 0) {
// Unload any that may already exist, as it is likely wrong
char *unload_argv[MAX_DAEMON_ARGS] = {
"/bin/launchctl",
"unload",
NULL
};
append_argv(unload_argv, plist_path);
errno = posix_spawnattr_init(&attr);
if (errno != 0) {
w_log(W_LOG_FATAL, "posix_spawnattr_init: %s\n", strerror(errno));
}
res = posix_spawnp(&pid, unload_argv[0], NULL, &attr, unload_argv, environ);
if (res == 0) {
waitpid(pid, &res, 0);
}
posix_spawnattr_destroy(&attr);
}
fp = fopen(plist_path, "w");
if (!fp) {
w_log(W_LOG_ERR, "Failed to open %s for write: %s\n",
plist_path, strerror(errno));
abort();
}
fprintf(fp,
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" "
"\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
"<plist version=\"1.0\">\n"
"<dict>\n"
" <key>Label</key>\n"
" <string>com.github.facebook.watchman</string>\n"
" <key>Disabled</key>\n"
" <false/>\n"
" <key>ProgramArguments</key>\n"
" <array>\n"
" <string>%s</string>\n"
" <string>--foreground</string>\n"
" <string>--logfile=%s</string>\n"
" <string>--sockname=%s</string>\n"
" <string>--statefile=%s</string>\n"
" </array>\n"
" <key>Sockets</key>\n"
" <dict>\n"
" <key>sock</key>\n" // coupled with get_listener_socket_from_launchd
" <dict>\n"
" <key>SockPathName</key>\n"
" <string>%s</string>\n"
" </dict>\n"
" </dict>\n"
" <key>KeepAlive</key>\n"
" <dict>\n"
" <key>Crashed</key>\n"
" <true/>\n"
" </dict>\n"
" <key>RunAtLoad</key>\n"
" <false/>\n"
"</dict>\n"
"</plist>\n",
watchman_path, log_name, sock_name, watchman_state_file, sock_name);
fclose(fp);
append_argv(argv, plist_path);
errno = posix_spawnattr_init(&attr);
if (errno != 0) {
w_log(W_LOG_FATAL, "posix_spawnattr_init: %s\n", strerror(errno));
}
res = posix_spawnp(&pid, argv[0], NULL, &attr, argv, environ);
if (res) {
w_log(W_LOG_FATAL, "Failed to spawn watchman via launchd: %s\n",
strerror(errno));
}
posix_spawnattr_destroy(&attr);
if (waitpid(pid, &res, 0) == -1) {
w_log(W_LOG_FATAL, "Failed waiting for launchctl load: %s\n",
strerror(errno));
}
if (WIFEXITED(res) && WEXITSTATUS(res) == 0) {
return;
}
// Most likely cause is "headless" operation with no GUI context
if (WIFEXITED(res)) {
w_log(W_LOG_ERR, "launchctl: exited with status %d\n", WEXITSTATUS(res));
} else if (WIFSIGNALED(res)) {
w_log(W_LOG_ERR, "launchctl: signalled with %d\n", WTERMSIG(res));
}
w_log(W_LOG_ERR, "Falling back to daemonize\n");
daemonize();
}
#endif
#endif // SPAWN_VIA_LAUNCHER
static void parse_encoding(const char *enc, enum w_pdu_type *pdu)
{
if (!enc) {
return;
}
if (!strcmp(enc, "json")) {
*pdu = is_json_compact;
return;
}
if (!strcmp(enc, "bser")) {
*pdu = is_bser;
return;
}
w_log(W_LOG_ERR, "Invalid encoding '%s', use one of json or bser\n", enc);
exit(EX_USAGE);
}
static const char *get_env_with_fallback(const char *name1,
const char *name2, const char *fallback)
{
const char *val;
val = getenv(name1);
if (!val || *val == 0) {
val = getenv(name2);
}
if (!val || *val == 0) {
val = fallback;
}
return val;
}
static void compute_file_name(char **strp,
const char *user,
const char *suffix,
const char *what)
{
char *str = NULL;
str = *strp;
if (!str) {
#ifdef WATCHMAN_STATE_DIR
/* avoid redundant naming if they picked something like
* "/var/watchman" */
ignore_result(asprintf(&str, "%s/%s%s%s",
WATCHMAN_STATE_DIR,
user,
suffix[0] ? "." : "",
suffix));
#else
ignore_result(asprintf(&str, "%s/.watchman.%s%s%s",
watchman_tmp_dir,
user,
suffix[0] ? "." : "",
suffix));
#endif
}
if (!str) {
w_log(W_LOG_ERR, "out of memory computing %s", what);
abort();
}
if (str[0] != '/') {
w_log(W_LOG_ERR, "invalid %s: %s", what, str);
abort();
}
*strp = str;
}
static void setup_sock_name(void)
{
const char *user = get_env_with_fallback("USER", "LOGNAME", NULL);
watchman_tmp_dir = get_env_with_fallback("TMPDIR", "TMP", "/tmp");
if (!user) {
uid_t uid = getuid();
struct passwd *pw;
pw = getpwuid(uid);
if (!pw) {
w_log(W_LOG_ERR, "getpwuid(%d) failed: %s. I don't know who you are\n",
uid, strerror(errno));
abort();
}
user = pw->pw_name;
if (!user) {
w_log(W_LOG_ERR, "watchman requires that you set $USER in your env\n");
abort();
}
}
compute_file_name(&sock_name, user, "", "sockname");
compute_file_name(&watchman_state_file, user, "state", "statefile");
compute_file_name(&log_name, user, "log", "logname");
#ifdef USE_GIMLI
compute_file_name(&pid_file, user, "pid", "pidfile");
#endif
un.sun_family = PF_LOCAL;
strcpy(un.sun_path, sock_name);
if (strlen(sock_name) >= sizeof(un.sun_path) - 1) {
w_log(W_LOG_ERR, "%s: path is too long\n",
sock_name);
abort();
}
}
static bool should_start(int err)
{
if (err == ECONNREFUSED) {
return true;
}
if (err == ENOENT) {
return true;
}
return false;
}
static bool try_command(json_t *cmd, int timeout)
{
int fd;
int res;
int tries;
int bufsize;
w_jbuffer_t buffer;
fd = socket(PF_LOCAL, SOCK_STREAM, 0);
if (fd == -1) {
perror("socket");
return false;
}
tries = 0;
do {
res = connect(fd, (struct sockaddr*)&un, sizeof(un));
if (res == 0) {
break;
}
if (timeout && tries < timeout && should_start(errno)) {
// Wait for socket to come up
sleep(1);
continue;
}
} while (++tries < timeout);
if (res) {
close(fd);
return false;
}
if (!cmd) {
close(fd);
return true;
}
bufsize = WATCHMAN_IO_BUF_SIZE;
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(bufsize));
w_json_buffer_init(&buffer);
// Send command
if (!w_ser_write_pdu(server_pdu, &buffer, fd, cmd)) {
w_log(W_LOG_ERR, "error sending PDU to server\n");
w_json_buffer_free(&buffer);
close(fd);
return false;
}
w_json_buffer_reset(&buffer);
do {
if (!w_json_buffer_passthru(&buffer, output_pdu, fd)) {
w_json_buffer_free(&buffer);
close(fd);
return false;
}
} while (persistent);
w_json_buffer_free(&buffer);
close(fd);
return true;
}
static struct watchman_getopt opts[] = {
{ "help", 'h', "Show this help",
OPT_NONE, &show_help, NULL, NOT_DAEMON },
{ "version", 'v', "Show version number",
OPT_NONE, &show_version, NULL, NOT_DAEMON },
{ "sockname", 'U', "Specify alternate sockname",
REQ_STRING, &sock_name, "PATH", IS_DAEMON },
{ "logfile", 'o', "Specify path to logfile",
REQ_STRING, &log_name, "PATH", IS_DAEMON },
{ "log-level", 0, "set the log level (0 = off, default is 1, verbose = 2)",
REQ_INT, &log_level, NULL, IS_DAEMON },
#ifdef USE_GIMLI
{ "pidfile", 0, "Specify path to gimli monitor pidfile",
REQ_STRING, &pid_file, "PATH", NOT_DAEMON },
#endif
{ "persistent", 'p', "Persist and wait for further responses",
OPT_NONE, &persistent, NULL, NOT_DAEMON },
{ "no-save-state", 'n', "Don't save state between invocations",
OPT_NONE, &dont_save_state, NULL, IS_DAEMON },
{ "statefile", 0, "Specify path to file to hold watch and trigger state",
REQ_STRING, &watchman_state_file, "PATH", IS_DAEMON },
{ "json-command", 'j', "Instead of parsing CLI arguments, take a single "
"json object from stdin",
OPT_NONE, &json_input_arg, NULL, NOT_DAEMON },
{ "output-encoding", 0, "CLI output encoding. json (default) or bser",
REQ_STRING, &output_encoding, NULL, NOT_DAEMON },
{ "server-encoding", 0, "CLI<->server encoding. bser (default) or json",
REQ_STRING, &server_encoding, NULL, NOT_DAEMON },
{ "foreground", 'f', "Run the service in the foreground",
OPT_NONE, &foreground, NULL, NOT_DAEMON },
{ "no-pretty", 0, "Don't pretty print JSON",
OPT_NONE, &no_pretty, NULL, NOT_DAEMON },
{ "no-spawn", 0, "Don't try to start the service if it is not available",
OPT_NONE, &no_spawn, NULL, NOT_DAEMON },
{ "no-local", 0, "When no-spawn is enabled, don't try to handle request"
" in client mode if service is unavailable",
OPT_NONE, &no_local, NULL, NOT_DAEMON },
{ 0, 0, 0, 0, 0, 0, 0 }
};
static void parse_cmdline(int *argcp, char ***argvp)
{
cfg_load_global_config_file();
w_getopt(opts, argcp, argvp, &daemon_argv);
if (show_help) {
usage(opts, stdout);
}
if (show_version) {
printf("%s\n", PACKAGE_VERSION);
exit(0);
}
setup_sock_name();
parse_encoding(server_encoding, &server_pdu);
parse_encoding(output_encoding, &output_pdu);
if (!output_encoding) {
output_pdu = no_pretty ? is_json_compact : is_json_pretty;
}
}
static json_t *build_command(int argc, char **argv)
{
json_t *cmd;
int i;
// Read blob from stdin
if (json_input_arg) {
json_error_t err;
cmd = json_loadf(stdin, 0, &err);
if (cmd == NULL) {
fprintf(stderr, "failed to parse JSON from stdin: %s\n",
err.text);
exit(1);
}
return cmd;
}
// Special case: no arguments means that we just want
// to verify that the service is up, starting it if
// needed
if (argc == 0) {
return NULL;
}
cmd = json_array();
for (i = 0; i < argc; i++) {
json_array_append_new(cmd, json_string(argv[i]));
}
return cmd;
}
const char *get_sock_name(void)
{
return sock_name;
}
int main(int argc, char **argv)
{
bool ran;
json_t *cmd;
parse_cmdline(&argc, &argv);
if (foreground) {
run_service();
return 0;
}
cmd = build_command(argc, argv);
preprocess_command(cmd, output_pdu);
ran = try_command(cmd, 0);
if (!ran && should_start(errno)) {
if (no_spawn) {
if (!no_local) {
ran = try_client_mode_command(cmd, !no_pretty);
}
} else {
#ifdef USE_GIMLI
spawn_via_gimli();
#elif defined(__APPLE__)
spawn_via_launchd();
#else
daemonize();
#endif
ran = try_command(cmd, 10);
}
}
json_decref(cmd);
if (ran) {
return 0;
}
if (!no_spawn) {
w_log(W_LOG_ERR, "unable to talk to your watchman on %s!\n", sock_name);
}
return 1;
}
/* vim:ts=2:sw=2:et:
*/