From: Ben Pfaff Date: Thu, 3 Dec 2009 22:20:33 +0000 (-0800) Subject: ovsdb-server: Open --listen sockets before detaching. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6dea5eaf5c9fb6e9eaa1610e8a9c44a587bafb25;p=openvswitch ovsdb-server: Open --listen sockets before detaching. This prevents a race condition in a command sequence that starts ovsdb-server and then immediately connects to it with ovsdb-client. --- diff --git a/ovsdb/jsonrpc-server.c b/ovsdb/jsonrpc-server.c index 897f9ae5..1c08a10f 100644 --- a/ovsdb/jsonrpc-server.c +++ b/ovsdb/jsonrpc-server.c @@ -89,23 +89,15 @@ ovsdb_jsonrpc_server_create(struct ovsdb *db) return server; } -int -ovsdb_jsonrpc_server_listen(struct ovsdb_jsonrpc_server *svr, const char *name) +void +ovsdb_jsonrpc_server_listen(struct ovsdb_jsonrpc_server *svr, + struct pstream *pstream) { - struct pstream *pstream; - int error; - - error = pstream_open(name, &pstream); - if (error) { - return error; - } - if (svr->n_listeners >= svr->allocated_listeners) { svr->listeners = x2nrealloc(svr->listeners, &svr->allocated_listeners, sizeof *svr->listeners); } svr->listeners[svr->n_listeners++] = pstream; - return 0; } void diff --git a/ovsdb/jsonrpc-server.h b/ovsdb/jsonrpc-server.h index 0a80ac61..6ff6fe0c 100644 --- a/ovsdb/jsonrpc-server.h +++ b/ovsdb/jsonrpc-server.h @@ -17,11 +17,12 @@ #define OVSDB_JSONRPC_SERVER_H 1 struct ovsdb; +struct pstream; struct ovsdb_jsonrpc_server *ovsdb_jsonrpc_server_create(struct ovsdb *); -int ovsdb_jsonrpc_server_listen(struct ovsdb_jsonrpc_server *, - const char *name); +void ovsdb_jsonrpc_server_listen(struct ovsdb_jsonrpc_server *, + struct pstream *); void ovsdb_jsonrpc_server_connect(struct ovsdb_jsonrpc_server *, const char *name); diff --git a/ovsdb/ovsdb-server.c b/ovsdb/ovsdb-server.c index f6308136..d80161b2 100644 --- a/ovsdb/ovsdb-server.c +++ b/ovsdb/ovsdb-server.c @@ -60,6 +60,7 @@ main(int argc, char *argv[]) struct unixctl_server *unixctl; struct ovsdb_jsonrpc_server *jsonrpc; struct svec active, passive; + struct pstream **listeners; struct ovsdb_error *error; struct ovsdb *db; const char *name; @@ -78,6 +79,18 @@ main(int argc, char *argv[]) parse_options(argc, argv, &file_name, &active, &passive, &unixctl_path); + /* Open all the passive sockets before detaching, to avoid race with + * processes that start up later. */ + listeners = xmalloc(passive.n * sizeof *listeners); + for (i = 0; i < passive.n; i++) { + int error; + + error = pstream_open(passive.names[i], &listeners[i]); + if (error) { + ovs_fatal(error, "failed to listen on \"%s\"", passive.names[i]); + } + } + if (get_detach() && is_chdir_enabled()) { /* We need to skip chdir("/") in daemonize() and do it later, because * we need to open the database and possible set up up Unix domain @@ -101,11 +114,8 @@ main(int argc, char *argv[]) SVEC_FOR_EACH (i, name, &active) { ovsdb_jsonrpc_server_connect(jsonrpc, name); } - SVEC_FOR_EACH (i, name, &passive) { - retval = ovsdb_jsonrpc_server_listen(jsonrpc, name); - if (retval) { - ovs_fatal(retval, "failed to listen on %s", name); - } + for (i = 0; i < passive.n; i++) { + ovsdb_jsonrpc_server_listen(jsonrpc, listeners[i]); } svec_destroy(&active); svec_destroy(&passive);