1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#define INCL_DOSERRORS 18#include "apr_arch_file_io.h" 19#include "apr_file_io.h" 20#include "apr_general.h" 21#include "apr_lib.h" 22#include "apr_strings.h" 23#include "apr_portable.h" 24#include <string.h> 25#include <process.h> 26 27APR_DECLARE(apr_status_t) apr_file_pipe_create(apr_file_t **in, apr_file_t **out, apr_pool_t *pool) 28{ 29 ULONG filedes[2]; 30 ULONG rc, action; 31 static int id = 0; 32 char pipename[50]; 33 34 sprintf(pipename, "/pipe/%d.%d", getpid(), id++); 35 rc = DosCreateNPipe(pipename, filedes, NP_ACCESS_INBOUND, NP_NOWAIT|1, 4096, 4096, 0); 36 37 if (rc) 38 return APR_FROM_OS_ERROR(rc); 39 40 rc = DosConnectNPipe(filedes[0]); 41 42 if (rc && rc != ERROR_PIPE_NOT_CONNECTED) { 43 DosClose(filedes[0]); 44 return APR_FROM_OS_ERROR(rc); 45 } 46 47 rc = DosOpen (pipename, filedes+1, &action, 0, FILE_NORMAL, 48 OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW, 49 OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYREADWRITE, 50 NULL); 51 52 if (rc) { 53 DosClose(filedes[0]); 54 return APR_FROM_OS_ERROR(rc); 55 } 56 57 (*in) = (apr_file_t *)apr_palloc(pool, sizeof(apr_file_t)); 58 rc = DosCreateEventSem(NULL, &(*in)->pipeSem, DC_SEM_SHARED, FALSE); 59 60 if (rc) { 61 DosClose(filedes[0]); 62 DosClose(filedes[1]); 63 return APR_FROM_OS_ERROR(rc); 64 } 65 66 rc = DosSetNPipeSem(filedes[0], (HSEM)(*in)->pipeSem, 1); 67 68 if (!rc) { 69 rc = DosSetNPHState(filedes[0], NP_WAIT); 70 } 71 72 if (rc) { 73 DosClose(filedes[0]); 74 DosClose(filedes[1]); 75 DosCloseEventSem((*in)->pipeSem); 76 return APR_FROM_OS_ERROR(rc); 77 } 78 79 (*in)->pool = pool; 80 (*in)->filedes = filedes[0]; 81 (*in)->fname = apr_pstrdup(pool, pipename); 82 (*in)->isopen = TRUE; 83 (*in)->buffered = FALSE; 84 (*in)->flags = 0; 85 (*in)->pipe = 1; 86 (*in)->timeout = -1; 87 (*in)->blocking = BLK_ON; 88 apr_pool_cleanup_register(pool, *in, apr_file_cleanup, apr_pool_cleanup_null); 89 90 (*out) = (apr_file_t *)apr_palloc(pool, sizeof(apr_file_t)); 91 (*out)->pool = pool; 92 (*out)->filedes = filedes[1]; 93 (*out)->fname = apr_pstrdup(pool, pipename); 94 (*out)->isopen = TRUE; 95 (*out)->buffered = FALSE; 96 (*out)->flags = 0; 97 (*out)->pipe = 1; 98 (*out)->timeout = -1; 99 (*out)->blocking = BLK_ON; 100 apr_pool_cleanup_register(pool, *out, apr_file_cleanup, apr_pool_cleanup_null); 101 102 return APR_SUCCESS; 103} 104 105 106 107APR_DECLARE(apr_status_t) apr_file_pipe_create_ex(apr_file_t **in, 108 apr_file_t **out, 109 apr_int32_t blocking, 110 apr_pool_t *pool) 111{ 112 apr_status_t status; 113 114 if ((status = apr_file_pipe_create(in, out, pool)) != APR_SUCCESS) 115 return status; 116 117 switch (blocking) { 118 case APR_FULL_BLOCK: 119 break; 120 case APR_READ_BLOCK: 121 apr_file_pipe_timeout_set(*out, 0); 122 break; 123 case APR_WRITE_BLOCK: 124 apr_file_pipe_timeout_set(*in, 0); 125 break; 126 default: 127 apr_file_pipe_timeout_set(*out, 0); 128 apr_file_pipe_timeout_set(*in, 0); 129 } 130 131 return APR_SUCCESS; 132} 133 134 135 136APR_DECLARE(apr_status_t) apr_file_namedpipe_create(const char *filename, apr_fileperms_t perm, apr_pool_t *pool) 137{ 138 /* Not yet implemented, interface not suitable */ 139 return APR_ENOTIMPL; 140} 141 142 143 144APR_DECLARE(apr_status_t) apr_file_pipe_timeout_set(apr_file_t *thepipe, apr_interval_time_t timeout) 145{ 146 if (thepipe->pipe == 1) { 147 thepipe->timeout = timeout; 148 149 if (thepipe->timeout >= 0) { 150 if (thepipe->blocking != BLK_OFF) { 151 thepipe->blocking = BLK_OFF; 152 return APR_FROM_OS_ERROR(DosSetNPHState(thepipe->filedes, NP_NOWAIT)); 153 } 154 } 155 else if (thepipe->timeout == -1) { 156 if (thepipe->blocking != BLK_ON) { 157 thepipe->blocking = BLK_ON; 158 return APR_FROM_OS_ERROR(DosSetNPHState(thepipe->filedes, NP_WAIT)); 159 } 160 } 161 } 162 return APR_EINVAL; 163} 164 165 166 167APR_DECLARE(apr_status_t) apr_file_pipe_timeout_get(apr_file_t *thepipe, apr_interval_time_t *timeout) 168{ 169 if (thepipe->pipe == 1) { 170 *timeout = thepipe->timeout; 171 return APR_SUCCESS; 172 } 173 return APR_EINVAL; 174} 175 176 177 178APR_DECLARE(apr_status_t) apr_os_pipe_put_ex(apr_file_t **file, 179 apr_os_file_t *thefile, 180 int register_cleanup, 181 apr_pool_t *pool) 182{ 183 (*file) = apr_pcalloc(pool, sizeof(apr_file_t)); 184 (*file)->pool = pool; 185 (*file)->isopen = TRUE; 186 (*file)->pipe = 1; 187 (*file)->blocking = BLK_UNKNOWN; /* app needs to make a timeout call */ 188 (*file)->timeout = -1; 189 (*file)->filedes = *thefile; 190 191 if (register_cleanup) { 192 apr_pool_cleanup_register(pool, *file, apr_file_cleanup, 193 apr_pool_cleanup_null); 194 } 195 196 return APR_SUCCESS; 197} 198 199 200 201APR_DECLARE(apr_status_t) apr_os_pipe_put(apr_file_t **file, 202 apr_os_file_t *thefile, 203 apr_pool_t *pool) 204{ 205 return apr_os_pipe_put_ex(file, thefile, 0, pool); 206} 207