static bool try_command(json_t *cmd, int timeout) { int fd; int res; int tries; 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; } w_json_buffer_init(&buffer); // Send command w_json_buffer_write(&buffer, fd, cmd, JSON_COMPACT); do { if (!read_response(&buffer, fd)) { w_json_buffer_free(&buffer); close(fd); return false; } } while (persistent); w_json_buffer_free(&buffer); close(fd); return true; }
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!\n"); } return 1; }
int main(int argc, char **argv) { bool ran; json_t *cmd; parse_cmdline(&argc, &argv); if (foreground) { unlink(sock_name); run_service(); return 0; } cmd = build_command(argc, argv); ran = try_command(cmd, 0); if (!ran && should_start(errno)) { if (no_spawn) { ran = try_client_mode_command(cmd, !no_pretty); } else { unlink(sock_name); #ifdef USE_GIMLI spawn_via_gimli(); #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!\n"); } return 1; }
int main(int argc, char **argv) { bool ran; json_t *cmd; w_client_lock_init(); parse_cmdline(&argc, &argv); if (foreground) { run_service(); return 0; } w_set_thread_name("cli"); 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 { spawn_watchman(); 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! (%s)\n", sock_name, strerror(errno)); #ifdef __APPLE__ if (getenv("TMUX")) { w_log(W_LOG_ERR, "\n" "You may be hitting a tmux related session issue.\n" "An immediate workaround is to run:\n" "\n" " watchman version\n" "\n" "just once, from *outside* your tmux session, to allow the launchd\n" "registration to be setup. Once done, you can continue to access\n" "watchman from inside your tmux sessions as usual.\n" "\n" "Longer term, you may wish to install this tool:\n" "\n" " https://github.com/ChrisJohnsen/tmux-MacOSX-pasteboard\n" "\n" "and configure tmux to use `reattach-to-user-namespace`\n" "when it launches your shell.\n"); } #endif } return 1; }
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; }