-static int control_compare(const struct event *a, const struct event *b) {
- return a < b ? -1 : a > b ? 1 : 0;
-}
-
-bool init_control() {
- int result;
- struct sockaddr_un addr;
- char *lastslash;
-
- if(strlen(controlsocketname) >= sizeof addr.sun_path) {
- logger(LOG_ERR, "Control socket filename too long!");
- goto bail;
- }
-
- memset(&addr, 0, sizeof addr);
- addr.sun_family = AF_UNIX;
- strncpy(addr.sun_path, controlsocketname, sizeof addr.sun_path - 1);
-
- control_socket = socket(PF_UNIX, SOCK_STREAM, 0);
-
- if(control_socket < 0) {
- logger(LOG_ERR, "Creating UNIX socket failed: %s", strerror(errno));
- goto bail;
- }
-
- /*
- * Restrict connections to our control socket by ensuring the parent
- * directory can be traversed only by root. Note this is not totally
- * race-free unless all ancestors are writable only by trusted users,
- * which we don't verify.
- */
-
- struct stat statbuf;
- lastslash = strrchr(controlsocketname, '/');
- if(lastslash != NULL) {
- *lastslash = 0; /* temporarily change controlsocketname to be dir */
- if(mkdir(controlsocketname, 0700) < 0 && errno != EEXIST) {
- logger(LOG_ERR, "Unable to create control socket directory %s: %s", controlsocketname, strerror(errno));
- *lastslash = '/';
- goto bail;
+bool init_control(void) {
+ randomize(controlcookie, sizeof controlcookie / 2);
+ bin2hex(controlcookie, controlcookie, sizeof controlcookie / 2);
+
+ FILE *f = fopen(pidfilename, "w");
+ if(!f) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Cannot write control socket cookie file %s: %s", pidfilename, strerror(errno));
+ return false;
+ }
+
+#ifdef HAVE_FCHMOD
+ fchmod(fileno(f), 0600);
+#else
+ chmod(pidfilename, 0600);
+#endif
+ // Get the address and port of the first listening socket
+
+ char *localhost = NULL;
+ sockaddr_t sa;
+ socklen_t len = sizeof sa;
+
+ // Make sure we have a valid address, and map 0.0.0.0 and :: to 127.0.0.1 and ::1.
+
+ if(getsockname(listen_socket[0].tcp, (struct sockaddr *)&sa, &len)) {
+ xasprintf(&localhost, "127.0.0.1 port %d", myport);
+ } else {
+ if(sa.sa.sa_family == AF_INET) {
+ if(sa.in.sin_addr.s_addr == 0)
+ sa.in.sin_addr.s_addr = htonl(0x7f000001);
+ } else if(sa.sa.sa_family == AF_INET6) {
+ static const uint8_t zero[16] = {0};
+ if(!memcmp(sa.in6.sin6_addr.s6_addr, zero, sizeof zero))
+ sa.in6.sin6_addr.s6_addr[15] = 1;