File: checksock.cpp

package info (click to toggle)
watchman 4.9.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,992 kB
  • sloc: cpp: 27,459; python: 6,538; java: 3,404; php: 3,257; ansic: 2,803; javascript: 1,116; makefile: 671; ruby: 364; sh: 124; xml: 102; lisp: 4
file content (66 lines) | stat: -rw-r--r-- 1,837 bytes parent folder | download | duplicates (3)
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
/* Copyright 2015-present Facebook, Inc.
 * Licensed under the Apache License, Version 2.0 */

#include "watchman.h"

/* Periodically connect to our endpoint and verify that we're talking
 * to ourselves.  This is normally a sign of madness, but if we don't
 * get an answer, or get a reply from someone else, we know things
 * are bad; someone removed our socket file or there was some kind of
 * race condition that resulted in multiple instances starting up.
 */

static void check_my_sock() {
  auto cmd = json_array({typed_string_to_json("get-pid", W_STRING_UNICODE)});
  w_jbuffer_t buf;
  json_error_t jerr;
  json_int_t remote_pid = 0;
  pid_t my_pid = getpid();

  w_set_thread_name("sockcheck");

  auto client = w_stm_connect(get_sock_name(), 6000);
  if (!client) {
    w_log(W_LOG_FATAL, "Failed to connect to myself for get-pid check: %s\n",
        strerror(errno));
    /* NOTREACHED */
  }

  client->setNonBlock(false);

  if (!buf.pduEncodeToStream(is_bser, 0, cmd, client.get())) {
    w_log(W_LOG_FATAL, "Failed to send get-pid PDU: %s\n",
          strerror(errno));
    /* NOTREACHED */
  }

  buf.clear();
  auto result = buf.decodeNext(client.get(), &jerr);
  if (!result) {
    w_log(W_LOG_FATAL, "Failed to decode get-pid response: %s %s\n",
        jerr.text, strerror(errno));
    /* NOTREACHED */
  }

  if (json_unpack_ex(result, &jerr, 0, "{s:i}",
        "pid", &remote_pid) != 0) {
    w_log(W_LOG_FATAL, "Failed to extract pid from get-pid response: %s\n",
        jerr.text);
    /* NOTREACHED */
  }

  if (remote_pid != my_pid) {
    w_log(W_LOG_FATAL,
        "remote pid from get-pid (%ld) doesn't match my pid (%ld)\n",
        (long)remote_pid, (long)my_pid);
    /* NOTREACHED */
  }
}

void w_check_my_sock(void) {
  std::thread thr(check_my_sock);
  thr.detach();
}

/* vim:ts=2:sw=2:et:
 */