| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License, version 2 only, |
| 7 | * as published by the Free Software Foundation. |
| 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 along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | #define _GNU_SOURCE |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <limits.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include <common/error.h> |
| 29 | |
| 30 | #include "utils.h" |
| 31 | |
| 32 | /* |
| 33 | * Write to writable pipe used to notify a thread. |
| 34 | */ |
| 35 | int notify_thread_pipe(int wpipe) |
| 36 | { |
| 37 | int ret; |
| 38 | |
| 39 | ret = write(wpipe, "!", 1); |
| 40 | if (ret < 0) { |
| 41 | PERROR("write poll pipe"); |
| 42 | } |
| 43 | |
| 44 | return ret; |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * Return pointer to home directory path using the env variable HOME. |
| 49 | * |
| 50 | * No home, NULL is returned. |
| 51 | */ |
| 52 | const char *get_home_dir(void) |
| 53 | { |
| 54 | return ((const char *) getenv("HOME")); |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Create a pipe in dst. |
| 59 | */ |
| 60 | int utils_create_pipe(int *dst) |
| 61 | { |
| 62 | int ret; |
| 63 | |
| 64 | if (dst == NULL) { |
| 65 | return -1; |
| 66 | } |
| 67 | |
| 68 | ret = pipe(dst); |
| 69 | if (ret < 0) { |
| 70 | PERROR("create pipe"); |
| 71 | } |
| 72 | |
| 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Create pipe and set CLOEXEC flag to both fd. |
| 78 | * |
| 79 | * Make sure the pipe opened by this function are closed at some point. Use |
| 80 | * utils_close_pipe(). |
| 81 | */ |
| 82 | int utils_create_pipe_cloexec(int *dst) |
| 83 | { |
| 84 | int ret, i; |
| 85 | |
| 86 | if (dst == NULL) { |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | ret = utils_create_pipe(dst); |
| 91 | if (ret < 0) { |
| 92 | goto error; |
| 93 | } |
| 94 | |
| 95 | for (i = 0; i < 2; i++) { |
| 96 | ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC); |
| 97 | if (ret < 0) { |
| 98 | PERROR("fcntl pipe cloexec"); |
| 99 | goto error; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | error: |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Close both read and write side of the pipe. |
| 109 | */ |
| 110 | void utils_close_pipe(int *src) |
| 111 | { |
| 112 | int i, ret; |
| 113 | |
| 114 | if (src == NULL) { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | for (i = 0; i < 2; i++) { |
| 119 | /* Safety check */ |
| 120 | if (src[i] < 0) { |
| 121 | continue; |
| 122 | } |
| 123 | |
| 124 | ret = close(src[i]); |
| 125 | if (ret) { |
| 126 | PERROR("close pipe"); |
| 127 | } |
| 128 | } |
| 129 | } |