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