X-Git-Url: http://git.lttng.org/?p=lttng-ust.git;a=blobdiff_plain;f=liblttng-ust-comm%2Flttng-ust-comm.c;fp=liblttng-ust-comm%2Flttng-ust-comm.c;h=3138016569e8d0c11d2036e2c2fb97225efc399a;hp=078b56a01bb68c49c2322fc2a8f454d597305314;hb=bf5ff35ed0a3a7f60e92d654a5b97e73b94da852;hpb=cbd7f39d9267e24159023db91712ca91558c5cc8 diff --git a/liblttng-ust-comm/lttng-ust-comm.c b/liblttng-ust-comm/lttng-ust-comm.c index 078b56a0..31380165 100644 --- a/liblttng-ust-comm/lttng-ust-comm.c +++ b/liblttng-ust-comm/lttng-ust-comm.c @@ -509,3 +509,85 @@ int ustcomm_recv_fd(int sock) end: return ret; } + +ssize_t ustcomm_send_string(int sock, char *str, size_t len) +{ + ssize_t slen, ret = -1; + + if (!str) { + goto end; + } + + /* Send string len first */ + slen = ustcomm_send_unix_sock(sock, &len, sizeof(len)); + + if (slen != sizeof(len)) { + fprintf(stderr, + "Unexpected sent size. Expected %zu got %zu\n", + sizeof(len), slen); + ret = -1; + goto end; + } + + /* Send the actual string */ + slen = ustcomm_send_unix_sock(sock, str, len); + if (slen != len) { + fprintf(stderr, + "Unexpected sent size. Expected %zu got %zu\n", + len, slen); + ret = -1; + goto end; + } + + ret = slen; + +end: + return ret; +} + +/* + * Allocate and return the received string. + * Return NULL on error. + * Caller is responsible of freeing the allocated string. + */ +char *ustcomm_recv_string(int sock) +{ + ssize_t rlen; + size_t len; + char *ret; + + /* Get the string len first */ + rlen = ustcomm_recv_unix_sock(sock, &len, sizeof(len)); + + if (rlen != sizeof(len)) { + fprintf(stderr, + "Unexpected received size. Expected %zu got %zu\n", + sizeof(len), rlen); + ret = NULL; + goto end; + } + + /* Account for the NULL byte */ + ret = malloc(len + 1); + if (!ret) { + ret = NULL; + goto end; + } + + /* Get the actual string */ + rlen = ustcomm_recv_unix_sock(sock, ret, len); + if (rlen != len) { + fprintf(stderr, + "Unexpected received size. Expected %zu got %zu\n", + len, rlen); + free(ret); + ret = NULL; + goto end; + } + + /* Set terminating NULL byte */ + ret[len] = '\0'; + +end: + return ret; +}