| 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 | if (flags & O_NONBLOCK) { |
| 158 | ret = fcntl(pipe->fd[i], F_SETFL, O_NONBLOCK); |
| 159 | if (ret < 0) { |
| 160 | PERROR("fcntl lttng pipe %d", flags); |
| 161 | goto end; |
| 162 | } |
| 163 | } |
| 164 | if (flags & FD_CLOEXEC) { |
| 165 | ret = fcntl(pipe->fd[i], F_SETFD, FD_CLOEXEC); |
| 166 | if (ret < 0) { |
| 167 | PERROR("fcntl lttng pipe %d", flags); |
| 168 | goto end; |
| 169 | } |
| 170 | } |
| 171 | /* |
| 172 | * We only check for O_NONBLOCK or FD_CLOEXEC, if another flag is |
| 173 | * needed, we can add it, but for now just make sure we don't make |
| 174 | * mistakes with the parameters we pass. |
| 175 | */ |
| 176 | if (!(flags & O_NONBLOCK) && !(flags & FD_CLOEXEC)) { |
| 177 | fprintf(stderr, "Unsupported flag\n"); |
| 178 | ret = -1; |
| 179 | goto end; |
| 180 | } |
| 181 | } |
| 182 | end: |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Open a new lttng pipe and set flags using fcntl(). |
| 188 | * |
| 189 | * Return a newly allocated lttng pipe on success or else NULL. |
| 190 | */ |
| 191 | LTTNG_HIDDEN |
| 192 | struct lttng_pipe *lttng_pipe_open(int flags) |
| 193 | { |
| 194 | int ret; |
| 195 | struct lttng_pipe *p; |
| 196 | |
| 197 | p = _pipe_create(); |
| 198 | if (!p) { |
| 199 | goto error; |
| 200 | } |
| 201 | |
| 202 | ret = pipe(p->fd); |
| 203 | if (ret < 0) { |
| 204 | PERROR("lttng pipe"); |
| 205 | goto error; |
| 206 | } |
| 207 | p->r_state = LTTNG_PIPE_STATE_OPENED; |
| 208 | p->w_state = LTTNG_PIPE_STATE_OPENED; |
| 209 | |
| 210 | ret = _pipe_set_flags(p, flags); |
| 211 | if (ret) { |
| 212 | goto error; |
| 213 | } |
| 214 | |
| 215 | p->flags = flags; |
| 216 | |
| 217 | return p; |
| 218 | error: |
| 219 | lttng_pipe_destroy(p); |
| 220 | return NULL; |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Open a new lttng pipe at path and set flags using fcntl(). |
| 225 | * |
| 226 | * Return a newly allocated lttng pipe on success or else NULL. |
| 227 | */ |
| 228 | LTTNG_HIDDEN |
| 229 | struct lttng_pipe *lttng_pipe_named_open(const char *path, mode_t mode, |
| 230 | int flags) |
| 231 | { |
| 232 | int ret, fd_r, fd_w; |
| 233 | struct lttng_pipe *pipe; |
| 234 | |
| 235 | pipe = _pipe_create(); |
| 236 | if (!pipe) { |
| 237 | goto error; |
| 238 | } |
| 239 | |
| 240 | ret = mkfifo(path, mode); |
| 241 | if (ret) { |
| 242 | PERROR("mkfifo"); |
| 243 | goto error; |
| 244 | } |
| 245 | |
| 246 | fd_r = open(path, O_RDONLY | O_NONBLOCK); |
| 247 | if (fd_r < 0) { |
| 248 | PERROR("open fifo"); |
| 249 | goto error; |
| 250 | } |
| 251 | pipe->fd[0] = fd_r; |
| 252 | pipe->r_state = LTTNG_PIPE_STATE_OPENED; |
| 253 | |
| 254 | fd_w = open(path, O_WRONLY | O_NONBLOCK); |
| 255 | if (fd_w < 0) { |
| 256 | PERROR("open fifo"); |
| 257 | goto error; |
| 258 | } |
| 259 | pipe->fd[1] = fd_w; |
| 260 | pipe->w_state = LTTNG_PIPE_STATE_OPENED; |
| 261 | |
| 262 | ret = _pipe_set_flags(pipe, flags); |
| 263 | if (ret) { |
| 264 | goto error; |
| 265 | } |
| 266 | pipe->flags = flags; |
| 267 | |
| 268 | return pipe; |
| 269 | error: |
| 270 | lttng_pipe_destroy(pipe); |
| 271 | return NULL; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * Close read side of a lttng pipe. |
| 276 | * |
| 277 | * Return 0 on success else a negative value. |
| 278 | */ |
| 279 | LTTNG_HIDDEN |
| 280 | int lttng_pipe_read_close(struct lttng_pipe *pipe) |
| 281 | { |
| 282 | int ret; |
| 283 | |
| 284 | assert(pipe); |
| 285 | |
| 286 | /* Handle read side first. */ |
| 287 | lock_read_side(pipe); |
| 288 | ret = _pipe_read_close(pipe); |
| 289 | unlock_read_side(pipe); |
| 290 | |
| 291 | return ret; |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Close write side of a lttng pipe. |
| 296 | * |
| 297 | * Return 0 on success else a negative value. |
| 298 | */ |
| 299 | LTTNG_HIDDEN |
| 300 | int lttng_pipe_write_close(struct lttng_pipe *pipe) |
| 301 | { |
| 302 | int ret; |
| 303 | |
| 304 | assert(pipe); |
| 305 | |
| 306 | lock_write_side(pipe); |
| 307 | ret = _pipe_write_close(pipe); |
| 308 | unlock_write_side(pipe); |
| 309 | |
| 310 | return ret; |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * Close both read and write side of a lttng pipe. |
| 315 | * |
| 316 | * Return 0 on success else a negative value. |
| 317 | */ |
| 318 | LTTNG_HIDDEN |
| 319 | int lttng_pipe_close(struct lttng_pipe *pipe) |
| 320 | { |
| 321 | int ret, ret_val = 0; |
| 322 | |
| 323 | assert(pipe); |
| 324 | |
| 325 | ret = lttng_pipe_read_close(pipe); |
| 326 | if (ret < 0) { |
| 327 | ret_val = ret; |
| 328 | } |
| 329 | |
| 330 | ret = lttng_pipe_write_close(pipe); |
| 331 | if (ret < 0) { |
| 332 | ret_val = ret; |
| 333 | } |
| 334 | |
| 335 | return ret_val; |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Close and destroy a lttng pipe object. Finally, pipe is freed. |
| 340 | */ |
| 341 | LTTNG_HIDDEN |
| 342 | void lttng_pipe_destroy(struct lttng_pipe *pipe) |
| 343 | { |
| 344 | int ret; |
| 345 | |
| 346 | if (!pipe) { |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | * Destroy should *never* be called with a locked mutex. These must always |
| 352 | * succeed so we unlock them after the close pipe below. |
| 353 | */ |
| 354 | ret = pthread_mutex_trylock(&pipe->read_mutex); |
| 355 | assert(!ret); |
| 356 | ret = pthread_mutex_trylock(&pipe->write_mutex); |
| 357 | assert(!ret); |
| 358 | |
| 359 | /* Close pipes WITHOUT trying to lock the pipes. */ |
| 360 | (void) _pipe_read_close(pipe); |
| 361 | (void) _pipe_write_close(pipe); |
| 362 | |
| 363 | unlock_read_side(pipe); |
| 364 | unlock_write_side(pipe); |
| 365 | |
| 366 | (void) pthread_mutex_destroy(&pipe->read_mutex); |
| 367 | (void) pthread_mutex_destroy(&pipe->write_mutex); |
| 368 | |
| 369 | free(pipe); |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * Read on a lttng pipe and put the data in buf of at least size count. |
| 374 | * |
| 375 | * Return "count" on success. Return < count on error. errno can be used |
| 376 | * to check the actual error. |
| 377 | */ |
| 378 | LTTNG_HIDDEN |
| 379 | ssize_t lttng_pipe_read(struct lttng_pipe *pipe, void *buf, size_t count) |
| 380 | { |
| 381 | ssize_t ret; |
| 382 | |
| 383 | assert(pipe); |
| 384 | assert(buf); |
| 385 | |
| 386 | lock_read_side(pipe); |
| 387 | if (!lttng_pipe_is_read_open(pipe)) { |
| 388 | ret = -1; |
| 389 | errno = EBADF; |
| 390 | goto error; |
| 391 | } |
| 392 | ret = lttng_read(pipe->fd[0], buf, count); |
| 393 | error: |
| 394 | unlock_read_side(pipe); |
| 395 | return ret; |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * Write on a lttng pipe using the data in buf and size of count. |
| 400 | * |
| 401 | * Return "count" on success. Return < count on error. errno can be used |
| 402 | * to check the actual error. |
| 403 | */ |
| 404 | LTTNG_HIDDEN |
| 405 | ssize_t lttng_pipe_write(struct lttng_pipe *pipe, const void *buf, |
| 406 | size_t count) |
| 407 | { |
| 408 | ssize_t ret; |
| 409 | |
| 410 | assert(pipe); |
| 411 | assert(buf); |
| 412 | |
| 413 | lock_write_side(pipe); |
| 414 | if (!lttng_pipe_is_write_open(pipe)) { |
| 415 | ret = -1; |
| 416 | errno = EBADF; |
| 417 | goto error; |
| 418 | } |
| 419 | ret = lttng_write(pipe->fd[1], buf, count); |
| 420 | error: |
| 421 | unlock_write_side(pipe); |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | /* |
| 426 | * Return and release the read end of the pipe. |
| 427 | * |
| 428 | * This call transfers the ownership of the read fd of the underlying pipe |
| 429 | * to the caller if it is still open. |
| 430 | * |
| 431 | * Returns the fd of the read end of the pipe, or -1 if it was already closed or |
| 432 | * released. |
| 433 | */ |
| 434 | LTTNG_HIDDEN |
| 435 | int lttng_pipe_release_readfd(struct lttng_pipe *pipe) |
| 436 | { |
| 437 | int ret; |
| 438 | |
| 439 | if (!pipe) { |
| 440 | ret = -1; |
| 441 | goto end; |
| 442 | } |
| 443 | |
| 444 | lock_read_side(pipe); |
| 445 | if (!lttng_pipe_is_read_open(pipe)) { |
| 446 | ret = -1; |
| 447 | goto end_unlock; |
| 448 | } |
| 449 | ret = pipe->fd[0]; |
| 450 | pipe->fd[0] = -1; |
| 451 | pipe->r_state = LTTNG_PIPE_STATE_CLOSED; |
| 452 | end_unlock: |
| 453 | unlock_read_side(pipe); |
| 454 | end: |
| 455 | return ret; |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * Return and release the write end of the pipe. |
| 460 | * |
| 461 | * This call transfers the ownership of the write fd of the underlying pipe |
| 462 | * to the caller if it is still open. |
| 463 | * |
| 464 | * Returns the fd of the write end of the pipe, or -1 if it was alwritey closed |
| 465 | * or released. |
| 466 | */ |
| 467 | LTTNG_HIDDEN |
| 468 | int lttng_pipe_release_writefd(struct lttng_pipe *pipe) |
| 469 | { |
| 470 | int ret; |
| 471 | |
| 472 | if (!pipe) { |
| 473 | ret = -1; |
| 474 | goto end; |
| 475 | } |
| 476 | |
| 477 | lock_write_side(pipe); |
| 478 | if (!lttng_pipe_is_write_open(pipe)) { |
| 479 | ret = -1; |
| 480 | goto end_unlock; |
| 481 | } |
| 482 | ret = pipe->fd[1]; |
| 483 | pipe->fd[1] = -1; |
| 484 | pipe->w_state = LTTNG_PIPE_STATE_CLOSED; |
| 485 | end_unlock: |
| 486 | unlock_write_side(pipe); |
| 487 | end: |
| 488 | return ret; |
| 489 | } |