Skip to content

Commit

Permalink
unixctl: Make path to unixctl_server socket available to the client.
Browse files Browse the repository at this point in the history
Acked-by: Alin Gabriel Serdean <[email protected]>
Acked-by: Mark Michelson <[email protected]>
Signed-off-by: Ben Pfaff <[email protected]>
  • Loading branch information
blp committed Aug 7, 2018
1 parent a521491 commit 295fc4d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
52 changes: 28 additions & 24 deletions lib/unixctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct unixctl_conn {
struct unixctl_server {
struct pstream *listener;
struct ovs_list conns;
char *path;
};

static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
Expand Down Expand Up @@ -216,48 +217,44 @@ unixctl_command_reply_error(struct unixctl_conn *conn, const char *error)
int
unixctl_server_create(const char *path, struct unixctl_server **serverp)
{
struct unixctl_server *server;
struct pstream *listener;
char *punix_path;
int error;

*serverp = NULL;
if (path && !strcmp(path, "none")) {
return 0;
}

if (path) {
char *abs_path;
abs_path = abs_file_name(ovs_rundir(), path);
punix_path = xasprintf("punix:%s", abs_path);
free(abs_path);
} else {
#ifndef _WIN32
punix_path = xasprintf("punix:%s/%s.%ld.ctl", ovs_rundir(),
program_name, (long int) getpid());
#ifdef _WIN32
enum { WINDOWS = 1 };
#else
punix_path = xasprintf("punix:%s/%s.ctl", ovs_rundir(), program_name);
enum { WINDOWS = 0 };
#endif
}

error = pstream_open(punix_path, &listener, 0);
long int pid = getpid();
char *abs_path
= (path ? abs_file_name(ovs_rundir(), path)
: WINDOWS ? xasprintf("%s/%s.ctl", ovs_rundir(), program_name)
: xasprintf("%s/%s.%ld.ctl", ovs_rundir(), program_name, pid));

struct pstream *listener;
char *punix_path = xasprintf("punix:%s", abs_path);
int error = pstream_open(punix_path, &listener, 0);
free(punix_path);

if (error) {
ovs_error(error, "could not initialize control socket %s", punix_path);
goto exit;
ovs_error(error, "%s: could not initialize control socket", abs_path);
free(abs_path);
return error;
}

unixctl_command_register("list-commands", "", 0, 0, unixctl_list_commands,
NULL);
unixctl_command_register("version", "", 0, 0, unixctl_version, NULL);

server = xmalloc(sizeof *server);
struct unixctl_server *server = xmalloc(sizeof *server);
server->listener = listener;
server->path = abs_path;
ovs_list_init(&server->conns);
*serverp = server;

exit:
free(punix_path);
return error;
return 0;
}

static void
Expand Down Expand Up @@ -429,10 +426,17 @@ unixctl_server_destroy(struct unixctl_server *server)
kill_connection(conn);
}

free (server->path);
pstream_close(server->listener);
free(server);
}
}

const char *
unixctl_server_get_path(const struct unixctl_server *server)
{
return server ? server->path : NULL;
}

/* On POSIX based systems, connects to a unixctl server socket. 'path' should
* be the name of a unixctl server socket. If it does not start with '/', it
Expand Down
2 changes: 2 additions & 0 deletions lib/unixctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ void unixctl_server_run(struct unixctl_server *);
void unixctl_server_wait(struct unixctl_server *);
void unixctl_server_destroy(struct unixctl_server *);

const char *unixctl_server_get_path(const struct unixctl_server *);

/* Client for Unix domain socket control connection. */
struct jsonrpc;
int unixctl_client_create(const char *path, struct jsonrpc **client);
Expand Down
4 changes: 2 additions & 2 deletions tests/daemon.at
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ AT_SETUP([daemon --detach startup errors])
AT_CAPTURE_FILE([pid])
OVSDB_INIT([db])
AT_CHECK([ovsdb-server --detach --no-chdir --pidfile --unixctl=nonexistent/unixctl db], [1], [], [stderr])
AT_CHECK([grep 'ovsdb-server: could not initialize control socket' stderr],
AT_CHECK([grep 'could not initialize control socket' stderr],
[0], [ignore])
AT_CHECK([test ! -s pid])
AT_CLEANUP
Expand All @@ -159,7 +159,7 @@ AT_SKIP_IF([test "$IS_WIN32" = "yes"])
AT_CAPTURE_FILE([pid])
OVSDB_INIT([db])
AT_CHECK([ovsdb-server --detach --no-chdir --pidfile --monitor --unixctl=nonexistent/unixctl db], [1], [], [stderr])
AT_CHECK([grep 'ovsdb-server: could not initialize control socket' stderr],
AT_CHECK([grep 'could not initialize control socket' stderr],
[0], [ignore])
AT_CHECK([test ! -s pid])
AT_CLEANUP
Expand Down

0 comments on commit 295fc4d

Please sign in to comment.