summaryrefslogtreecommitdiff
path: root/src/or/control.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2011-05-09 12:13:37 -0400
committerNick Mathewson <nickm@torproject.org>2011-05-13 10:41:28 -0400
commitdad12188a6ca957f6fde1eb602fd98b2fa93b1a4 (patch)
treeac7c2d4d5e1ff39675df1bc3723b57ec453b4b92 /src/or/control.c
parentc55c8f0d4926481ea2cf49e14915688de2eb4938 (diff)
downloadtor-dad12188a6ca957f6fde1eb602fd98b2fa93b1a4.tar.gz
tor-dad12188a6ca957f6fde1eb602fd98b2fa93b1a4.zip
Write automatically-chosen control ports to a file.
Diffstat (limited to 'src/or/control.c')
-rw-r--r--src/or/control.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/or/control.c b/src/or/control.c
index 19e8539a20..634674233c 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -508,6 +508,45 @@ connection_printf_to_buf(control_connection_t *conn, const char *format, ...)
connection_write_to_buf(buf, len, TO_CONN(conn));
}
+/** Write all of the open control ports to ControlPortWriteToFile */
+void
+control_ports_write_to_file(void)
+{
+ smartlist_t *lines;
+ char *joined = NULL;
+ or_options_t *options = get_options();
+
+ if (!options->ControlPortWriteToFile)
+ return;
+
+ lines = smartlist_create();
+
+ SMARTLIST_FOREACH_BEGIN(get_connection_array(), const connection_t *, conn) {
+ char *port_str = NULL;
+ if (conn->type != CONN_TYPE_CONTROL_LISTENER || conn->marked_for_close)
+ continue;
+#ifdef AF_UNIX
+ if (conn->socket_family == AF_UNIX) {
+ tor_asprintf(&port_str, "UNIX_PORT=%s\n", conn->address);
+ smartlist_add(lines, port_str);
+ continue;
+ }
+#endif
+ tor_asprintf(&port_str, "PORT=%s:%d\n", conn->address, conn->port);
+ smartlist_add(lines, port_str);
+ } SMARTLIST_FOREACH_END(conn);
+
+ joined = smartlist_join_strings(lines, "", 0, NULL);
+
+ if (write_str_to_file(options->ControlPortWriteToFile, joined, 0) < 0) {
+ log_warn(LD_CONTROL, "Writing %s failed: %s",
+ options->ControlPortWriteToFile, strerror(errno));
+ }
+ tor_free(joined);
+ SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
+ smartlist_free(lines);
+}
+
/** Send a "DONE" message down the control connection <b>conn</b>. */
static void
send_control_done(control_connection_t *conn)