Commit | Line | Data |
---|---|---|
826d496d MD |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
fac6795d DG |
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. | |
fac6795d DG |
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 | }; | |
42 | ||
43 | /* | |
44 | * lttcom_get_readable_code | |
45 | * | |
46 | * Return ptr to string representing a human readable | |
47 | * error code from the lttcomm_return_code enum. | |
48 | * | |
49 | * These code MUST be negative in other to treat that | |
50 | * as an error value. | |
51 | */ | |
52 | const char *lttcomm_get_readable_code(enum lttcomm_return_code code) | |
53 | { | |
54 | int tmp_code = -code; | |
55 | ||
56 | if (tmp_code >= LTTCOMM_OK && tmp_code < LTTCOMM_NR) { | |
57 | return lttcomm_readable_code[LTTCOMM_ERR_INDEX(tmp_code)]; | |
58 | } | |
59 | ||
60 | return "Unknown error code"; | |
61 | } | |
62 | ||
63 | /* | |
64 | * lttcomm_connect_unix_sock | |
65 | * | |
66 | * Connect to unix socket using the path name. | |
67 | */ | |
68 | int lttcomm_connect_unix_sock(const char *pathname) | |
69 | { | |
686204ab MD |
70 | struct sockaddr_un sun; |
71 | int fd; | |
fac6795d DG |
72 | int ret = 1; |
73 | ||
686204ab | 74 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
fac6795d DG |
75 | if (fd < 0) { |
76 | perror("socket"); | |
77 | goto error; | |
78 | } | |
79 | ||
686204ab MD |
80 | memset(&sun, 0, sizeof(sun)); |
81 | sun.sun_family = AF_UNIX; | |
82 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); | |
fac6795d | 83 | |
686204ab MD |
84 | ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun)); |
85 | if (ret < 0) { | |
86 | perror("connect"); | |
fac6795d | 87 | goto error; |
686204ab | 88 | } |
fac6795d | 89 | |
686204ab | 90 | return fd; |
fac6795d DG |
91 | |
92 | error: | |
93 | return -1; | |
94 | } | |
95 | ||
96 | /* | |
97 | * lttcomm_accept_unix_sock | |
98 | * | |
99 | * Do an accept(2) on the sock and return the | |
100 | * new file descriptor. The socket MUST be bind(2) before. | |
101 | */ | |
102 | int lttcomm_accept_unix_sock(int sock) | |
103 | { | |
104 | int new_fd; | |
105 | struct sockaddr_un sun; | |
106 | socklen_t len = 0; | |
107 | ||
108 | /* Blocking call */ | |
109 | new_fd = accept(sock, (struct sockaddr *) &sun, &len); | |
110 | if (new_fd < 0) { | |
111 | perror("accept"); | |
112 | goto error; | |
113 | } | |
114 | ||
115 | return new_fd; | |
116 | ||
117 | error: | |
118 | return -1; | |
119 | } | |
120 | ||
121 | /* | |
122 | * lttcomm_create_unix_sock | |
123 | * | |
124 | * Creates a AF_UNIX local socket using pathname | |
125 | * bind the socket upon creation and return the fd. | |
126 | */ | |
127 | int lttcomm_create_unix_sock(const char *pathname) | |
128 | { | |
129 | struct sockaddr_un sun; | |
130 | int fd; | |
131 | int ret = -1; | |
132 | ||
133 | /* Create server socket */ | |
134 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { | |
135 | perror("socket"); | |
136 | goto error; | |
137 | } | |
138 | ||
139 | memset(&sun, 0, sizeof(sun)); | |
140 | sun.sun_family = AF_UNIX; | |
141 | strncpy(sun.sun_path, pathname, strlen(pathname)); | |
142 | ||
143 | ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun)); | |
144 | if (ret < 0) { | |
145 | perror("bind"); | |
146 | goto error; | |
147 | } | |
148 | ||
149 | return fd; | |
150 | ||
151 | error: | |
152 | return ret; | |
153 | } | |
154 | ||
155 | /* | |
156 | * lttcomm_listen_unix_sock | |
157 | * | |
158 | * Make the socket listen using MAX_LISTEN. | |
159 | */ | |
160 | int lttcomm_listen_unix_sock(int sock) | |
161 | { | |
162 | int ret; | |
163 | ||
164 | ret = listen(sock, MAX_LISTEN); | |
165 | if (ret < 0) { | |
166 | perror("listen"); | |
167 | } | |
168 | ||
169 | return ret; | |
170 | } | |
171 | ||
172 | /* | |
173 | * lttcomm_recv_unix_sock | |
174 | * | |
175 | * Receive data of size len in put that data into | |
176 | * the buf param. Using recvmsg API. | |
177 | * Return the size of received data. | |
178 | */ | |
179 | ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len) | |
180 | { | |
181 | struct msghdr msg; | |
182 | struct iovec iov[1]; | |
183 | ssize_t ret = -1; | |
184 | ||
185 | memset(&msg, 0, sizeof(msg)); | |
186 | ||
187 | iov[0].iov_base = buf; | |
188 | iov[0].iov_len = len; | |
189 | msg.msg_iov = iov; | |
190 | msg.msg_iovlen = 1; | |
191 | ||
192 | ret = recvmsg(sock, &msg, 0); | |
193 | if (ret < 0) { | |
194 | perror("recvmsg"); | |
195 | } | |
196 | ||
197 | return ret; | |
198 | } | |
199 | ||
200 | /* | |
201 | * lttcomm_send_unix_sock | |
202 | * | |
203 | * Send buf data of size len. Using sendmsg API. | |
204 | * Return the size of sent data. | |
205 | */ | |
206 | ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len) | |
207 | { | |
208 | struct msghdr msg; | |
209 | struct iovec iov[1]; | |
210 | ssize_t ret = -1; | |
211 | ||
212 | memset(&msg, 0, sizeof(msg)); | |
213 | ||
214 | iov[0].iov_base = buf; | |
215 | iov[0].iov_len = len; | |
216 | msg.msg_iov = iov; | |
217 | msg.msg_iovlen = 1; | |
218 | ||
219 | ret = sendmsg(sock, &msg, 0); | |
220 | if (ret < 0) { | |
221 | perror("sendmsg"); | |
222 | } | |
223 | ||
224 | return ret; | |
225 | } |