Add close unix socket function to libcomm API
[lttng-tools.git] / liblttsessiondcomm / liblttsessiondcomm.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/socket.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <sys/un.h>
28 #include <unistd.h>
29
30 #include "liblttsessiondcomm.h"
31
32 /*
33 * Human readable error message.
34 */
35 static const char *lttcomm_readable_code[] = {
36 [ LTTCOMM_ERR_INDEX(LTTCOMM_OK) ] = "Success",
37 [ LTTCOMM_ERR_INDEX(LTTCOMM_ERR) ] = "Unknown error",
38 [ LTTCOMM_ERR_INDEX(LTTCOMM_UND) ] = "Undefined command",
39 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_SESSION) ] = "No session found",
40 [ LTTCOMM_ERR_INDEX(LTTCOMM_LIST_FAIL) ] = "Unable to list traceable apps",
41 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_APPS) ] = "No traceable apps found",
42 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_SESS) ] = "No session found",
43 [ LTTCOMM_ERR_INDEX(LTTCOMM_FATAL) ] = "Fatal error of the session daemon",
44 };
45
46 /*
47 * lttcom_get_readable_code
48 *
49 * Return ptr to string representing a human readable
50 * error code from the lttcomm_return_code enum.
51 *
52 * These code MUST be negative in other to treat that
53 * as an error value.
54 */
55 const char *lttcomm_get_readable_code(enum lttcomm_return_code code)
56 {
57 int tmp_code = -code;
58
59 if (tmp_code >= LTTCOMM_OK && tmp_code < LTTCOMM_NR) {
60 return lttcomm_readable_code[LTTCOMM_ERR_INDEX(tmp_code)];
61 }
62
63 return "Unknown error code";
64 }
65
66 /*
67 * lttcomm_connect_unix_sock
68 *
69 * Connect to unix socket using the path name.
70 */
71 int lttcomm_connect_unix_sock(const char *pathname)
72 {
73 struct sockaddr_un sun;
74 int fd;
75 int ret = 1;
76
77 fd = socket(PF_UNIX, SOCK_STREAM, 0);
78 if (fd < 0) {
79 perror("socket");
80 goto error;
81 }
82
83 memset(&sun, 0, sizeof(sun));
84 sun.sun_family = AF_UNIX;
85 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
86
87 ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun));
88 if (ret < 0) {
89 perror("connect");
90 goto error;
91 }
92
93 return fd;
94
95 error:
96 return -1;
97 }
98
99 /*
100 * lttcomm_accept_unix_sock
101 *
102 * Do an accept(2) on the sock and return the
103 * new file descriptor. The socket MUST be bind(2) before.
104 */
105 int lttcomm_accept_unix_sock(int sock)
106 {
107 int new_fd;
108 struct sockaddr_un sun;
109 socklen_t len = 0;
110
111 /* Blocking call */
112 new_fd = accept(sock, (struct sockaddr *) &sun, &len);
113 if (new_fd < 0) {
114 perror("accept");
115 goto error;
116 }
117
118 return new_fd;
119
120 error:
121 return -1;
122 }
123
124 /*
125 * lttcomm_create_unix_sock
126 *
127 * Creates a AF_UNIX local socket using pathname
128 * bind the socket upon creation and return the fd.
129 */
130 int lttcomm_create_unix_sock(const char *pathname)
131 {
132 struct sockaddr_un sun;
133 int fd;
134 int ret = -1;
135
136 /* Create server socket */
137 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
138 perror("socket");
139 goto error;
140 }
141
142 memset(&sun, 0, sizeof(sun));
143 sun.sun_family = AF_UNIX;
144 strncpy(sun.sun_path, pathname, strlen(pathname));
145
146 ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun));
147 if (ret < 0) {
148 perror("bind");
149 goto error;
150 }
151
152 return fd;
153
154 error:
155 return ret;
156 }
157
158 /*
159 * lttcomm_listen_unix_sock
160 *
161 * Make the socket listen using MAX_LISTEN.
162 */
163 int lttcomm_listen_unix_sock(int sock)
164 {
165 int ret;
166
167 ret = listen(sock, MAX_LISTEN);
168 if (ret < 0) {
169 perror("listen");
170 }
171
172 return ret;
173 }
174
175 /*
176 * lttcomm_recv_unix_sock
177 *
178 * Receive data of size len in put that data into
179 * the buf param. Using recvmsg API.
180 * Return the size of received data.
181 */
182 ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len)
183 {
184 struct msghdr msg;
185 struct iovec iov[1];
186 ssize_t ret = -1;
187
188 memset(&msg, 0, sizeof(msg));
189
190 iov[0].iov_base = buf;
191 iov[0].iov_len = len;
192 msg.msg_iov = iov;
193 msg.msg_iovlen = 1;
194
195 ret = recvmsg(sock, &msg, 0);
196 if (ret < 0) {
197 perror("recvmsg");
198 }
199
200 return ret;
201 }
202
203 /*
204 * lttcomm_send_unix_sock
205 *
206 * Send buf data of size len. Using sendmsg API.
207 * Return the size of sent data.
208 */
209 ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len)
210 {
211 struct msghdr msg;
212 struct iovec iov[1];
213 ssize_t ret = -1;
214
215 memset(&msg, 0, sizeof(msg));
216
217 iov[0].iov_base = buf;
218 iov[0].iov_len = len;
219 msg.msg_iov = iov;
220 msg.msg_iovlen = 1;
221
222 ret = sendmsg(sock, &msg, 0);
223 if (ret < 0) {
224 perror("sendmsg");
225 }
226
227 return ret;
228 }
229
230 /*
231 * lttcomm_close_unix_sock
232 *
233 * Shutdown cleanly a unix socket.
234 */
235 int lttcomm_close_unix_sock(int sock)
236 {
237 int ret;
238
239 /* Shutdown receptions and transmissions */
240 ret = shutdown(sock, SHUT_RDWR);
241 if (ret < 0) {
242 perror("shutdown");
243 }
244
245 return ret;
246 }
This page took 0.034774 seconds and 5 git commands to generate.