upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / test / testflock.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 /*
18  * USAGE
19  *
20  * Start one process, no args, and place it into the background. Start a
21  * second process with the "-r" switch to attempt a read on the file
22  * created by the first process.
23  *
24  * $ ./testflock &
25  * ...messages...
26  * $ ./testflock -r
27  * ...messages...
28  *
29  * The first process will sleep for 30 seconds while holding a lock. The
30  * second process will attempt to grab it (non-blocking) and fail. It
31  * will then grab it with a blocking scheme. When the first process' 30
32  * seconds are up, it will exit (thus releasing its lock). The second
33  * process will acquire the lock, then exit.
34  */
35
36 #include "apr_pools.h"
37 #include "apr_file_io.h"
38 #include "apr_time.h"
39 #include "apr_general.h"
40 #include "apr_getopt.h"
41 #include "apr_strings.h"
42
43 #include <stdlib.h>
44 #include <stdio.h>
45
46 const char *testfile = "testfile.tmp";
47
48 static apr_pool_t *pool = NULL;
49
50 static void errmsg(const char *msg)
51 {
52     if (pool != NULL)
53         apr_pool_destroy(pool);
54     fprintf(stderr, msg);
55     exit(1);
56 }
57
58 static void errmsg2(const char *msg, apr_status_t rv)
59 {
60     char *newmsg;
61     char errstr[120];
62
63     apr_strerror(rv, errstr, sizeof errstr);
64     newmsg = apr_psprintf(pool, "%s: %s (%d)\n",
65                           msg, errstr, rv);
66     errmsg(newmsg);
67     exit(1);
68 }
69
70 static void do_read(void)
71 {
72     apr_file_t *file;
73     apr_status_t status;
74
75     if (apr_file_open(&file, testfile, APR_WRITE,
76                  APR_OS_DEFAULT, pool) != APR_SUCCESS)
77         errmsg("Could not open test file.\n");
78     printf("Test file opened.\n");
79
80     status = apr_file_lock(file, APR_FLOCK_EXCLUSIVE | APR_FLOCK_NONBLOCK);
81     if (!APR_STATUS_IS_EAGAIN(status)) {
82         char msg[200];
83         errmsg(apr_psprintf(pool, "Expected APR_EAGAIN. Got %d: %s.\n",
84                             status, apr_strerror(status, msg, sizeof(msg))));
85     }
86     printf("First attempt: we were properly locked out.\nWaiting for lock...");
87     fflush(stdout);
88
89     if (apr_file_lock(file, APR_FLOCK_EXCLUSIVE) != APR_SUCCESS)
90         errmsg("Could not establish lock on test file.");
91     printf(" got it.\n");
92
93     (void) apr_file_close(file);
94     printf("Exiting.\n");
95 }
96
97 static void do_write(void)
98 {
99     apr_file_t *file;
100     apr_status_t rv;
101
102     if (apr_file_open(&file, testfile, APR_WRITE|APR_CREATE, APR_OS_DEFAULT,
103                  pool) != APR_SUCCESS)
104         errmsg("Could not create file.\n");
105     printf("Test file created.\n");
106
107     if ((rv = apr_file_lock(file, APR_FLOCK_EXCLUSIVE)) != APR_SUCCESS)
108         errmsg2("Could not lock the file", rv);
109     printf("Lock created.\nSleeping...");
110     fflush(stdout);
111
112     apr_sleep(apr_time_from_sec(30));
113
114     (void) apr_file_close(file);
115     printf(" done.\nExiting.\n");
116 }
117
118 int main(int argc, const char * const *argv)
119 {
120     int reader = 0;
121     apr_status_t status;
122     char optchar;
123     const char *optarg;
124     apr_getopt_t *opt;
125
126     if (apr_initialize() != APR_SUCCESS)
127         errmsg("Could not initialize APR.\n");
128     atexit(apr_terminate);
129
130     if (apr_pool_create(&pool, NULL) != APR_SUCCESS)
131         errmsg("Could not create global pool.\n");
132
133     if (apr_getopt_init(&opt, pool, argc, argv) != APR_SUCCESS)
134         errmsg("Could not parse options.\n");
135
136     while ((status = apr_getopt(opt, "rf:", &optchar, &optarg)) == APR_SUCCESS) {
137         if (optchar == 'r')
138             ++reader;
139         else if (optchar == 'f')
140             testfile = optarg;
141     }
142     if (status != APR_SUCCESS && status != APR_EOF) {
143         char msgbuf[80];
144
145         fprintf(stderr, "error: %s\n",
146                 apr_strerror(status, msgbuf, sizeof msgbuf));
147         exit(1);
148     }
149
150     if (reader)
151         do_read();
152     else
153         do_write();
154
155     return 0;
156 }