diff options
author | Ben Pfaff <blp@ovn.org> | 2017-07-14 14:33:44 -0700 |
---|---|---|
committer | Ben Pfaff <blp@ovn.org> | 2017-07-17 10:05:46 -0700 |
commit | b7636967a8901935a1597e973f658c4856663e55 (patch) | |
tree | f2ef1551db8c16b948aad06c6de17e2afbf3bcf8 | |
parent | 2002a49a46c0a15f0c2202567cefc843f4ed6f9a (diff) | |
download | openvswitch-b7636967a8901935a1597e973f658c4856663e55.tar.gz |
stream: Make [p]stream_init() take ownership of 'name' parameter.
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 <blp@ovn.org>
Tested-by: Numan Siddique <nusiddiq@redhat.com>
Acked-by: Numan Siddique <nusiddiq@redhat.com>
-rw-r--r-- | lib/stream-fd.c | 8 | ||||
-rw-r--r-- | lib/stream-fd.h | 4 | ||||
-rw-r--r-- | lib/stream-provider.h | 4 | ||||
-rw-r--r-- | lib/stream-ssl.c | 9 | ||||
-rw-r--r-- | lib/stream-tcp.c | 11 | ||||
-rw-r--r-- | lib/stream-unix.c | 17 | ||||
-rw-r--r-- | lib/stream-windows.c | 6 | ||||
-rw-r--r-- | lib/stream.c | 11 |
8 files changed, 37 insertions, 33 deletions
diff --git a/lib/stream-fd.c b/lib/stream-fd.c index 31bfc6ed9..95373db11 100644 --- a/lib/stream-fd.c +++ b/lib/stream-fd.c @@ -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; @@ -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) diff --git a/lib/stream-fd.h b/lib/stream-fd.h index 12452dd61..24639900b 100644 --- a/lib/stream-fd.h +++ b/lib/stream-fd.h @@ -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, diff --git a/lib/stream-provider.h b/lib/stream-provider.h index ce88785ca..75f4f059b 100644 --- a/lib/stream-provider.h +++ b/lib/stream-provider.h @@ -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) { @@ -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) diff --git a/lib/stream-ssl.c b/lib/stream-ssl.c index 97d6e7464..6dc6f25fd 100644 --- a/lib/stream-ssl.c +++ b/lib/stream-ssl.c @@ -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; @@ -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; @@ -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; @@ -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); } diff --git a/lib/stream-tcp.c b/lib/stream-tcp.c index 1749fadbf..da88cbafa 100644 --- a/lib/stream-tcp.c +++ b/lib/stream-tcp.c @@ -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); @@ -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; @@ -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)); } @@ -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 = { diff --git a/lib/stream-unix.c b/lib/stream-unix.c index 6424d3e4a..40ded1952 100644 --- a/lib/stream-unix.c +++ b/lib/stream-unix.c @@ -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); } @@ -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 @@ -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 = { diff --git a/lib/stream-windows.c b/lib/stream-windows.c index c5a26c814..b4b2aa7f3 100644 --- a/lib/stream-windows.c +++ b/lib/stream-windows.c @@ -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. */ @@ -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; @@ -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)); diff --git a/lib/stream.c b/lib/stream.c index 6b57c7c1c..ec33a690e 100644 --- a/lib/stream.c +++ b/lib/stream.c @@ -634,10 +634,10 @@ 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; @@ -645,17 +645,18 @@ stream_init(struct stream *stream, const struct stream_class *class, : !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 |