diff options
-rw-r--r-- | CHANGES | 5 | ||||
-rw-r--r-- | file_io/unix/mktemp.c | 102 | ||||
-rw-r--r-- | include/apr_file_io.h | 10 | ||||
-rw-r--r-- | include/arch/unix/fileio.h | 2 | ||||
-rw-r--r-- | include/arch/unix/locks.h | 2 | ||||
-rw-r--r-- | include/arch/unix/proc_mutex.h | 4 | ||||
-rw-r--r-- | locks/unix/crossproc.c | 51 | ||||
-rw-r--r-- | locks/unix/locks.c | 6 | ||||
-rw-r--r-- | locks/unix/proc_mutex.c | 65 |
9 files changed, 123 insertions, 124 deletions
@@ -1,5 +1,10 @@ Changes with APR b1 + *) Add the apr_file_mktemp function. This creates and opens a + temporary file, for use by the program. This file is created + delete_on_close. The initial implementation only works on + Unix, but Windows is coming soon. [Ryan Bloom] + *) Make the unix version of apr_proc_wait_all_procs a simple wrapper around apr_proc_wait, and which extracts the exit code from the status returned by waitpid. diff --git a/file_io/unix/mktemp.c b/file_io/unix/mktemp.c index 0376afc64..b199e73f2 100644 --- a/file_io/unix/mktemp.c +++ b/file_io/unix/mktemp.c @@ -31,6 +31,9 @@ * SUCH DAMAGE. */ +#include "apr_private.h" +#include "apr_file_io.h" /* prototype of apr_mkstemp() */ +#include "apr_strings.h" /* prototype of apr_mkstemp() */ #include "fileio.h" /* prototype of apr_mkstemp() */ #ifndef HAVE_MKSTEMP @@ -49,13 +52,6 @@ #endif #define _open(a,b,c) open(a,b,c) -#if defined(LIBC_SCCS) && !defined(lint) -#if 0 -static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93"; -#endif -static const char rcsid[] = - "$FreeBSD: src/lib/libc/stdio/mktemp.c,v 1.19.2.1 2001/01/20 09:35:24 kris Exp $"; -#endif /* LIBC_SCCS and not lint */ #include <sys/types.h> #include <sys/stat.h> @@ -66,70 +62,11 @@ static const char rcsid[] = #include <string.h> #include <ctype.h> -#ifdef SVR4 -/* arrange to compile it on my machine */ -char *_mktemp(char *); -#else -char *_mktemp __P((char *)); -static int _gettemp (char *, int *, int, int); -#endif - - static const unsigned char padchar[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; static uint32_t randseed=0; -#ifdef APR_STARTS_USING_IT -int -mkstemps(path, slen) - char *path; - int slen; -{ - int fd; - - return (_gettemp(path, &fd, 0, slen) ? fd : -1); -} -#endif /* APR_STARTS_USING_IT */ - -int apr_mkstemp(char *path) -{ - int fd; - - return (_gettemp(path, &fd, 0, 0) ? fd : -1); -} - -#ifdef APR_STARTS_USING_IT -char * -mkdtemp(path) - char *path; -{ - return(_gettemp(path, (int *)NULL, 1, 0) ? path : (char *)NULL); -} - -char * -_mktemp(path) - char *path; -{ - return(_gettemp(path, (int *)NULL, 0, 0) ? path : (char *)NULL); -} - -__warn_references(mktemp, - "warning: mktemp() possibly used unsafely; consider using mkstemp()"); - -char * -mktemp(path) - char *path; -{ - return(_mktemp(path)); -} -#endif /* APR_STARTS_USING_IT */ - -static int -_gettemp(path, doopen, domkdir, slen) - char *path; - register int *doopen; - int domkdir; - int slen; +static int gettemp(char *path, register int *doopen, int domkdir, int slen) { register char *start, *trv, *suffp; char *pad; @@ -226,9 +163,36 @@ _gettemp(path, doopen, domkdir, slen) #include <unistd.h> /* for mkstemp() - FreeBSD */ #endif -int apr_mkstemp(char *template) +apr_status_t apr_file_mktemp(apr_file_t **fp, char *template, apr_pool_t *p) { - return mkstemp(template); + int fd; +#ifndef HAVE_MKSTEMP + int rv; +#endif + + (*fp) = apr_pcalloc(p, sizeof(**fp)); + (*fp)->cntxt = p; + (*fp)->timeout = -1; + (*fp)->blocking = BLK_ON; + (*fp)->flags = APR_READ | APR_WRITE | APR_EXCL | APR_DELONCLOSE; + +#ifndef HAVE_MKSTEMP + rv = gettemp(path, &fd, 0, 0); + if (rv == 0) { + return errno; + } +#else + fd = mkstemp(template); + if (fd == -1) { + return errno; + } +#endif + (*fp)->fname = apr_pstrdup(p, template); + (*fp)->filedes = fd; + unlink((*fp)->fname); + apr_pool_cleanup_register((*fp)->cntxt, (void *)(*fp), + apr_unix_file_cleanup, apr_unix_file_cleanup); + return APR_SUCCESS; } #endif /* !defined(HAVE_MKSTEMP) */ diff --git a/include/apr_file_io.h b/include/apr_file_io.h index d15f47fef..de1b8c2db 100644 --- a/include/apr_file_io.h +++ b/include/apr_file_io.h @@ -575,6 +575,16 @@ APR_DECLARE(void) apr_file_set_inherit(apr_file_t *file); */ APR_DECLARE(void) apr_file_unset_inherit(apr_file_t *file); +/** + * Open a temporary file + * @param fp The apr file to use as a temporary file. + * @param template The template to use when creating a temp file. + * @param p The pool to allocate the file out of. + * @ingroup apr_file_open + */ +APR_DECLARE(apr_status_t) apr_file_mktemp(apr_file_t **fp, char *tmplt, + apr_pool_t *p); + #ifdef __cplusplus } #endif diff --git a/include/arch/unix/fileio.h b/include/arch/unix/fileio.h index cd3dd0848..75daacaf5 100644 --- a/include/arch/unix/fileio.h +++ b/include/arch/unix/fileio.h @@ -153,7 +153,5 @@ apr_status_t apr_unix_file_cleanup(void *); mode_t apr_unix_perms2mode(apr_fileperms_t perms); apr_fileperms_t apr_unix_mode2perms(mode_t mode); -int apr_mkstemp(char *template); - #endif /* ! FILE_IO_H */ diff --git a/include/arch/unix/locks.h b/include/arch/unix/locks.h index c83e8d09c..47f089c14 100644 --- a/include/arch/unix/locks.h +++ b/include/arch/unix/locks.h @@ -139,7 +139,7 @@ struct apr_lock_t { int curr_locked; char *fname; #if APR_HAS_SYSVSEM_SERIALIZE || APR_HAS_FCNTL_SERIALIZE || APR_HAS_FLOCK_SERIALIZE - int interproc; + apr_file_t *interproc; #endif #if APR_HAS_PROC_PTHREAD_SERIALIZE pthread_mutex_t *pthread_interproc; diff --git a/include/arch/unix/proc_mutex.h b/include/arch/unix/proc_mutex.h index cff88145a..b401cd9df 100644 --- a/include/arch/unix/proc_mutex.h +++ b/include/arch/unix/proc_mutex.h @@ -62,6 +62,8 @@ #include "apr_proc_mutex.h" #include "apr_pools.h" #include "apr_portable.h" +#include "apr_file_io.h" +#include "fileio.h" /* System headers required by Locks library */ #if APR_HAVE_SYS_TYPES_H @@ -145,7 +147,7 @@ struct apr_proc_mutex_t { int curr_locked; char *fname; #if APR_HAS_SYSVSEM_SERIALIZE || APR_HAS_FCNTL_SERIALIZE || APR_HAS_FLOCK_SERIALIZE - int interproc; + apr_file_t *interproc; #endif #if APR_HAS_PROC_PTHREAD_SERIALIZE pthread_mutex_t *pthread_interproc; diff --git a/locks/unix/crossproc.c b/locks/unix/crossproc.c index f555b2a21..2ed15f3ad 100644 --- a/locks/unix/crossproc.c +++ b/locks/unix/crossproc.c @@ -53,6 +53,7 @@ */ #include "apr.h" +#include "apr_file_io.h" #include "apr_strings.h" #include "locks.h" #include "fileio.h" /* for apr_mkstemp() */ @@ -77,9 +78,9 @@ static apr_status_t sysv_cleanup(void *lock_) apr_lock_t *lock=lock_; union semun ick; - if (lock->interproc != -1) { + if (lock->interproc->filedes != -1) { ick.val = 0; - semctl(lock->interproc, 0, IPC_RMID, ick); + semctl(lock->interproc->filedes, 0, IPC_RMID, ick); } return APR_SUCCESS; } @@ -89,15 +90,16 @@ static apr_status_t sysv_create(apr_lock_t *new, const char *fname) union semun ick; apr_status_t stat; - new->interproc = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600); + new->interproc = apr_palloc(new->pool, sizeof(*new->interproc)); + new->interproc->filedes = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600); - if (new->interproc < 0) { + if (new->interproc->filedes < 0) { stat = errno; sysv_cleanup(new); return stat; } ick.val = 1; - if (semctl(new->interproc, 0, SETVAL, ick) < 0) { + if (semctl(new->interproc->filedes, 0, SETVAL, ick) < 0) { stat = errno; sysv_cleanup(new); return stat; @@ -113,7 +115,7 @@ static apr_status_t sysv_acquire(apr_lock_t *lock) int rc; do { - rc = semop(lock->interproc, &op_on, 1); + rc = semop(lock->interproc->filedes, &op_on, 1); } while (rc < 0 && errno == EINTR); if (rc < 0) { return errno; @@ -127,7 +129,7 @@ static apr_status_t sysv_release(apr_lock_t *lock) int rc; do { - rc = semop(lock->interproc, &op_off, 1); + rc = semop(lock->interproc->filedes, &op_off, 1); } while (rc < 0 && errno == EINTR); if (rc < 0) { return errno; @@ -369,23 +371,25 @@ static apr_status_t fcntl_cleanup(void *lock_) if (status != APR_SUCCESS) return status; } - close(lock->interproc); return APR_SUCCESS; } static apr_status_t fcntl_create(apr_lock_t *new, const char *fname) { + int rv; + if (fname) { new->fname = apr_pstrdup(new->pool, fname); - new->interproc = open(new->fname, O_CREAT | O_WRONLY | O_EXCL, 0644); + rv = apr_file_open(&new->interproc, new->fname, + APR_CREATE | APR_WRITE | APR_EXCL, 0644, new->pool); } else { new->fname = apr_pstrdup(new->pool, "/tmp/aprXXXXXX"); - new->interproc = apr_mkstemp(new->fname); + rv = apr_file_mktemp(&new->interproc, new->fname, new->pool); } - if (new->interproc < 0) { + if (rv != APR_SUCCESS) { apr_status_t stat = errno; fcntl_cleanup(new); @@ -404,7 +408,7 @@ static apr_status_t fcntl_acquire(apr_lock_t *lock) int rc; do { - rc = fcntl(lock->interproc, F_SETLKW, &lock_it); + rc = fcntl(lock->interproc->filedes, F_SETLKW, &lock_it); } while (rc < 0 && errno == EINTR); if (rc < 0) { return errno; @@ -418,7 +422,7 @@ static apr_status_t fcntl_release(apr_lock_t *lock) int rc; do { - rc = fcntl(lock->interproc, F_SETLKW, &unlock_it); + rc = fcntl(lock->interproc->filedes, F_SETLKW, &unlock_it); } while (rc < 0 && errno == EINTR); if (rc < 0) { return errno; @@ -480,23 +484,26 @@ static apr_status_t flock_cleanup(void *lock_) if (status != APR_SUCCESS) return status; } - close(lock->interproc); + apr_file_close(lock->interproc); unlink(lock->fname); return APR_SUCCESS; } static apr_status_t flock_create(apr_lock_t *new, const char *fname) { + int rv; + if (fname) { new->fname = apr_pstrdup(new->pool, fname); - new->interproc = open(new->fname, O_CREAT | O_WRONLY | O_EXCL, 0600); + rv = apr_file_open(&new->interproc, new->fname, + APR_CREATE | APR_WRITE | APR_EXCL, 0644, new->pool); } else { new->fname = apr_pstrdup(new->pool, "/tmp/aprXXXXXX"); - new->interproc = apr_mkstemp(new->fname); + rv = apr_file_mktemp(&new->interproc, new->fname, new->pool); } - if (new->interproc < 0) { + if (rv != APR_SUCCESS) { apr_status_t stat = errno; flock_cleanup(new); @@ -513,7 +520,7 @@ static apr_status_t flock_acquire(apr_lock_t *lock) int rc; do { - rc = flock(lock->interproc, LOCK_EX); + rc = flock(lock->interproc->filedes, LOCK_EX); } while (rc < 0 && errno == EINTR); if (rc < 0) { return errno; @@ -527,7 +534,7 @@ static apr_status_t flock_release(apr_lock_t *lock) int rc; do { - rc = flock(lock->interproc, LOCK_UN); + rc = flock(lock->interproc->filedes, LOCK_UN); } while (rc < 0 && errno == EINTR); if (rc < 0) { return errno; @@ -550,14 +557,16 @@ static apr_status_t flock_child_init(apr_lock_t **lock, apr_pool_t *cont, const char *fname) { apr_lock_t *new; + int rv; new = (apr_lock_t *)apr_palloc(cont, sizeof(apr_lock_t)); memcpy(new, *lock, sizeof *new); new->pool = cont; new->fname = apr_pstrdup(cont, fname); - new->interproc = open(new->fname, O_WRONLY, 0600); - if (new->interproc == -1) { + rv = apr_file_open(&new->interproc, new->fname, + APR_CREATE | APR_WRITE, 0600, new->pool); + if (rv != APR_SUCCESS) { apr_status_t stat = errno; flock_destroy(new); diff --git a/locks/unix/locks.c b/locks/unix/locks.c index fe96abea6..7e5d3b69e 100644 --- a/locks/unix/locks.c +++ b/locks/unix/locks.c @@ -243,7 +243,7 @@ APR_DECLARE(apr_status_t) apr_lock_create_np(apr_lock_t **lock, apr_locktype_e t new->type = type; new->scope = scope; #if APR_HAS_SYSVSEM_SERIALIZE || APR_HAS_FCNTL_SERIALIZE || APR_HAS_FLOCK_SERIALIZE - new->interproc = -1; + new->interproc = NULL; #endif if ((stat = create_lock(new, mech, fname)) != APR_SUCCESS) @@ -370,7 +370,7 @@ APR_DECLARE(apr_status_t) apr_lock_data_set(apr_lock_t *lock, void *data, const APR_DECLARE(apr_status_t) apr_os_lock_get(apr_os_lock_t *oslock, apr_lock_t *lock) { #if APR_HAS_SYSVSEM_SERIALIZE || APR_HAS_FCNTL_SERIALIZE || APR_HAS_FLOCK_SERIALIZE - oslock->crossproc = lock->interproc; + oslock->crossproc = lock->interproc->filedes; #endif #if APR_HAS_PROC_PTHREAD_SERIALIZE oslock->pthread_interproc = lock->pthread_interproc; @@ -395,7 +395,7 @@ APR_DECLARE(apr_status_t) apr_os_lock_put(apr_lock_t **lock, apr_os_lock_t *thel (*lock)->pool = pool; } #if APR_HAS_SYSVSEM_SERIALIZE || APR_HAS_FCNTL_SERIALIZE || APR_HAS_FLOCK_SERIALIZE - (*lock)->interproc = thelock->crossproc; + apr_os_file_put(&(*lock)->interproc, &thelock->crossproc, pool); #endif #if APR_HAS_PROC_PTHREAD_SERIALIZE (*lock)->pthread_interproc = thelock->pthread_interproc; diff --git a/locks/unix/proc_mutex.c b/locks/unix/proc_mutex.c index 037b5a160..125a9b26f 100644 --- a/locks/unix/proc_mutex.c +++ b/locks/unix/proc_mutex.c @@ -77,9 +77,9 @@ static apr_status_t proc_mutex_sysv_cleanup(void *mutex_) apr_proc_mutex_t *mutex=mutex_; union semun ick; - if (mutex->interproc != -1) { + if (mutex->interproc->filedes != -1) { ick.val = 0; - semctl(mutex->interproc, 0, IPC_RMID, ick); + semctl(mutex->interproc->filedes, 0, IPC_RMID, ick); } return APR_SUCCESS; } @@ -90,15 +90,16 @@ static apr_status_t proc_mutex_sysv_create(apr_proc_mutex_t *new_mutex, union semun ick; apr_status_t stat; - new_mutex->interproc = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600); + new_mutex->interproc = apr_palloc(new_mutex->pool, sizeof(*new_mutex->interproc)); + new_mutex->interproc->filedes = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600); - if (new_mutex->interproc < 0) { + if (new_mutex->interproc->filedes < 0) { stat = errno; proc_mutex_sysv_cleanup(new_mutex); return stat; } ick.val = 1; - if (semctl(new_mutex->interproc, 0, SETVAL, ick) < 0) { + if (semctl(new_mutex->interproc->filedes, 0, SETVAL, ick) < 0) { stat = errno; proc_mutex_sysv_cleanup(new_mutex); return stat; @@ -115,7 +116,7 @@ static apr_status_t proc_mutex_sysv_acquire(apr_proc_mutex_t *mutex) int rc; do { - rc = semop(mutex->interproc, &proc_mutex_op_on, 1); + rc = semop(mutex->interproc->filedes, &proc_mutex_op_on, 1); } while (rc < 0 && errno == EINTR); if (rc < 0) { return errno; @@ -129,7 +130,7 @@ static apr_status_t proc_mutex_sysv_release(apr_proc_mutex_t *mutex) int rc; do { - rc = semop(mutex->interproc, &proc_mutex_op_off, 1); + rc = semop(mutex->interproc->filedes, &proc_mutex_op_off, 1); } while (rc < 0 && errno == EINTR); if (rc < 0) { return errno; @@ -376,7 +377,7 @@ static apr_status_t proc_mutex_fcntl_cleanup(void *mutex_) if (status != APR_SUCCESS) return status; } - close(mutex->interproc); + apr_file_close(mutex->interproc); return APR_SUCCESS; } @@ -384,23 +385,27 @@ static apr_status_t proc_mutex_fcntl_cleanup(void *mutex_) static apr_status_t proc_mutex_fcntl_create(apr_proc_mutex_t *new_mutex, const char *fname) { + int rv; + if (fname) { new_mutex->fname = apr_pstrdup(new_mutex->pool, fname); - new_mutex->interproc = open(new_mutex->fname, - O_CREAT | O_WRONLY | O_EXCL, 0644); + rv = apr_file_open(&new_mutex->interproc, new_mutex->fname, + APR_CREATE | APR_WRITE | APR_EXCL, 0644, + new_mutex->pool); } else { new_mutex->fname = apr_pstrdup(new_mutex->pool, "/tmp/aprXXXXXX"); - new_mutex->interproc = apr_mkstemp(new_mutex->fname); + rv = apr_file_mktemp(&new_mutex->interproc, new_mutex->fname, + new_mutex->pool); } - - if (new_mutex->interproc < 0) { + + if (rv != APR_SUCCESS) { proc_mutex_fcntl_cleanup(new_mutex); return errno; } new_mutex->curr_locked = 0; - unlink(new_mutex->fname); +/* unlink(new_mutex->fname); */ apr_pool_cleanup_register(new_mutex->pool, (void*)new_mutex, proc_mutex_fcntl_cleanup, @@ -413,7 +418,7 @@ static apr_status_t proc_mutex_fcntl_acquire(apr_proc_mutex_t *mutex) int rc; do { - rc = fcntl(mutex->interproc, F_SETLKW, &proc_mutex_lock_it); + rc = fcntl(mutex->interproc->filedes, F_SETLKW, &proc_mutex_lock_it); } while (rc < 0 && errno == EINTR); if (rc < 0) { return errno; @@ -427,7 +432,7 @@ static apr_status_t proc_mutex_fcntl_release(apr_proc_mutex_t *mutex) int rc; do { - rc = fcntl(mutex->interproc, F_SETLKW, &proc_mutex_unlock_it); + rc = fcntl(mutex->interproc->filedes, F_SETLKW, &proc_mutex_unlock_it); } while (rc < 0 && errno == EINTR); if (rc < 0) { return errno; @@ -488,7 +493,7 @@ static apr_status_t proc_mutex_flock_cleanup(void *mutex_) if (status != APR_SUCCESS) return status; } - close(mutex->interproc); + apr_file_close(mutex->interproc); unlink(mutex->fname); return APR_SUCCESS; } @@ -496,17 +501,21 @@ static apr_status_t proc_mutex_flock_cleanup(void *mutex_) static apr_status_t proc_mutex_flock_create(apr_proc_mutex_t *new_mutex, const char *fname) { + int rv; + if (fname) { new_mutex->fname = apr_pstrdup(new_mutex->pool, fname); - new_mutex->interproc = open(new_mutex->fname, - O_CREAT | O_WRONLY | O_EXCL, 0600); + rv = apr_file_open(&new_mutex->interproc, new_mutex->fname, + APR_CREATE | APR_WRITE | APR_EXCL, 0644, + new_mutex->pool); } else { new_mutex->fname = apr_pstrdup(new_mutex->pool, "/tmp/aprXXXXXX"); - new_mutex->interproc = apr_mkstemp(new_mutex->fname); + rv = apr_file_mktemp(&new_mutex->interproc, new_mutex->fname, + new_mutex->pool); } - - if (new_mutex->interproc < 0) { + + if (rv != APR_SUCCESS) { proc_mutex_flock_cleanup(new_mutex); return errno; } @@ -522,7 +531,7 @@ static apr_status_t proc_mutex_flock_acquire(apr_proc_mutex_t *mutex) int rc; do { - rc = flock(mutex->interproc, LOCK_EX); + rc = flock(mutex->interproc->filedes, LOCK_EX); } while (rc < 0 && errno == EINTR); if (rc < 0) { return errno; @@ -536,7 +545,7 @@ static apr_status_t proc_mutex_flock_release(apr_proc_mutex_t *mutex) int rc; do { - rc = flock(mutex->interproc, LOCK_UN); + rc = flock(mutex->interproc->filedes, LOCK_UN); } while (rc < 0 && errno == EINTR); if (rc < 0) { return errno; @@ -560,14 +569,16 @@ static apr_status_t proc_mutex_flock_child_init(apr_proc_mutex_t **mutex, const char *fname) { apr_proc_mutex_t *new_mutex; + int rv; new_mutex = (apr_proc_mutex_t *)apr_palloc(pool, sizeof(apr_proc_mutex_t)); memcpy(new_mutex, *mutex, sizeof *new_mutex); new_mutex->pool = pool; new_mutex->fname = apr_pstrdup(pool, fname); - new_mutex->interproc = open(new_mutex->fname, O_WRONLY, 0600); - if (new_mutex->interproc == -1) { + rv = apr_file_open(&new_mutex->interproc, new_mutex->fname, + APR_CREATE | APR_WRITE, 0600, new_mutex->pool); + if (rv != APR_SUCCESS) { proc_mutex_flock_destroy(new_mutex); return errno; } @@ -705,7 +716,7 @@ APR_DECLARE(apr_status_t) apr_proc_mutex_create_np(apr_proc_mutex_t **mutex, new_mutex->pool = pool; #if APR_HAS_SYSVSEM_SERIALIZE || APR_HAS_FCNTL_SERIALIZE || APR_HAS_FLOCK_SERIALIZE - new_mutex->interproc = -1; + new_mutex->interproc = NULL; #endif if ((stat = proc_mutex_create(new_mutex, mech, fname)) != APR_SUCCESS) |