adae8410401ba41c19ef4176e8b6aea45cc8a443
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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.
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.
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.
24 #include <sys/socket.h>
26 #include <sys/types.h>
30 #include "liblttsessiondcomm.h"
33 * Human readable error message.
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",
46 * lttcom_get_readable_code
48 * Return ptr to string representing a human readable
49 * error code from the lttcomm_return_code enum.
51 * These code MUST be negative in other to treat that
54 const char *lttcomm_get_readable_code(enum lttcomm_return_code code
)
58 if (tmp_code
>= LTTCOMM_OK
&& tmp_code
< LTTCOMM_NR
) {
59 return lttcomm_readable_code
[LTTCOMM_ERR_INDEX(tmp_code
)];
62 return "Unknown error code";
66 * lttcomm_connect_unix_sock
68 * Connect to unix socket using the path name.
70 int lttcomm_connect_unix_sock(const char *pathname
)
72 struct sockaddr_un sun
;
76 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
82 memset(&sun
, 0, sizeof(sun
));
83 sun
.sun_family
= AF_UNIX
;
84 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
86 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
99 * lttcomm_accept_unix_sock
101 * Do an accept(2) on the sock and return the
102 * new file descriptor. The socket MUST be bind(2) before.
104 int lttcomm_accept_unix_sock(int sock
)
107 struct sockaddr_un sun
;
111 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
124 * lttcomm_create_unix_sock
126 * Creates a AF_UNIX local socket using pathname
127 * bind the socket upon creation and return the fd.
129 int lttcomm_create_unix_sock(const char *pathname
)
131 struct sockaddr_un sun
;
135 /* Create server socket */
136 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
141 memset(&sun
, 0, sizeof(sun
));
142 sun
.sun_family
= AF_UNIX
;
143 strncpy(sun
.sun_path
, pathname
, strlen(pathname
));
145 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
158 * lttcomm_listen_unix_sock
160 * Make the socket listen using MAX_LISTEN.
162 int lttcomm_listen_unix_sock(int sock
)
166 ret
= listen(sock
, MAX_LISTEN
);
175 * lttcomm_recv_unix_sock
177 * Receive data of size len in put that data into
178 * the buf param. Using recvmsg API.
179 * Return the size of received data.
181 ssize_t
lttcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
187 memset(&msg
, 0, sizeof(msg
));
189 iov
[0].iov_base
= buf
;
190 iov
[0].iov_len
= len
;
194 ret
= recvmsg(sock
, &msg
, 0);
203 * lttcomm_send_unix_sock
205 * Send buf data of size len. Using sendmsg API.
206 * Return the size of sent data.
208 ssize_t
lttcomm_send_unix_sock(int sock
, void *buf
, size_t len
)
214 memset(&msg
, 0, sizeof(msg
));
216 iov
[0].iov_base
= buf
;
217 iov
[0].iov_len
= len
;
221 ret
= sendmsg(sock
, &msg
, 0);
This page took 0.034238 seconds and 3 git commands to generate.