1/* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved. 2 * 3 * Redistribution and use in source and binary forms, with or without 4 * modification, are permitted provided that the following conditions are 5 * met: 6 * * Redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer. 8 * * Redistributions in binary form must reproduce the above 9 * copyright notice, this list of conditions and the following 10 * disclaimer in the documentation and/or other materials provided 11 * with the distribution. 12 * * Neither the name of The Linux Foundation, nor the names of its 13 * contributors may be used to endorse or promote products derived 14 * from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 */ 29#include <string.h> 30#include <unistd.h> 31#include <errno.h> 32 33// #include <linux/stat.h> 34#include <fcntl.h> 35// #include <linux/types.h> 36#include <sys/types.h> 37#include <sys/stat.h> 38 39#include "loc_eng_dmn_conn_glue_pipe.h" 40#include "log_util.h" 41#include "platform_lib_includes.h" 42/*=========================================================================== 43FUNCTION loc_eng_dmn_conn_glue_pipeget 44 45DESCRIPTION 46 create a named pipe. 47 48 pipe_name - pipe name path 49 mode - mode 50 51DEPENDENCIES 52 None 53 54RETURN VALUE 55 0: success or negative value for failure 56 57SIDE EFFECTS 58 N/A 59 60===========================================================================*/ 61int loc_eng_dmn_conn_glue_pipeget(const char * pipe_name, int mode) 62{ 63 int fd; 64 int result; 65 66 LOC_LOGD("%s, mode = %d\n", pipe_name, mode); 67 result = mkfifo(pipe_name, 0660); 68 69 if ((result == -1) && (errno != EEXIST)) { 70 LOC_LOGE("failed: %s\n", strerror(errno)); 71 return result; 72 } 73 74 // The mode in mkfifo is not honoured and does not provide the 75 // group permissions. Doing chmod to add group permissions. 76 result = chmod (pipe_name, 0660); 77 if (result != 0){ 78 LOC_LOGE ("%s failed to change mode for %s, error = %s\n", __func__, 79 pipe_name, strerror(errno)); 80 } 81 82 fd = open(pipe_name, mode); 83 if (fd <= 0) 84 { 85 LOC_LOGE("failed: %s\n", strerror(errno)); 86 } 87 LOC_LOGD("fd = %d, %s\n", fd, pipe_name); 88 return fd; 89} 90 91/*=========================================================================== 92FUNCTION loc_eng_dmn_conn_glue_piperemove 93 94DESCRIPTION 95 remove a pipe 96 97 pipe_name - pipe name path 98 fd - fd for the pipe 99 100DEPENDENCIES 101 None 102 103RETURN VALUE 104 0: success 105 106SIDE EFFECTS 107 N/A 108 109===========================================================================*/ 110int loc_eng_dmn_conn_glue_piperemove(const char * pipe_name, int fd) 111{ 112 close(fd); 113 if (pipe_name) unlink(pipe_name); 114 LOC_LOGD("fd = %d, %s\n", fd, pipe_name); 115 return 0; 116} 117 118/*=========================================================================== 119FUNCTION loc_eng_dmn_conn_glue_pipewrite 120 121DESCRIPTION 122 write to a pipe 123 124 fd - fd of a pipe 125 buf - buffer for the data to write 126 sz - size of the data in buffer 127 128DEPENDENCIES 129 None 130 131RETURN VALUE 132 number of bytes written or negative value for failure 133 134SIDE EFFECTS 135 N/A 136 137===========================================================================*/ 138int loc_eng_dmn_conn_glue_pipewrite(int fd, const void * buf, size_t sz) 139{ 140 int result; 141 142 result = write(fd, buf, sz); 143 144 /* @todo check for non EINTR & EAGAIN, shall not do select again, select_tut Law 7) */ 145 146 /* LOC_LOGD("fd = %d, buf = 0x%lx, size = %d, result = %d\n", fd, (long) buf, (int) sz, (int) result); */ 147 return result; 148} 149 150/*=========================================================================== 151FUNCTION loc_eng_dmn_conn_glue_piperead 152 153DESCRIPTION 154 read from a pipe 155 156 fd - fd for the pipe 157 buf - buffer to hold the data read from pipe 158 sz - size of the buffer 159 160DEPENDENCIES 161 None 162 163RETURN VALUE 164 number of bytes read from pipe or negative value for failure 165 166SIDE EFFECTS 167 N/A 168 169===========================================================================*/ 170int loc_eng_dmn_conn_glue_piperead(int fd, void * buf, size_t sz) 171{ 172 int len; 173 174 len = read(fd, buf, sz); 175 176 /* @todo check for non EINTR & EAGAIN, shall not do select again, select_tut Law 7) */ 177 178 /* LOC_LOGD("fd = %d, buf = 0x%lx, size = %d, len = %d\n", fd, (long) buf, (int) sz, len); */ 179 return len; 180} 181 182/*=========================================================================== 183FUNCTION loc_eng_dmn_conn_glue_pipeunblock 184 185DESCRIPTION 186 unblock a pipe 187 188 fd - fd for the pipe 189 190DEPENDENCIES 191 None 192 193RETURN VALUE 194 0 for success or negative value for failure 195 196SIDE EFFECTS 197 N/A 198 199===========================================================================*/ 200int loc_eng_dmn_conn_glue_pipeunblock(int fd) 201{ 202 int result; 203 struct flock flock_v; 204 LOC_LOGD("\n"); 205// result = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NDELAY); 206 flock_v.l_type = F_UNLCK; 207 flock_v.l_len = 32; 208 result = fcntl(fd, F_SETLK, &flock_v); 209 if (result < 0) { 210 LOC_LOGE("fcntl failure, %s\n", strerror(errno)); 211 } 212 213 return result; 214} 215