Skip to content

Commit

Permalink
stream: Make [p]stream_init() take ownership of 'name' parameter.
Browse files Browse the repository at this point in the history
This will be a more sensible interface in an upcoming commit where many of
the callers are assembling dynamic name strings anyway.

Signed-off-by: Ben Pfaff <[email protected]>
Tested-by: Numan Siddique <[email protected]>
Acked-by: Numan Siddique <[email protected]>
  • Loading branch information
blp committed Jul 17, 2017
1 parent 2002a49 commit b763696
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 33 deletions.
8 changes: 6 additions & 2 deletions lib/stream-fd.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ static void maybe_unlink_and_free(char *path);
* 'connect_status' is interpreted as described for stream_init(). 'fd_type'
* tells whether the socket is TCP or Unix domain socket.
*
* Takes ownership of 'name'.
*
* Returns 0 if successful, otherwise a positive errno value. (The current
* implementation never fails.) */
int
new_fd_stream(const char *name, int fd, int connect_status, int fd_type,
new_fd_stream(char *name, int fd, int connect_status, int fd_type,
struct stream **streamp)
{
struct stream_fd *s;
Expand Down Expand Up @@ -205,10 +207,12 @@ fd_pstream_cast(struct pstream *pstream)
* When '*pstreamp' is closed, then 'unlink_path' (if nonnull) will be passed
* to fatal_signal_unlink_file_now() and freed with free().
*
* Takes ownership of 'name'.
*
* Returns 0 if successful, otherwise a positive errno value. (The current
* implementation never fails.) */
int
new_fd_pstream(const char *name, int fd,
new_fd_pstream(char *name, int fd,
int (*accept_cb)(int fd, const struct sockaddr_storage *ss,
size_t ss_len, struct stream **streamp),
char *unlink_path, struct pstream **pstreamp)
Expand Down
4 changes: 2 additions & 2 deletions lib/stream-fd.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ struct stream;
struct pstream;
struct sockaddr_storage;

int new_fd_stream(const char *name, int fd, int connect_status,
int new_fd_stream(char *name, int fd, int connect_status,
int fd_type, struct stream **streamp);
int new_fd_pstream(const char *name, int fd,
int new_fd_pstream(char *name, int fd,
int (*accept_cb)(int fd, const struct sockaddr_storage *ss,
size_t ss_len, struct stream **),
char *unlink_path,
Expand Down
4 changes: 2 additions & 2 deletions lib/stream-provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct stream {
};

void stream_init(struct stream *, const struct stream_class *,
int connect_status, const char *name);
int connect_status, char *name);
static inline void stream_assert_class(const struct stream *stream,
const struct stream_class *class)
{
Expand Down Expand Up @@ -135,7 +135,7 @@ struct pstream {
ovs_be16 bound_port;
};

void pstream_init(struct pstream *, const struct pstream_class *, const char *name);
void pstream_init(struct pstream *, const struct pstream_class *, char *name);
void pstream_set_bound_port(struct pstream *, ovs_be16 bound_port);
static inline void pstream_assert_class(const struct pstream *pstream,
const struct pstream_class *class)
Expand Down
9 changes: 5 additions & 4 deletions lib/stream-ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,9 @@ want_to_poll_events(int want)
}
}

/* Takes ownership of 'name'. */
static int
new_ssl_stream(const char *name, int fd, enum session_type type,
new_ssl_stream(char *name, int fd, enum session_type type,
enum ssl_state state, struct stream **streamp)
{
struct ssl_stream *sslv;
Expand Down Expand Up @@ -323,7 +324,7 @@ ssl_open(const char *name, char *suffix, struct stream **streamp, uint8_t dscp)
dscp);
if (fd >= 0) {
int state = error ? STATE_TCP_CONNECTING : STATE_SSL_CONNECTING;
return new_ssl_stream(name, fd, CLIENT, state, streamp);
return new_ssl_stream(xstrdup(name), fd, CLIENT, state, streamp);
} else {
VLOG_ERR("%s: connect: %s", name, ovs_strerror(error));
return error;
Expand Down Expand Up @@ -847,7 +848,7 @@ pssl_open(const char *name OVS_UNUSED, char *suffix, struct pstream **pstreamp,
port, ss_format_address(&ss, addrbuf, sizeof addrbuf));

pssl = xmalloc(sizeof *pssl);
pstream_init(&pssl->pstream, &pssl_pstream_class, bound_name);
pstream_init(&pssl->pstream, &pssl_pstream_class, xstrdup(bound_name));
pstream_set_bound_port(&pssl->pstream, htons(port));
pssl->fd = fd;
*pstreamp = &pssl->pstream;
Expand Down Expand Up @@ -896,7 +897,7 @@ pssl_accept(struct pstream *pstream, struct stream **new_streamp)
snprintf(name, sizeof name, "ssl:%s:%"PRIu16,
ss_format_address(&ss, addrbuf, sizeof addrbuf),
ss_get_port(&ss));
return new_ssl_stream(name, new_fd, SERVER, STATE_SSL_CONNECTING,
return new_ssl_stream(xstrdup(name), new_fd, SERVER, STATE_SSL_CONNECTING,
new_streamp);
}

Expand Down
11 changes: 6 additions & 5 deletions lib/stream-tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ VLOG_DEFINE_THIS_MODULE(stream_tcp);

/* Active TCP. */

/* Takes ownership of 'name'. */
static int
new_tcp_stream(const char *name, int fd, int connect_status,
struct stream **streamp)
new_tcp_stream(char *name, int fd, int connect_status, struct stream **streamp)
{
if (connect_status == 0) {
setsockopt_tcp_nodelay(fd);
Expand All @@ -55,7 +55,7 @@ tcp_open(const char *name, char *suffix, struct stream **streamp, uint8_t dscp)

error = inet_open_active(SOCK_STREAM, suffix, 0, NULL, &fd, dscp);
if (fd >= 0) {
return new_tcp_stream(name, fd, error, streamp);
return new_tcp_stream(xstrdup(name), fd, error, streamp);
} else {
VLOG_ERR("%s: connect: %s", name, ovs_strerror(error));
return error;
Expand Down Expand Up @@ -105,7 +105,8 @@ new_pstream(char *suffix, const char *name, struct pstream **pstreamp,
conn_name = bound_name;
}

error = new_fd_pstream(conn_name, fd, ptcp_accept, unlink_path, pstreamp);
error = new_fd_pstream(xstrdup(conn_name), fd,
ptcp_accept, unlink_path, pstreamp);
if (!error) {
pstream_set_bound_port(*pstreamp, htons(port));
}
Expand All @@ -129,7 +130,7 @@ ptcp_accept(int fd, const struct sockaddr_storage *ss,
snprintf(name, sizeof name, "tcp:%s:%"PRIu16,
ss_format_address(ss, addrbuf, sizeof addrbuf),
ss_get_port(ss));
return new_tcp_stream(name, fd, 0, streamp);
return new_tcp_stream(xstrdup(name), fd, 0, streamp);
}

const struct pstream_class ptcp_pstream_class = {
Expand Down
17 changes: 7 additions & 10 deletions lib/stream-unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ unix_open(const char *name, char *suffix, struct stream **streamp,
}

free(connect_path);
return new_fd_stream(name, fd, check_connection_completion(fd),
return new_fd_stream(xstrdup(name), fd, check_connection_completion(fd),
AF_UNIX, streamp);
}

Expand Down Expand Up @@ -102,7 +102,8 @@ punix_open(const char *name OVS_UNUSED, char *suffix,
return error;
}

return new_fd_pstream(name, fd, punix_accept, bind_path, pstreamp);
return new_fd_pstream(xstrdup(name), fd,
punix_accept, bind_path, pstreamp);
}

static int
Expand All @@ -111,14 +112,10 @@ punix_accept(int fd, const struct sockaddr_storage *ss, size_t ss_len,
{
const struct sockaddr_un *sun = (const struct sockaddr_un *) ss;
int name_len = get_unix_name_len(sun, ss_len);
char name[128];

if (name_len > 0) {
snprintf(name, sizeof name, "unix:%.*s", name_len, sun->sun_path);
} else {
strcpy(name, "unix");
}
return new_fd_stream(name, fd, 0, AF_UNIX, streamp);
char *bound_name = (name_len > 0
? xasprintf("unix:%.*s", name_len, sun->sun_path)
: xstrdup("unix"));
return new_fd_stream(bound_name, fd, 0, AF_UNIX, streamp);
}

const struct pstream_class punix_pstream_class = {
Expand Down
6 changes: 3 additions & 3 deletions lib/stream-windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ windows_open(const char *name, char *suffix, struct stream **streamp,
return ENOENT;
}
struct windows_stream *s = xmalloc(sizeof *s);
stream_init(&s->stream, &windows_stream_class, 0, name);
stream_init(&s->stream, &windows_stream_class, 0, xstrdup(name));
s->pipe_path = connect_path;
s->fd = npipe;
/* This is an active stream. */
Expand Down Expand Up @@ -555,7 +555,7 @@ pwindows_accept(struct pstream *pstream, struct stream **new_streamp)
/* Give the handle p->fd to the new created active stream and specify it
* was created by an active stream. */
struct windows_stream *p_temp = xmalloc(sizeof *p_temp);
stream_init(&p_temp->stream, &windows_stream_class, 0, "unix");
stream_init(&p_temp->stream, &windows_stream_class, 0, xstrdup("unix"));
p_temp->fd = p->fd;
/* Specify it was created by a passive stream. */
p_temp->server = true;
Expand Down Expand Up @@ -642,7 +642,7 @@ pwindows_open(const char *name OVS_UNUSED, char *suffix,
}

struct pwindows_pstream *p = xmalloc(sizeof *p);
pstream_init(&p->pstream, &pwindows_pstream_class, name);
pstream_init(&p->pstream, &pwindows_pstream_class, xstrdup(name));
p->fd = npipe;
p->unlink_path = orig_path;
memset(&p->connect, 0, sizeof(p->connect));
Expand Down
11 changes: 6 additions & 5 deletions lib/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -634,28 +634,29 @@ pstream_get_bound_port(const struct pstream *pstream)
* After calling this function, stream_close() must be used to destroy
* 'stream', otherwise resources will be leaked.
*
* The caller retains ownership of 'name'. */
* Takes ownership of 'name'. */
void
stream_init(struct stream *stream, const struct stream_class *class,
int connect_status, const char *name)
int connect_status, char *name)
{
memset(stream, 0, sizeof *stream);
stream->class = class;
stream->state = (connect_status == EAGAIN ? SCS_CONNECTING
: !connect_status ? SCS_CONNECTED
: SCS_DISCONNECTED);
stream->error = connect_status;
stream->name = xstrdup(name);
stream->name = name;
ovs_assert(stream->state != SCS_CONNECTING || class->connect);
}

/* Takes ownership of 'name'. */
void
pstream_init(struct pstream *pstream, const struct pstream_class *class,
const char *name)
char *name)
{
memset(pstream, 0, sizeof *pstream);
pstream->class = class;
pstream->name = xstrdup(name);
pstream->name = name;
}

void
Expand Down

0 comments on commit b763696

Please sign in to comment.