bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / file_io / os2 / pipe.c
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
27 APR_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
107 APR_DECLARE(apr_status_t) apr_file_namedpipe_create(const char *filename, apr_fileperms_t perm, apr_pool_t *pool)
108 {
109     /* Not yet implemented, interface not suitable */
110     return APR_ENOTIMPL;
111
112
113  
114
115 APR_DECLARE(apr_status_t) apr_file_pipe_timeout_set(apr_file_t *thepipe, apr_interval_time_t timeout)
116 {
117     if (thepipe->pipe == 1) {
118         thepipe->timeout = timeout;
119
120         if (thepipe->timeout >= 0) {
121             if (thepipe->blocking != BLK_OFF) {
122                 thepipe->blocking = BLK_OFF;
123                 return APR_FROM_OS_ERROR(DosSetNPHState(thepipe->filedes, NP_NOWAIT));
124             }
125         }
126         else if (thepipe->timeout == -1) {
127             if (thepipe->blocking != BLK_ON) {
128                 thepipe->blocking = BLK_ON;
129                 return APR_FROM_OS_ERROR(DosSetNPHState(thepipe->filedes, NP_WAIT));
130             }
131         }
132     }
133     return APR_EINVAL;
134 }
135
136
137
138 APR_DECLARE(apr_status_t) apr_file_pipe_timeout_get(apr_file_t *thepipe, apr_interval_time_t *timeout)
139 {
140     if (thepipe->pipe == 1) {
141         *timeout = thepipe->timeout;
142         return APR_SUCCESS;
143     }
144     return APR_EINVAL;
145 }
146
147
148
149 APR_DECLARE(apr_status_t) apr_os_pipe_put_ex(apr_file_t **file,
150                                              apr_os_file_t *thefile,
151                                              int register_cleanup,
152                                              apr_pool_t *pool)
153 {
154     (*file) = apr_pcalloc(pool, sizeof(apr_file_t));
155     (*file)->pool = pool;
156     (*file)->isopen = TRUE;
157     (*file)->pipe = 1;
158     (*file)->blocking = BLK_UNKNOWN; /* app needs to make a timeout call */
159     (*file)->timeout = -1;
160     (*file)->filedes = *thefile;
161
162     if (register_cleanup) {
163         apr_pool_cleanup_register(pool, *file, apr_file_cleanup,
164                                   apr_pool_cleanup_null);
165     }
166
167     return APR_SUCCESS;
168 }
169
170
171
172 APR_DECLARE(apr_status_t) apr_os_pipe_put(apr_file_t **file,
173                                           apr_os_file_t *thefile,
174                                           apr_pool_t *pool)
175 {
176     return apr_os_pipe_put_ex(file, thefile, 0, pool);
177 }