Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

os_handle.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1998-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: os_handle.c,v 12.2 2005/08/10 15:47:25 bostic Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #ifndef NO_SYSTEM_INCLUDES
00013 #include <sys/types.h>
00014 
00015 #include <fcntl.h>
00016 #include <string.h>
00017 #endif
00018 
00019 #include "db_int.h"
00020 
00021 /*
00022  * __os_openhandle --
00023  *      Open a file, using POSIX 1003.1 open flags.
00024  *
00025  * PUBLIC: int __os_openhandle
00026  * PUBLIC:     __P((DB_ENV *, const char *, int, int, DB_FH **));
00027  */
00028 int
00029 __os_openhandle(dbenv, name, flags, mode, fhpp)
00030         DB_ENV *dbenv;
00031         const char *name;
00032         int flags, mode;
00033         DB_FH **fhpp;
00034 {
00035         DB_FH *fhp;
00036         u_int nrepeat, retries;
00037         int ret;
00038 #ifdef HAVE_VXWORKS
00039         int newflags;
00040 #endif
00041 
00042         if ((ret = __os_calloc(dbenv, 1, sizeof(DB_FH), fhpp)) != 0)
00043                 return (ret);
00044         fhp = *fhpp;
00045 
00046         /* If the application specified an interface, use it. */
00047         if (DB_GLOBAL(j_open) != NULL) {
00048                 if ((fhp->fd = DB_GLOBAL(j_open)(name, flags, mode)) == -1) {
00049                         ret = __os_get_errno();
00050                         goto err;
00051                 }
00052                 F_SET(fhp, DB_FH_OPENED);
00053                 return (0);
00054         }
00055 
00056         retries = 0;
00057         for (nrepeat = 1; nrepeat < 4; ++nrepeat) {
00058                 ret = 0;
00059 #ifdef  HAVE_VXWORKS
00060                 /*
00061                  * VxWorks does not support O_CREAT on open, you have to use
00062                  * creat() instead.  (It does not support O_EXCL or O_TRUNC
00063                  * either, even though they are defined "for future support".)
00064                  * We really want the POSIX behavior that if O_CREAT is set,
00065                  * we open if it exists, or create it if it doesn't exist.
00066                  * If O_CREAT is specified, single thread and try to open the
00067                  * file.  If successful, and O_EXCL return EEXIST.  If
00068                  * unsuccessful call creat and then end single threading.
00069                  */
00070                 if (LF_ISSET(O_CREAT)) {
00071                         DB_BEGIN_SINGLE_THREAD;
00072                         newflags = flags & ~(O_CREAT | O_EXCL);
00073                         if ((fhp->fd = open(name, newflags, mode)) != -1) {
00074                                 /*
00075                                  * We need to mark the file opened at this
00076                                  * point so that if we get any error below
00077                                  * we will properly close the fd we just
00078                                  * opened on the error path.
00079                                  */
00080                                 F_SET(fhp, DB_FH_OPENED);
00081                                 if (LF_ISSET(O_EXCL)) {
00082                                         /*
00083                                          * If we get here, want O_EXCL create,
00084                                          * and the file exists.  Close and
00085                                          * return EEXISTS.
00086                                          */
00087                                         DB_END_SINGLE_THREAD;
00088                                         ret = EEXIST;
00089                                         goto err;
00090                                 }
00091                                 /*
00092                                  * XXX
00093                                  * Assume any error means non-existence.
00094                                  * Unfortunately return values (even for
00095                                  * non-existence) are driver specific so
00096                                  * there is no single error we can use to
00097                                  * verify we truly got the equivalent of
00098                                  * ENOENT.
00099                                  */
00100                         } else
00101                                 fhp->fd = creat(name, newflags);
00102                         DB_END_SINGLE_THREAD;
00103                 } else
00104                 /* FALLTHROUGH */
00105 #endif
00106 #ifdef __VMS
00107                 /*
00108                  * !!!
00109                  * Open with full sharing on VMS.
00110                  *
00111                  * We use these flags because they are the ones set by the VMS
00112                  * CRTL mmap() call when it opens a file, and we have to be
00113                  * able to open files that mmap() has previously opened, e.g.,
00114                  * when we're joining already existing DB regions.
00115                  */
00116                 fhp->fd = open(name, flags, mode, "shr=get,put,upd,del,upi");
00117 #else
00118                 fhp->fd = open(name, flags, mode);
00119 #endif
00120                 if (fhp->fd != -1) {
00121                         F_SET(fhp, DB_FH_OPENED);
00122 
00123 #if defined(HAVE_FCNTL_F_SETFD)
00124                         /* Deny file descriptor access to any child process. */
00125                         if (fcntl(fhp->fd, F_SETFD, 1) == -1) {
00126                                 ret = __os_get_errno();
00127                                 __db_err(dbenv,
00128                                     "fcntl(F_SETFD): %s", strerror(ret));
00129                                 goto err;
00130                         }
00131 #endif
00132                         break;
00133                 }
00134 
00135                 switch (ret = __os_get_errno()) {
00136                 case EMFILE:
00137                 case ENFILE:
00138                 case ENOSPC:
00139                         /*
00140                          * If it's a "temporary" error, we retry up to 3 times,
00141                          * waiting up to 12 seconds.  While it's not a problem
00142                          * if we can't open a database, an inability to open a
00143                          * log file is cause for serious dismay.
00144                          */
00145                         __os_sleep(dbenv, nrepeat * 2, 0);
00146                         break;
00147                 case EAGAIN:
00148                 case EBUSY:
00149                 case EINTR:
00150                         /*
00151                          * If an EAGAIN, EBUSY or EINTR, retry immediately for
00152                          * DB_RETRY times.
00153                          */
00154                         if (++retries < DB_RETRY)
00155                                 --nrepeat;
00156                         break;
00157                 default:
00158                         break;
00159                 }
00160         }
00161 
00162 err:    if (ret != 0) {
00163                 (void)__os_closehandle(dbenv, fhp);
00164                 *fhpp = NULL;
00165         }
00166 
00167         return (ret);
00168 }
00169 
00170 /*
00171  * __os_closehandle --
00172  *      Close a file.
00173  *
00174  * PUBLIC: int __os_closehandle __P((DB_ENV *, DB_FH *));
00175  */
00176 int
00177 __os_closehandle(dbenv, fhp)
00178         DB_ENV *dbenv;
00179         DB_FH *fhp;
00180 {
00181         int ret;
00182 
00183         ret = 0;
00184 
00185         /*
00186          * If we have a valid handle, close it and unlink any temporary
00187          * file.
00188          */
00189         if (F_ISSET(fhp, DB_FH_OPENED)) {
00190                 if (DB_GLOBAL(j_close) != NULL)
00191                         ret = DB_GLOBAL(j_close)(fhp->fd);
00192                 else
00193                         RETRY_CHK((close(fhp->fd)), ret);
00194 
00195                 if (ret != 0)
00196                         __db_err(dbenv, "close: %s", strerror(ret));
00197 
00198                 /* Unlink the file if we haven't already done so. */
00199                 if (F_ISSET(fhp, DB_FH_UNLINK)) {
00200                         (void)__os_unlink(dbenv, fhp->name);
00201                         __os_free(dbenv, fhp->name);
00202                 }
00203         }
00204 
00205         __os_free(dbenv, fhp);
00206 
00207         return (ret);
00208 }

Generated on Sun Dec 25 12:14:42 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2