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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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.
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.
36 #include "apr_pools.h"
37 #include "apr_file_io.h"
39 #include "apr_general.h"
40 #include "apr_getopt.h"
41 #include "apr_strings.h"
46 const char *testfile = "testfile.tmp";
48 static apr_pool_t *pool = NULL;
50 static void errmsg(const char *msg)
53 apr_pool_destroy(pool);
58 static void errmsg2(const char *msg, apr_status_t rv)
63 apr_strerror(rv, errstr, sizeof errstr);
64 newmsg = apr_psprintf(pool, "%s: %s (%d)\n",
70 static void do_read(void)
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");
80 status = apr_file_lock(file, APR_FLOCK_EXCLUSIVE | APR_FLOCK_NONBLOCK);
81 if (!APR_STATUS_IS_EAGAIN(status)) {
83 errmsg(apr_psprintf(pool, "Expected APR_EAGAIN. Got %d: %s.\n",
84 status, apr_strerror(status, msg, sizeof(msg))));
86 printf("First attempt: we were properly locked out.\nWaiting for lock...");
89 if (apr_file_lock(file, APR_FLOCK_EXCLUSIVE) != APR_SUCCESS)
90 errmsg("Could not establish lock on test file.");
93 (void) apr_file_close(file);
97 static void do_write(void)
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");
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...");
112 apr_sleep(apr_time_from_sec(30));
114 (void) apr_file_close(file);
115 printf(" done.\nExiting.\n");
118 int main(int argc, const char * const *argv)
126 if (apr_initialize() != APR_SUCCESS)
127 errmsg("Could not initialize APR.\n");
128 atexit(apr_terminate);
130 if (apr_pool_create(&pool, NULL) != APR_SUCCESS)
131 errmsg("Could not create global pool.\n");
133 if (apr_getopt_init(&opt, pool, argc, argv) != APR_SUCCESS)
134 errmsg("Could not parse options.\n");
136 while ((status = apr_getopt(opt, "rf:", &optchar, &optarg)) == APR_SUCCESS) {
139 else if (optchar == 'f')
142 if (status != APR_SUCCESS && status != APR_EOF) {
145 fprintf(stderr, "error: %s\n",
146 apr_strerror(status, msgbuf, sizeof msgbuf));