| 1 | /* |
| 2 | * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License, version 2 only, as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #define _LGPL_SOURCE |
| 19 | #include <assert.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <unistd.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/stat.h> |
| 24 | |
| 25 | #include <common/common.h> |
| 26 | |
| 27 | #include "pipe.h" |
| 28 | |
| 29 | /* |
| 30 | * Lock read side of a pipe. |
| 31 | */ |
| 32 | static void lock_read_side(struct lttng_pipe *pipe) |
| 33 | { |
| 34 | pthread_mutex_lock(&pipe->read_mutex); |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | * Unlock read side of a pipe. |
| 39 | */ |
| 40 | static void unlock_read_side(struct lttng_pipe *pipe) |
| 41 | { |
| 42 | pthread_mutex_unlock(&pipe->read_mutex); |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * Lock write side of a pipe. |
| 47 | */ |
| 48 | static void lock_write_side(struct lttng_pipe *pipe) |
| 49 | { |
| 50 | pthread_mutex_lock(&pipe->write_mutex); |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * Unlock write side of a pipe. |
| 55 | */ |
| 56 | static void unlock_write_side(struct lttng_pipe *pipe) |
| 57 | { |
| 58 | pthread_mutex_unlock(&pipe->write_mutex); |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Internal function. Close read side of pipe WITHOUT locking the mutex. |
| 63 | * |
| 64 | * Return 0 on success else a negative errno from close(2). |
| 65 | */ |
| 66 | static int _pipe_read_close(struct lttng_pipe *pipe) |
| 67 | { |
| 68 | int ret, ret_val = 0; |
| 69 | |
| 70 | assert(pipe); |
| 71 | |
| 72 | if (!lttng_pipe_is_read_open(pipe)) { |
| 73 | goto end; |
| 74 | } |
| 75 | |
| 76 | do { |
| 77 | ret = close(pipe->fd[0]); |
| 78 | } while (ret < 0 && errno == EINTR); |
| 79 | if (ret < 0) { |
| 80 | PERROR("close lttng read pipe"); |
| 81 | ret_val = -errno; |
| 82 | } |
| 83 | pipe->r_state = LTTNG_PIPE_STATE_CLOSED; |
| 84 | |
| 85 | end: |
| 86 | return ret_val; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Internal function. Close write side of pipe WITHOUT locking the mutex. |
| 91 | * |
| 92 | * Return 0 on success else a negative errno from close(2). |
| 93 | */ |
| 94 | static int _pipe_write_close(struct lttng_pipe *pipe) |
| 95 | { |
| 96 | int ret, ret_val = 0; |
| 97 | |
| 98 | assert(pipe); |
| 99 | |
| 100 | if (!lttng_pipe_is_write_open(pipe)) { |
| 101 | goto end; |
| 102 | } |
| 103 | |
| 104 | do { |
| 105 | ret = close(pipe->fd[1]); |
| 106 | } while (ret < 0 && errno == EINTR); |
| 107 | if (ret < 0) { |
| 108 | PERROR("close lttng write pipe"); |
| 109 | ret_val = -errno; |
| 110 | } |
| 111 | pipe->w_state = LTTNG_PIPE_STATE_CLOSED; |
| 112 | |
| 113 | end: |
| 114 | return ret_val; |
| 115 | } |
| 116 | |
| 117 | static struct lttng_pipe *_pipe_create(void) |
| 118 | { |
| 119 | int ret; |
| 120 | struct lttng_pipe *p; |
| 121 | |
| 122 | p = zmalloc(sizeof(*p)); |
| 123 | if (!p) { |
| 124 | PERROR("zmalloc pipe create"); |
| 125 | goto end; |
| 126 | } |
| 127 | p->fd[0] = p->fd[1] = -1; |
| 128 | |
| 129 | ret = pthread_mutex_init(&p->read_mutex, NULL); |
| 130 | if (ret) { |
| 131 | PERROR("pthread_mutex_init read lock pipe create"); |
| 132 | goto error_destroy; |
| 133 | } |
| 134 | ret = pthread_mutex_init(&p->write_mutex, NULL); |
| 135 | if (ret) { |
| 136 | PERROR("pthread_mutex_init write lock pipe create"); |
| 137 | goto error_destroy_rmutex; |
| 138 | } |
| 139 | end: |
| 140 | return p; |
| 141 | error_destroy_rmutex: |
| 142 | (void) pthread_mutex_destroy(&p->read_mutex); |
| 143 | error_destroy: |
| 144 | free(p); |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | static int _pipe_set_flags(struct lttng_pipe *pipe, int flags) |
| 149 | { |
| 150 | int i, ret = 0; |
| 151 | |
| 152 | if (!flags) { |
| 153 | goto end; |
| 154 | } |
| 155 | |
| 156 | for (i = 0; i < 2; i++) { |
| 157 | ret = fcntl(pipe->fd[i], F_SETFD, flags); |
| 158 | if (ret < 0) { |
| 159 | PERROR("fcntl lttng pipe %d", flags); |
| 160 | goto end; |
| 161 | } |
| 162 | } |
| 163 | end: |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Open a new lttng pipe and set flags using fcntl(). |
| 169 | * |
| 170 | * Return a newly allocated lttng pipe on success or else NULL. |
| 171 | */ |
| 172 | LTTNG_HIDDEN |
| 173 | struct lttng_pipe *lttng_pipe_open(int flags) |
| 174 | { |
| 175 | int ret; |
| 176 | struct lttng_pipe *p; |
| 177 | |
| 178 | p = _pipe_create(); |
| 179 | if (!p) { |
| 180 | goto error; |
| 181 | } |
| 182 | |
| 183 | ret = pipe(p->fd); |
| 184 | if (ret < 0) { |
| 185 | PERROR("lttng pipe"); |
| 186 | goto error; |
| 187 | } |
| 188 | p->r_state = LTTNG_PIPE_STATE_OPENED; |
| 189 | p->w_state = LTTNG_PIPE_STATE_OPENED; |
| 190 | |
| 191 | ret = _pipe_set_flags(p, flags); |
| 192 | if (ret) { |
| 193 | goto error; |
| 194 | } |
| 195 | |
| 196 | p->flags = flags; |
| 197 | |
| 198 | return p; |
| 199 | error: |
| 200 | lttng_pipe_destroy(p); |
| 201 | return NULL; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Open a new lttng pipe at path and set flags using fcntl(). |
| 206 | * |
| 207 | * Return a newly allocated lttng pipe on success or else NULL. |
| 208 | */ |
| 209 | LTTNG_HIDDEN |
| 210 | struct lttng_pipe *lttng_pipe_named_open(const char *path, mode_t mode, |
| 211 | int flags) |
| 212 | { |
| 213 | int ret, fd_r, fd_w; |
| 214 | struct lttng_pipe *pipe; |
| 215 | |
| 216 | pipe = _pipe_create(); |
| 217 | if (!pipe) { |
| 218 | goto error; |
| 219 | } |
| 220 | |
| 221 | ret = mkfifo(path, mode); |
| 222 | if (ret) { |
| 223 | PERROR("mkfifo"); |
| 224 | goto error; |
| 225 | } |
| 226 | |
| 227 | fd_r = open(path, O_RDONLY | O_NONBLOCK); |
| 228 | if (fd_r < 0) { |
| 229 | PERROR("open fifo"); |
| 230 | goto error; |
| 231 | } |
| 232 | pipe->fd[0] = fd_r; |
| 233 | pipe->r_state = LTTNG_PIPE_STATE_OPENED; |
| 234 | |
| 235 | fd_w = open(path, O_WRONLY | O_NONBLOCK); |
| 236 | if (fd_w < 0) { |
| 237 | PERROR("open fifo"); |
| 238 | goto error; |
| 239 | } |
| 240 | pipe->fd[1] = fd_w; |
| 241 | pipe->w_state = LTTNG_PIPE_STATE_OPENED; |
| 242 | |
| 243 | ret = _pipe_set_flags(pipe, flags); |
| 244 | if (ret) { |
| 245 | goto error; |
| 246 | } |
| 247 | pipe->flags = flags; |
| 248 | |
| 249 | return pipe; |
| 250 | error: |
| 251 | lttng_pipe_destroy(pipe); |
| 252 | return NULL; |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Close read side of a lttng pipe. |
| 257 | * |
| 258 | * Return 0 on success else a negative value. |
| 259 | */ |
| 260 | LTTNG_HIDDEN |
| 261 | int lttng_pipe_read_close(struct lttng_pipe *pipe) |
| 262 | { |
| 263 | int ret; |
| 264 | |
| 265 | assert(pipe); |
| 266 | |
| 267 | /* Handle read side first. */ |
| 268 | lock_read_side(pipe); |
| 269 | ret = _pipe_read_close(pipe); |
| 270 | unlock_read_side(pipe); |
| 271 | |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * Close write side of a lttng pipe. |
| 277 | * |
| 278 | * Return 0 on success else a negative value. |
| 279 | */ |
| 280 | LTTNG_HIDDEN |
| 281 | int lttng_pipe_write_close(struct lttng_pipe *pipe) |
| 282 | { |
| 283 | int ret; |
| 284 | |
| 285 | assert(pipe); |
| 286 | |
| 287 | lock_write_side(pipe); |
| 288 | ret = _pipe_write_close(pipe); |
| 289 | unlock_write_side(pipe); |
| 290 | |
| 291 | return ret; |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Close both read and write side of a lttng pipe. |
| 296 | * |
| 297 | * Return 0 on success else a negative value. |
| 298 | */ |
| 299 | LTTNG_HIDDEN |
| 300 | int lttng_pipe_close(struct lttng_pipe *pipe) |
| 301 | { |
| 302 | int ret, ret_val = 0; |
| 303 | |
| 304 | assert(pipe); |
| 305 | |
| 306 | ret = lttng_pipe_read_close(pipe); |
| 307 | if (ret < 0) { |
| 308 | ret_val = ret; |
| 309 | } |
| 310 | |
| 311 | ret = lttng_pipe_write_close(pipe); |
| 312 | if (ret < 0) { |
| 313 | ret_val = ret; |
| 314 | } |
| 315 | |
| 316 | return ret_val; |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * Close and destroy a lttng pipe object. Finally, pipe is freed. |
| 321 | */ |
| 322 | LTTNG_HIDDEN |
| 323 | void lttng_pipe_destroy(struct lttng_pipe *pipe) |
| 324 | { |
| 325 | int ret; |
| 326 | |
| 327 | if (!pipe) { |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Destroy should *never* be called with a locked mutex. These must always |
| 333 | * succeed so we unlock them after the close pipe below. |
| 334 | */ |
| 335 | ret = pthread_mutex_trylock(&pipe->read_mutex); |
| 336 | assert(!ret); |
| 337 | ret = pthread_mutex_trylock(&pipe->write_mutex); |
| 338 | assert(!ret); |
| 339 | |
| 340 | /* Close pipes WITHOUT trying to lock the pipes. */ |
| 341 | (void) _pipe_read_close(pipe); |
| 342 | (void) _pipe_write_close(pipe); |
| 343 | |
| 344 | unlock_read_side(pipe); |
| 345 | unlock_write_side(pipe); |
| 346 | |
| 347 | (void) pthread_mutex_destroy(&pipe->read_mutex); |
| 348 | (void) pthread_mutex_destroy(&pipe->write_mutex); |
| 349 | |
| 350 | free(pipe); |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Read on a lttng pipe and put the data in buf of at least size count. |
| 355 | * |
| 356 | * Return "count" on success. Return < count on error. errno can be used |
| 357 | * to check the actual error. |
| 358 | */ |
| 359 | LTTNG_HIDDEN |
| 360 | ssize_t lttng_pipe_read(struct lttng_pipe *pipe, void *buf, size_t count) |
| 361 | { |
| 362 | ssize_t ret; |
| 363 | |
| 364 | assert(pipe); |
| 365 | assert(buf); |
| 366 | |
| 367 | lock_read_side(pipe); |
| 368 | if (!lttng_pipe_is_read_open(pipe)) { |
| 369 | ret = -1; |
| 370 | errno = EBADF; |
| 371 | goto error; |
| 372 | } |
| 373 | ret = lttng_read(pipe->fd[0], buf, count); |
| 374 | error: |
| 375 | unlock_read_side(pipe); |
| 376 | return ret; |
| 377 | } |
| 378 | |
| 379 | /* |
| 380 | * Write on a lttng pipe using the data in buf and size of count. |
| 381 | * |
| 382 | * Return "count" on success. Return < count on error. errno can be used |
| 383 | * to check the actual error. |
| 384 | */ |
| 385 | LTTNG_HIDDEN |
| 386 | ssize_t lttng_pipe_write(struct lttng_pipe *pipe, const void *buf, |
| 387 | size_t count) |
| 388 | { |
| 389 | ssize_t ret; |
| 390 | |
| 391 | assert(pipe); |
| 392 | assert(buf); |
| 393 | |
| 394 | lock_write_side(pipe); |
| 395 | if (!lttng_pipe_is_write_open(pipe)) { |
| 396 | ret = -1; |
| 397 | errno = EBADF; |
| 398 | goto error; |
| 399 | } |
| 400 | ret = lttng_write(pipe->fd[1], buf, count); |
| 401 | error: |
| 402 | unlock_write_side(pipe); |
| 403 | return ret; |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * Return and release the read end of the pipe. |
| 408 | * |
| 409 | * This call transfers the ownership of the read fd of the underlying pipe |
| 410 | * to the caller if it is still open. |
| 411 | * |
| 412 | * Returns the fd of the read end of the pipe, or -1 if it was already closed or |
| 413 | * released. |
| 414 | */ |
| 415 | LTTNG_HIDDEN |
| 416 | int lttng_pipe_release_readfd(struct lttng_pipe *pipe) |
| 417 | { |
| 418 | int ret; |
| 419 | |
| 420 | if (!pipe) { |
| 421 | ret = -1; |
| 422 | goto end; |
| 423 | } |
| 424 | |
| 425 | lock_read_side(pipe); |
| 426 | if (!lttng_pipe_is_read_open(pipe)) { |
| 427 | ret = -1; |
| 428 | goto end_unlock; |
| 429 | } |
| 430 | ret = pipe->fd[0]; |
| 431 | pipe->fd[0] = -1; |
| 432 | pipe->r_state = LTTNG_PIPE_STATE_CLOSED; |
| 433 | end_unlock: |
| 434 | unlock_read_side(pipe); |
| 435 | end: |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | * Return and release the write end of the pipe. |
| 441 | * |
| 442 | * This call transfers the ownership of the write fd of the underlying pipe |
| 443 | * to the caller if it is still open. |
| 444 | * |
| 445 | * Returns the fd of the write end of the pipe, or -1 if it was alwritey closed |
| 446 | * or released. |
| 447 | */ |
| 448 | LTTNG_HIDDEN |
| 449 | int lttng_pipe_release_writefd(struct lttng_pipe *pipe) |
| 450 | { |
| 451 | int ret; |
| 452 | |
| 453 | if (!pipe) { |
| 454 | ret = -1; |
| 455 | goto end; |
| 456 | } |
| 457 | |
| 458 | lock_write_side(pipe); |
| 459 | if (!lttng_pipe_is_write_open(pipe)) { |
| 460 | ret = -1; |
| 461 | goto end_unlock; |
| 462 | } |
| 463 | ret = pipe->fd[1]; |
| 464 | pipe->fd[1] = -1; |
| 465 | pipe->w_state = LTTNG_PIPE_STATE_CLOSED; |
| 466 | end_unlock: |
| 467 | unlock_write_side(pipe); |
| 468 | end: |
| 469 | return ret; |
| 470 | } |