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#include "apr_arch_file_io.h" 18 19#if APR_HAVE_FCNTL_H 20#include <fcntl.h> 21#endif 22#ifdef HAVE_SYS_FILE_H 23#include <sys/file.h> 24#endif 25 26APR_DECLARE(apr_status_t) apr_file_lock(apr_file_t *thefile, int type) 27{ 28 int rc; 29 30#if defined(HAVE_FCNTL_H) 31 { 32 struct flock l = { 0 }; 33 int fc; 34 35 l.l_whence = SEEK_SET; /* count l_start from start of file */ 36 l.l_start = 0; /* lock from start of file */ 37 l.l_len = 0; /* lock to end of file */ 38 if ((type & APR_FLOCK_TYPEMASK) == APR_FLOCK_SHARED) 39 l.l_type = F_RDLCK; 40 else 41 l.l_type = F_WRLCK; 42 43 fc = (type & APR_FLOCK_NONBLOCK) ? F_SETLK : F_SETLKW; 44 45 /* keep trying if fcntl() gets interrupted (by a signal) */ 46 while ((rc = fcntl(thefile->filedes, fc, &l)) < 0 && errno == EINTR) 47 continue; 48 49 if (rc == -1) { 50 /* on some Unix boxes (e.g., Tru64), we get EACCES instead 51 * of EAGAIN; we don't want APR_STATUS_IS_EAGAIN() matching EACCES 52 * since that breaks other things, so fix up the retcode here 53 */ 54 if (errno == EACCES) { 55 return EAGAIN; 56 } 57 return errno; 58 } 59 } 60#elif defined(HAVE_SYS_FILE_H) 61 { 62 int ltype; 63 64 if ((type & APR_FLOCK_TYPEMASK) == APR_FLOCK_SHARED) 65 ltype = LOCK_SH; 66 else 67 ltype = LOCK_EX; 68 if ((type & APR_FLOCK_NONBLOCK) != 0) 69 ltype |= LOCK_NB; 70 71 /* keep trying if flock() gets interrupted (by a signal) */ 72 while ((rc = flock(thefile->filedes, ltype)) < 0 && errno == EINTR) 73 continue; 74 75 if (rc == -1) 76 return errno; 77 } 78#else 79#error No file locking mechanism is available. 80#endif 81 82 return APR_SUCCESS; 83} 84 85APR_DECLARE(apr_status_t) apr_file_unlock(apr_file_t *thefile) 86{ 87 int rc; 88 89#if defined(HAVE_FCNTL_H) 90 { 91 struct flock l = { 0 }; 92 93 l.l_whence = SEEK_SET; /* count l_start from start of file */ 94 l.l_start = 0; /* lock from start of file */ 95 l.l_len = 0; /* lock to end of file */ 96 l.l_type = F_UNLCK; 97 98 /* keep trying if fcntl() gets interrupted (by a signal) */ 99 while ((rc = fcntl(thefile->filedes, F_SETLKW, &l)) < 0 100 && errno == EINTR) 101 continue; 102 103 if (rc == -1) 104 return errno; 105 } 106#elif defined(HAVE_SYS_FILE_H) 107 { 108 /* keep trying if flock() gets interrupted (by a signal) */ 109 while ((rc = flock(thefile->filedes, LOCK_UN)) < 0 && errno == EINTR) 110 continue; 111 112 if (rc == -1) 113 return errno; 114 } 115#else 116#error No file locking mechanism is available. 117#endif 118 119 return APR_SUCCESS; 120} 121