Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 #include <ngx_event.h>
0011 
0012 
0013 /*
0014  * open file cache caches
0015  *    open file handles with stat() info;
0016  *    directories stat() info;
0017  *    files and directories errors: not found, access denied, etc.
0018  */
0019 
0020 
0021 #define NGX_MIN_READ_AHEAD  (128 * 1024)
0022 
0023 
0024 static void ngx_open_file_cache_cleanup(void *data);
0025 #if (NGX_HAVE_OPENAT)
0026 static ngx_fd_t ngx_openat_file_owner(ngx_fd_t at_fd, const u_char *name,
0027     ngx_int_t mode, ngx_int_t create, ngx_int_t access, ngx_log_t *log);
0028 #if (NGX_HAVE_O_PATH)
0029 static ngx_int_t ngx_file_o_path_info(ngx_fd_t fd, ngx_file_info_t *fi,
0030     ngx_log_t *log);
0031 #endif
0032 #endif
0033 static ngx_fd_t ngx_open_file_wrapper(ngx_str_t *name,
0034     ngx_open_file_info_t *of, ngx_int_t mode, ngx_int_t create,
0035     ngx_int_t access, ngx_log_t *log);
0036 static ngx_int_t ngx_file_info_wrapper(ngx_str_t *name,
0037     ngx_open_file_info_t *of, ngx_file_info_t *fi, ngx_log_t *log);
0038 static ngx_int_t ngx_open_and_stat_file(ngx_str_t *name,
0039     ngx_open_file_info_t *of, ngx_log_t *log);
0040 static void ngx_open_file_add_event(ngx_open_file_cache_t *cache,
0041     ngx_cached_open_file_t *file, ngx_open_file_info_t *of, ngx_log_t *log);
0042 static void ngx_open_file_cleanup(void *data);
0043 static void ngx_close_cached_file(ngx_open_file_cache_t *cache,
0044     ngx_cached_open_file_t *file, ngx_uint_t min_uses, ngx_log_t *log);
0045 static void ngx_open_file_del_event(ngx_cached_open_file_t *file);
0046 static void ngx_expire_old_cached_files(ngx_open_file_cache_t *cache,
0047     ngx_uint_t n, ngx_log_t *log);
0048 static void ngx_open_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp,
0049     ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
0050 static ngx_cached_open_file_t *
0051     ngx_open_file_lookup(ngx_open_file_cache_t *cache, ngx_str_t *name,
0052     uint32_t hash);
0053 static void ngx_open_file_cache_remove(ngx_event_t *ev);
0054 
0055 
0056 ngx_open_file_cache_t *
0057 ngx_open_file_cache_init(ngx_pool_t *pool, ngx_uint_t max, time_t inactive)
0058 {
0059     ngx_pool_cleanup_t     *cln;
0060     ngx_open_file_cache_t  *cache;
0061 
0062     cache = ngx_palloc(pool, sizeof(ngx_open_file_cache_t));
0063     if (cache == NULL) {
0064         return NULL;
0065     }
0066 
0067     ngx_rbtree_init(&cache->rbtree, &cache->sentinel,
0068                     ngx_open_file_cache_rbtree_insert_value);
0069 
0070     ngx_queue_init(&cache->expire_queue);
0071 
0072     cache->current = 0;
0073     cache->max = max;
0074     cache->inactive = inactive;
0075 
0076     cln = ngx_pool_cleanup_add(pool, 0);
0077     if (cln == NULL) {
0078         return NULL;
0079     }
0080 
0081     cln->handler = ngx_open_file_cache_cleanup;
0082     cln->data = cache;
0083 
0084     return cache;
0085 }
0086 
0087 
0088 static void
0089 ngx_open_file_cache_cleanup(void *data)
0090 {
0091     ngx_open_file_cache_t  *cache = data;
0092 
0093     ngx_queue_t             *q;
0094     ngx_cached_open_file_t  *file;
0095 
0096     ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
0097                    "open file cache cleanup");
0098 
0099     for ( ;; ) {
0100 
0101         if (ngx_queue_empty(&cache->expire_queue)) {
0102             break;
0103         }
0104 
0105         q = ngx_queue_last(&cache->expire_queue);
0106 
0107         file = ngx_queue_data(q, ngx_cached_open_file_t, queue);
0108 
0109         ngx_queue_remove(q);
0110 
0111         ngx_rbtree_delete(&cache->rbtree, &file->node);
0112 
0113         cache->current--;
0114 
0115         ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
0116                        "delete cached open file: %s", file->name);
0117 
0118         if (!file->err && !file->is_dir) {
0119             file->close = 1;
0120             file->count = 0;
0121             ngx_close_cached_file(cache, file, 0, ngx_cycle->log);
0122 
0123         } else {
0124             ngx_free(file->name);
0125             ngx_free(file);
0126         }
0127     }
0128 
0129     if (cache->current) {
0130         ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
0131                       "%ui items still left in open file cache",
0132                       cache->current);
0133     }
0134 
0135     if (cache->rbtree.root != cache->rbtree.sentinel) {
0136         ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
0137                       "rbtree still is not empty in open file cache");
0138 
0139     }
0140 }
0141 
0142 
0143 ngx_int_t
0144 ngx_open_cached_file(ngx_open_file_cache_t *cache, ngx_str_t *name,
0145     ngx_open_file_info_t *of, ngx_pool_t *pool)
0146 {
0147     time_t                          now;
0148     uint32_t                        hash;
0149     ngx_int_t                       rc;
0150     ngx_file_info_t                 fi;
0151     ngx_pool_cleanup_t             *cln;
0152     ngx_cached_open_file_t         *file;
0153     ngx_pool_cleanup_file_t        *clnf;
0154     ngx_open_file_cache_cleanup_t  *ofcln;
0155 
0156     of->fd = NGX_INVALID_FILE;
0157     of->err = 0;
0158 
0159     if (cache == NULL) {
0160 
0161         if (of->test_only) {
0162 
0163             if (ngx_file_info_wrapper(name, of, &fi, pool->log)
0164                 == NGX_FILE_ERROR)
0165             {
0166                 return NGX_ERROR;
0167             }
0168 
0169             of->uniq = ngx_file_uniq(&fi);
0170             of->mtime = ngx_file_mtime(&fi);
0171             of->size = ngx_file_size(&fi);
0172             of->fs_size = ngx_file_fs_size(&fi);
0173             of->is_dir = ngx_is_dir(&fi);
0174             of->is_file = ngx_is_file(&fi);
0175             of->is_link = ngx_is_link(&fi);
0176             of->is_exec = ngx_is_exec(&fi);
0177 
0178             return NGX_OK;
0179         }
0180 
0181         cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_cleanup_file_t));
0182         if (cln == NULL) {
0183             return NGX_ERROR;
0184         }
0185 
0186         rc = ngx_open_and_stat_file(name, of, pool->log);
0187 
0188         if (rc == NGX_OK && !of->is_dir) {
0189             cln->handler = ngx_pool_cleanup_file;
0190             clnf = cln->data;
0191 
0192             clnf->fd = of->fd;
0193             clnf->name = name->data;
0194             clnf->log = pool->log;
0195         }
0196 
0197         return rc;
0198     }
0199 
0200     cln = ngx_pool_cleanup_add(pool, sizeof(ngx_open_file_cache_cleanup_t));
0201     if (cln == NULL) {
0202         return NGX_ERROR;
0203     }
0204 
0205     now = ngx_time();
0206 
0207     hash = ngx_crc32_long(name->data, name->len);
0208 
0209     file = ngx_open_file_lookup(cache, name, hash);
0210 
0211     if (file) {
0212 
0213         file->uses++;
0214 
0215         ngx_queue_remove(&file->queue);
0216 
0217         if (file->fd == NGX_INVALID_FILE && file->err == 0 && !file->is_dir) {
0218 
0219             /* file was not used often enough to keep open */
0220 
0221             rc = ngx_open_and_stat_file(name, of, pool->log);
0222 
0223             if (rc != NGX_OK && (of->err == 0 || !of->errors)) {
0224                 goto failed;
0225             }
0226 
0227             goto add_event;
0228         }
0229 
0230         if (file->use_event
0231             || (file->event == NULL
0232                 && (of->uniq == 0 || of->uniq == file->uniq)
0233                 && now - file->created < of->valid
0234 #if (NGX_HAVE_OPENAT)
0235                 && of->disable_symlinks == file->disable_symlinks
0236                 && of->disable_symlinks_from == file->disable_symlinks_from
0237 #endif
0238             ))
0239         {
0240             if (file->err == 0) {
0241 
0242                 of->fd = file->fd;
0243                 of->uniq = file->uniq;
0244                 of->mtime = file->mtime;
0245                 of->size = file->size;
0246 
0247                 of->is_dir = file->is_dir;
0248                 of->is_file = file->is_file;
0249                 of->is_link = file->is_link;
0250                 of->is_exec = file->is_exec;
0251                 of->is_directio = file->is_directio;
0252 
0253                 if (!file->is_dir) {
0254                     file->count++;
0255                     ngx_open_file_add_event(cache, file, of, pool->log);
0256                 }
0257 
0258             } else {
0259                 of->err = file->err;
0260 #if (NGX_HAVE_OPENAT)
0261                 of->failed = file->disable_symlinks ? ngx_openat_file_n
0262                                                     : ngx_open_file_n;
0263 #else
0264                 of->failed = ngx_open_file_n;
0265 #endif
0266             }
0267 
0268             goto found;
0269         }
0270 
0271         ngx_log_debug4(NGX_LOG_DEBUG_CORE, pool->log, 0,
0272                        "retest open file: %s, fd:%d, c:%d, e:%d",
0273                        file->name, file->fd, file->count, file->err);
0274 
0275         if (file->is_dir) {
0276 
0277             /*
0278              * chances that directory became file are very small
0279              * so test_dir flag allows to use a single syscall
0280              * in ngx_file_info() instead of three syscalls
0281              */
0282 
0283             of->test_dir = 1;
0284         }
0285 
0286         of->fd = file->fd;
0287         of->uniq = file->uniq;
0288 
0289         rc = ngx_open_and_stat_file(name, of, pool->log);
0290 
0291         if (rc != NGX_OK && (of->err == 0 || !of->errors)) {
0292             goto failed;
0293         }
0294 
0295         if (of->is_dir) {
0296 
0297             if (file->is_dir || file->err) {
0298                 goto update;
0299             }
0300 
0301             /* file became directory */
0302 
0303         } else if (of->err == 0) {  /* file */
0304 
0305             if (file->is_dir || file->err) {
0306                 goto add_event;
0307             }
0308 
0309             if (of->uniq == file->uniq) {
0310 
0311                 if (file->event) {
0312                     file->use_event = 1;
0313                 }
0314 
0315                 of->is_directio = file->is_directio;
0316 
0317                 goto update;
0318             }
0319 
0320             /* file was changed */
0321 
0322         } else { /* error to cache */
0323 
0324             if (file->err || file->is_dir) {
0325                 goto update;
0326             }
0327 
0328             /* file was removed, etc. */
0329         }
0330 
0331         if (file->count == 0) {
0332 
0333             ngx_open_file_del_event(file);
0334 
0335             if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {
0336                 ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,
0337                               ngx_close_file_n " \"%V\" failed", name);
0338             }
0339 
0340             goto add_event;
0341         }
0342 
0343         ngx_rbtree_delete(&cache->rbtree, &file->node);
0344 
0345         cache->current--;
0346 
0347         file->close = 1;
0348 
0349         goto create;
0350     }
0351 
0352     /* not found */
0353 
0354     rc = ngx_open_and_stat_file(name, of, pool->log);
0355 
0356     if (rc != NGX_OK && (of->err == 0 || !of->errors)) {
0357         goto failed;
0358     }
0359 
0360 create:
0361 
0362     if (cache->current >= cache->max) {
0363         ngx_expire_old_cached_files(cache, 0, pool->log);
0364     }
0365 
0366     file = ngx_alloc(sizeof(ngx_cached_open_file_t), pool->log);
0367 
0368     if (file == NULL) {
0369         goto failed;
0370     }
0371 
0372     file->name = ngx_alloc(name->len + 1, pool->log);
0373 
0374     if (file->name == NULL) {
0375         ngx_free(file);
0376         file = NULL;
0377         goto failed;
0378     }
0379 
0380     ngx_cpystrn(file->name, name->data, name->len + 1);
0381 
0382     file->node.key = hash;
0383 
0384     ngx_rbtree_insert(&cache->rbtree, &file->node);
0385 
0386     cache->current++;
0387 
0388     file->uses = 1;
0389     file->count = 0;
0390     file->use_event = 0;
0391     file->event = NULL;
0392 
0393 add_event:
0394 
0395     ngx_open_file_add_event(cache, file, of, pool->log);
0396 
0397 update:
0398 
0399     file->fd = of->fd;
0400     file->err = of->err;
0401 #if (NGX_HAVE_OPENAT)
0402     file->disable_symlinks = of->disable_symlinks;
0403     file->disable_symlinks_from = of->disable_symlinks_from;
0404 #endif
0405 
0406     if (of->err == 0) {
0407         file->uniq = of->uniq;
0408         file->mtime = of->mtime;
0409         file->size = of->size;
0410 
0411         file->close = 0;
0412 
0413         file->is_dir = of->is_dir;
0414         file->is_file = of->is_file;
0415         file->is_link = of->is_link;
0416         file->is_exec = of->is_exec;
0417         file->is_directio = of->is_directio;
0418 
0419         if (!of->is_dir) {
0420             file->count++;
0421         }
0422     }
0423 
0424     file->created = now;
0425 
0426 found:
0427 
0428     file->accessed = now;
0429 
0430     ngx_queue_insert_head(&cache->expire_queue, &file->queue);
0431 
0432     ngx_log_debug5(NGX_LOG_DEBUG_CORE, pool->log, 0,
0433                    "cached open file: %s, fd:%d, c:%d, e:%d, u:%d",
0434                    file->name, file->fd, file->count, file->err, file->uses);
0435 
0436     if (of->err == 0) {
0437 
0438         if (!of->is_dir) {
0439             cln->handler = ngx_open_file_cleanup;
0440             ofcln = cln->data;
0441 
0442             ofcln->cache = cache;
0443             ofcln->file = file;
0444             ofcln->min_uses = of->min_uses;
0445             ofcln->log = pool->log;
0446         }
0447 
0448         return NGX_OK;
0449     }
0450 
0451     return NGX_ERROR;
0452 
0453 failed:
0454 
0455     if (file) {
0456         ngx_rbtree_delete(&cache->rbtree, &file->node);
0457 
0458         cache->current--;
0459 
0460         if (file->count == 0) {
0461 
0462             if (file->fd != NGX_INVALID_FILE) {
0463                 if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {
0464                     ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,
0465                                   ngx_close_file_n " \"%s\" failed",
0466                                   file->name);
0467                 }
0468             }
0469 
0470             ngx_free(file->name);
0471             ngx_free(file);
0472 
0473         } else {
0474             file->close = 1;
0475         }
0476     }
0477 
0478     if (of->fd != NGX_INVALID_FILE) {
0479         if (ngx_close_file(of->fd) == NGX_FILE_ERROR) {
0480             ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,
0481                           ngx_close_file_n " \"%V\" failed", name);
0482         }
0483     }
0484 
0485     return NGX_ERROR;
0486 }
0487 
0488 
0489 #if (NGX_HAVE_OPENAT)
0490 
0491 static ngx_fd_t
0492 ngx_openat_file_owner(ngx_fd_t at_fd, const u_char *name,
0493     ngx_int_t mode, ngx_int_t create, ngx_int_t access, ngx_log_t *log)
0494 {
0495     ngx_fd_t         fd;
0496     ngx_err_t        err;
0497     ngx_file_info_t  fi, atfi;
0498 
0499     /*
0500      * To allow symlinks with the same owner, use openat() (followed
0501      * by fstat()) and fstatat(AT_SYMLINK_NOFOLLOW), and then compare
0502      * uids between fstat() and fstatat().
0503      *
0504      * As there is a race between openat() and fstatat() we don't
0505      * know if openat() in fact opened symlink or not.  Therefore,
0506      * we have to compare uids even if fstatat() reports the opened
0507      * component isn't a symlink (as we don't know whether it was
0508      * symlink during openat() or not).
0509      */
0510 
0511     fd = ngx_openat_file(at_fd, name, mode, create, access);
0512 
0513     if (fd == NGX_INVALID_FILE) {
0514         return NGX_INVALID_FILE;
0515     }
0516 
0517     if (ngx_file_at_info(at_fd, name, &atfi, AT_SYMLINK_NOFOLLOW)
0518         == NGX_FILE_ERROR)
0519     {
0520         err = ngx_errno;
0521         goto failed;
0522     }
0523 
0524 #if (NGX_HAVE_O_PATH)
0525     if (ngx_file_o_path_info(fd, &fi, log) == NGX_ERROR) {
0526         err = ngx_errno;
0527         goto failed;
0528     }
0529 #else
0530     if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) {
0531         err = ngx_errno;
0532         goto failed;
0533     }
0534 #endif
0535 
0536     if (fi.st_uid != atfi.st_uid) {
0537         err = NGX_ELOOP;
0538         goto failed;
0539     }
0540 
0541     return fd;
0542 
0543 failed:
0544 
0545     if (ngx_close_file(fd) == NGX_FILE_ERROR) {
0546         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
0547                       ngx_close_file_n " \"%s\" failed", name);
0548     }
0549 
0550     ngx_set_errno(err);
0551 
0552     return NGX_INVALID_FILE;
0553 }
0554 
0555 
0556 #if (NGX_HAVE_O_PATH)
0557 
0558 static ngx_int_t
0559 ngx_file_o_path_info(ngx_fd_t fd, ngx_file_info_t *fi, ngx_log_t *log)
0560 {
0561     static ngx_uint_t  use_fstat = 1;
0562 
0563     /*
0564      * In Linux 2.6.39 the O_PATH flag was introduced that allows to obtain
0565      * a descriptor without actually opening file or directory.  It requires
0566      * less permissions for path components, but till Linux 3.6 fstat() returns
0567      * EBADF on such descriptors, and fstatat() with the AT_EMPTY_PATH flag
0568      * should be used instead.
0569      *
0570      * Three scenarios are handled in this function:
0571      *
0572      * 1) The kernel is newer than 3.6 or fstat() with O_PATH support was
0573      *    backported by vendor.  Then fstat() is used.
0574      *
0575      * 2) The kernel is newer than 2.6.39 but older than 3.6.  In this case
0576      *    the first call of fstat() returns EBADF and we fallback to fstatat()
0577      *    with AT_EMPTY_PATH which was introduced at the same time as O_PATH.
0578      *
0579      * 3) The kernel is older than 2.6.39 but nginx was build with O_PATH
0580      *    support.  Since descriptors are opened with O_PATH|O_RDONLY flags
0581      *    and O_PATH is ignored by the kernel then the O_RDONLY flag is
0582      *    actually used.  In this case fstat() just works.
0583      */
0584 
0585     if (use_fstat) {
0586         if (ngx_fd_info(fd, fi) != NGX_FILE_ERROR) {
0587             return NGX_OK;
0588         }
0589 
0590         if (ngx_errno != NGX_EBADF) {
0591             return NGX_ERROR;
0592         }
0593 
0594         ngx_log_error(NGX_LOG_NOTICE, log, 0,
0595                       "fstat(O_PATH) failed with EBADF, "
0596                       "switching to fstatat(AT_EMPTY_PATH)");
0597 
0598         use_fstat = 0;
0599     }
0600 
0601     if (ngx_file_at_info(fd, "", fi, AT_EMPTY_PATH) != NGX_FILE_ERROR) {
0602         return NGX_OK;
0603     }
0604 
0605     return NGX_ERROR;
0606 }
0607 
0608 #endif
0609 
0610 #endif /* NGX_HAVE_OPENAT */
0611 
0612 
0613 static ngx_fd_t
0614 ngx_open_file_wrapper(ngx_str_t *name, ngx_open_file_info_t *of,
0615     ngx_int_t mode, ngx_int_t create, ngx_int_t access, ngx_log_t *log)
0616 {
0617     ngx_fd_t  fd;
0618 
0619 #if !(NGX_HAVE_OPENAT)
0620 
0621     fd = ngx_open_file(name->data, mode, create, access);
0622 
0623     if (fd == NGX_INVALID_FILE) {
0624         of->err = ngx_errno;
0625         of->failed = ngx_open_file_n;
0626         return NGX_INVALID_FILE;
0627     }
0628 
0629     return fd;
0630 
0631 #else
0632 
0633     u_char           *p, *cp, *end;
0634     ngx_fd_t          at_fd;
0635     ngx_str_t         at_name;
0636 
0637     if (of->disable_symlinks == NGX_DISABLE_SYMLINKS_OFF) {
0638         fd = ngx_open_file(name->data, mode, create, access);
0639 
0640         if (fd == NGX_INVALID_FILE) {
0641             of->err = ngx_errno;
0642             of->failed = ngx_open_file_n;
0643             return NGX_INVALID_FILE;
0644         }
0645 
0646         return fd;
0647     }
0648 
0649     p = name->data;
0650     end = p + name->len;
0651 
0652     at_name = *name;
0653 
0654     if (of->disable_symlinks_from) {
0655 
0656         cp = p + of->disable_symlinks_from;
0657 
0658         *cp = '\0';
0659 
0660         at_fd = ngx_open_file(p, NGX_FILE_SEARCH|NGX_FILE_NONBLOCK,
0661                               NGX_FILE_OPEN, 0);
0662 
0663         *cp = '/';
0664 
0665         if (at_fd == NGX_INVALID_FILE) {
0666             of->err = ngx_errno;
0667             of->failed = ngx_open_file_n;
0668             return NGX_INVALID_FILE;
0669         }
0670 
0671         at_name.len = of->disable_symlinks_from;
0672         p = cp + 1;
0673 
0674     } else if (*p == '/') {
0675 
0676         at_fd = ngx_open_file("/",
0677                               NGX_FILE_SEARCH|NGX_FILE_NONBLOCK,
0678                               NGX_FILE_OPEN, 0);
0679 
0680         if (at_fd == NGX_INVALID_FILE) {
0681             of->err = ngx_errno;
0682             of->failed = ngx_openat_file_n;
0683             return NGX_INVALID_FILE;
0684         }
0685 
0686         at_name.len = 1;
0687         p++;
0688 
0689     } else {
0690         at_fd = NGX_AT_FDCWD;
0691     }
0692 
0693     for ( ;; ) {
0694         cp = ngx_strlchr(p, end, '/');
0695         if (cp == NULL) {
0696             break;
0697         }
0698 
0699         if (cp == p) {
0700             p++;
0701             continue;
0702         }
0703 
0704         *cp = '\0';
0705 
0706         if (of->disable_symlinks == NGX_DISABLE_SYMLINKS_NOTOWNER) {
0707             fd = ngx_openat_file_owner(at_fd, p,
0708                                        NGX_FILE_SEARCH|NGX_FILE_NONBLOCK,
0709                                        NGX_FILE_OPEN, 0, log);
0710 
0711         } else {
0712             fd = ngx_openat_file(at_fd, p,
0713                            NGX_FILE_SEARCH|NGX_FILE_NONBLOCK|NGX_FILE_NOFOLLOW,
0714                            NGX_FILE_OPEN, 0);
0715         }
0716 
0717         *cp = '/';
0718 
0719         if (fd == NGX_INVALID_FILE) {
0720             of->err = ngx_errno;
0721             of->failed = ngx_openat_file_n;
0722             goto failed;
0723         }
0724 
0725         if (at_fd != NGX_AT_FDCWD && ngx_close_file(at_fd) == NGX_FILE_ERROR) {
0726             ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
0727                           ngx_close_file_n " \"%V\" failed", &at_name);
0728         }
0729 
0730         p = cp + 1;
0731         at_fd = fd;
0732         at_name.len = cp - at_name.data;
0733     }
0734 
0735     if (p == end) {
0736 
0737         /*
0738          * If pathname ends with a trailing slash, assume the last path
0739          * component is a directory and reopen it with requested flags;
0740          * if not, fail with ENOTDIR as per POSIX.
0741          *
0742          * We cannot rely on O_DIRECTORY in the loop above to check
0743          * that the last path component is a directory because
0744          * O_DIRECTORY doesn't work on FreeBSD 8.  Fortunately, by
0745          * reopening a directory, we don't depend on it at all.
0746          */
0747 
0748         fd = ngx_openat_file(at_fd, ".", mode, create, access);
0749         goto done;
0750     }
0751 
0752     if (of->disable_symlinks == NGX_DISABLE_SYMLINKS_NOTOWNER
0753         && !(create & (NGX_FILE_CREATE_OR_OPEN|NGX_FILE_TRUNCATE)))
0754     {
0755         fd = ngx_openat_file_owner(at_fd, p, mode, create, access, log);
0756 
0757     } else {
0758         fd = ngx_openat_file(at_fd, p, mode|NGX_FILE_NOFOLLOW, create, access);
0759     }
0760 
0761 done:
0762 
0763     if (fd == NGX_INVALID_FILE) {
0764         of->err = ngx_errno;
0765         of->failed = ngx_openat_file_n;
0766     }
0767 
0768 failed:
0769 
0770     if (at_fd != NGX_AT_FDCWD && ngx_close_file(at_fd) == NGX_FILE_ERROR) {
0771         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
0772                       ngx_close_file_n " \"%V\" failed", &at_name);
0773     }
0774 
0775     return fd;
0776 #endif
0777 }
0778 
0779 
0780 static ngx_int_t
0781 ngx_file_info_wrapper(ngx_str_t *name, ngx_open_file_info_t *of,
0782     ngx_file_info_t *fi, ngx_log_t *log)
0783 {
0784     ngx_int_t  rc;
0785 
0786 #if !(NGX_HAVE_OPENAT)
0787 
0788     rc = ngx_file_info(name->data, fi);
0789 
0790     if (rc == NGX_FILE_ERROR) {
0791         of->err = ngx_errno;
0792         of->failed = ngx_file_info_n;
0793         return NGX_FILE_ERROR;
0794     }
0795 
0796     return rc;
0797 
0798 #else
0799 
0800     ngx_fd_t  fd;
0801 
0802     if (of->disable_symlinks == NGX_DISABLE_SYMLINKS_OFF) {
0803 
0804         rc = ngx_file_info(name->data, fi);
0805 
0806         if (rc == NGX_FILE_ERROR) {
0807             of->err = ngx_errno;
0808             of->failed = ngx_file_info_n;
0809             return NGX_FILE_ERROR;
0810         }
0811 
0812         return rc;
0813     }
0814 
0815     fd = ngx_open_file_wrapper(name, of, NGX_FILE_RDONLY|NGX_FILE_NONBLOCK,
0816                                NGX_FILE_OPEN, 0, log);
0817 
0818     if (fd == NGX_INVALID_FILE) {
0819         return NGX_FILE_ERROR;
0820     }
0821 
0822     rc = ngx_fd_info(fd, fi);
0823 
0824     if (rc == NGX_FILE_ERROR) {
0825         of->err = ngx_errno;
0826         of->failed = ngx_fd_info_n;
0827     }
0828 
0829     if (ngx_close_file(fd) == NGX_FILE_ERROR) {
0830         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
0831                       ngx_close_file_n " \"%V\" failed", name);
0832     }
0833 
0834     return rc;
0835 #endif
0836 }
0837 
0838 
0839 static ngx_int_t
0840 ngx_open_and_stat_file(ngx_str_t *name, ngx_open_file_info_t *of,
0841     ngx_log_t *log)
0842 {
0843     ngx_fd_t         fd;
0844     ngx_file_info_t  fi;
0845 
0846     if (of->fd != NGX_INVALID_FILE) {
0847 
0848         if (ngx_file_info_wrapper(name, of, &fi, log) == NGX_FILE_ERROR) {
0849             of->fd = NGX_INVALID_FILE;
0850             return NGX_ERROR;
0851         }
0852 
0853         if (of->uniq == ngx_file_uniq(&fi)) {
0854             goto done;
0855         }
0856 
0857     } else if (of->test_dir) {
0858 
0859         if (ngx_file_info_wrapper(name, of, &fi, log) == NGX_FILE_ERROR) {
0860             of->fd = NGX_INVALID_FILE;
0861             return NGX_ERROR;
0862         }
0863 
0864         if (ngx_is_dir(&fi)) {
0865             goto done;
0866         }
0867     }
0868 
0869     if (!of->log) {
0870 
0871         /*
0872          * Use non-blocking open() not to hang on FIFO files, etc.
0873          * This flag has no effect on a regular files.
0874          */
0875 
0876         fd = ngx_open_file_wrapper(name, of, NGX_FILE_RDONLY|NGX_FILE_NONBLOCK,
0877                                    NGX_FILE_OPEN, 0, log);
0878 
0879     } else {
0880         fd = ngx_open_file_wrapper(name, of, NGX_FILE_APPEND,
0881                                    NGX_FILE_CREATE_OR_OPEN,
0882                                    NGX_FILE_DEFAULT_ACCESS, log);
0883     }
0884 
0885     if (fd == NGX_INVALID_FILE) {
0886         of->fd = NGX_INVALID_FILE;
0887         return NGX_ERROR;
0888     }
0889 
0890     if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) {
0891         ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
0892                       ngx_fd_info_n " \"%V\" failed", name);
0893 
0894         if (ngx_close_file(fd) == NGX_FILE_ERROR) {
0895             ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
0896                           ngx_close_file_n " \"%V\" failed", name);
0897         }
0898 
0899         of->fd = NGX_INVALID_FILE;
0900 
0901         return NGX_ERROR;
0902     }
0903 
0904     if (ngx_is_dir(&fi)) {
0905         if (ngx_close_file(fd) == NGX_FILE_ERROR) {
0906             ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
0907                           ngx_close_file_n " \"%V\" failed", name);
0908         }
0909 
0910         of->fd = NGX_INVALID_FILE;
0911 
0912     } else {
0913         of->fd = fd;
0914 
0915         if (of->read_ahead && ngx_file_size(&fi) > NGX_MIN_READ_AHEAD) {
0916             if (ngx_read_ahead(fd, of->read_ahead) == NGX_ERROR) {
0917                 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
0918                               ngx_read_ahead_n " \"%V\" failed", name);
0919             }
0920         }
0921 
0922         if (of->directio <= ngx_file_size(&fi)) {
0923             if (ngx_directio_on(fd) == NGX_FILE_ERROR) {
0924                 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
0925                               ngx_directio_on_n " \"%V\" failed", name);
0926 
0927             } else {
0928                 of->is_directio = 1;
0929             }
0930         }
0931     }
0932 
0933 done:
0934 
0935     of->uniq = ngx_file_uniq(&fi);
0936     of->mtime = ngx_file_mtime(&fi);
0937     of->size = ngx_file_size(&fi);
0938     of->fs_size = ngx_file_fs_size(&fi);
0939     of->is_dir = ngx_is_dir(&fi);
0940     of->is_file = ngx_is_file(&fi);
0941     of->is_link = ngx_is_link(&fi);
0942     of->is_exec = ngx_is_exec(&fi);
0943 
0944     return NGX_OK;
0945 }
0946 
0947 
0948 /*
0949  * we ignore any possible event setting error and
0950  * fallback to usual periodic file retests
0951  */
0952 
0953 static void
0954 ngx_open_file_add_event(ngx_open_file_cache_t *cache,
0955     ngx_cached_open_file_t *file, ngx_open_file_info_t *of, ngx_log_t *log)
0956 {
0957     ngx_open_file_cache_event_t  *fev;
0958 
0959     if (!(ngx_event_flags & NGX_USE_VNODE_EVENT)
0960         || !of->events
0961         || file->event
0962         || of->fd == NGX_INVALID_FILE
0963         || file->uses < of->min_uses)
0964     {
0965         return;
0966     }
0967 
0968     file->use_event = 0;
0969 
0970     file->event = ngx_calloc(sizeof(ngx_event_t), log);
0971     if (file->event== NULL) {
0972         return;
0973     }
0974 
0975     fev = ngx_alloc(sizeof(ngx_open_file_cache_event_t), log);
0976     if (fev == NULL) {
0977         ngx_free(file->event);
0978         file->event = NULL;
0979         return;
0980     }
0981 
0982     fev->fd = of->fd;
0983     fev->file = file;
0984     fev->cache = cache;
0985 
0986     file->event->handler = ngx_open_file_cache_remove;
0987     file->event->data = fev;
0988 
0989     /*
0990      * although vnode event may be called while ngx_cycle->poll
0991      * destruction, however, cleanup procedures are run before any
0992      * memory freeing and events will be canceled.
0993      */
0994 
0995     file->event->log = ngx_cycle->log;
0996 
0997     if (ngx_add_event(file->event, NGX_VNODE_EVENT, NGX_ONESHOT_EVENT)
0998         != NGX_OK)
0999     {
1000         ngx_free(file->event->data);
1001         ngx_free(file->event);
1002         file->event = NULL;
1003         return;
1004     }
1005 
1006     /*
1007      * we do not set file->use_event here because there may be a race
1008      * condition: a file may be deleted between opening the file and
1009      * adding event, so we rely upon event notification only after
1010      * one file revalidation on next file access
1011      */
1012 
1013     return;
1014 }
1015 
1016 
1017 static void
1018 ngx_open_file_cleanup(void *data)
1019 {
1020     ngx_open_file_cache_cleanup_t  *c = data;
1021 
1022     c->file->count--;
1023 
1024     ngx_close_cached_file(c->cache, c->file, c->min_uses, c->log);
1025 
1026     /* drop one or two expired open files */
1027     ngx_expire_old_cached_files(c->cache, 1, c->log);
1028 }
1029 
1030 
1031 static void
1032 ngx_close_cached_file(ngx_open_file_cache_t *cache,
1033     ngx_cached_open_file_t *file, ngx_uint_t min_uses, ngx_log_t *log)
1034 {
1035     ngx_log_debug5(NGX_LOG_DEBUG_CORE, log, 0,
1036                    "close cached open file: %s, fd:%d, c:%d, u:%d, %d",
1037                    file->name, file->fd, file->count, file->uses, file->close);
1038 
1039     if (!file->close) {
1040 
1041         file->accessed = ngx_time();
1042 
1043         ngx_queue_remove(&file->queue);
1044 
1045         ngx_queue_insert_head(&cache->expire_queue, &file->queue);
1046 
1047         if (file->uses >= min_uses || file->count) {
1048             return;
1049         }
1050     }
1051 
1052     ngx_open_file_del_event(file);
1053 
1054     if (file->count) {
1055         return;
1056     }
1057 
1058     if (file->fd != NGX_INVALID_FILE) {
1059 
1060         if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {
1061             ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
1062                           ngx_close_file_n " \"%s\" failed", file->name);
1063         }
1064 
1065         file->fd = NGX_INVALID_FILE;
1066     }
1067 
1068     if (!file->close) {
1069         return;
1070     }
1071 
1072     ngx_free(file->name);
1073     ngx_free(file);
1074 }
1075 
1076 
1077 static void
1078 ngx_open_file_del_event(ngx_cached_open_file_t *file)
1079 {
1080     if (file->event == NULL) {
1081         return;
1082     }
1083 
1084     (void) ngx_del_event(file->event, NGX_VNODE_EVENT,
1085                          file->count ? NGX_FLUSH_EVENT : NGX_CLOSE_EVENT);
1086 
1087     ngx_free(file->event->data);
1088     ngx_free(file->event);
1089     file->event = NULL;
1090     file->use_event = 0;
1091 }
1092 
1093 
1094 static void
1095 ngx_expire_old_cached_files(ngx_open_file_cache_t *cache, ngx_uint_t n,
1096     ngx_log_t *log)
1097 {
1098     time_t                   now;
1099     ngx_queue_t             *q;
1100     ngx_cached_open_file_t  *file;
1101 
1102     now = ngx_time();
1103 
1104     /*
1105      * n == 1 deletes one or two inactive files
1106      * n == 0 deletes least recently used file by force
1107      *        and one or two inactive files
1108      */
1109 
1110     while (n < 3) {
1111 
1112         if (ngx_queue_empty(&cache->expire_queue)) {
1113             return;
1114         }
1115 
1116         q = ngx_queue_last(&cache->expire_queue);
1117 
1118         file = ngx_queue_data(q, ngx_cached_open_file_t, queue);
1119 
1120         if (n++ != 0 && now - file->accessed <= cache->inactive) {
1121             return;
1122         }
1123 
1124         ngx_queue_remove(q);
1125 
1126         ngx_rbtree_delete(&cache->rbtree, &file->node);
1127 
1128         cache->current--;
1129 
1130         ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
1131                        "expire cached open file: %s", file->name);
1132 
1133         if (!file->err && !file->is_dir) {
1134             file->close = 1;
1135             ngx_close_cached_file(cache, file, 0, log);
1136 
1137         } else {
1138             ngx_free(file->name);
1139             ngx_free(file);
1140         }
1141     }
1142 }
1143 
1144 
1145 static void
1146 ngx_open_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp,
1147     ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
1148 {
1149     ngx_rbtree_node_t       **p;
1150     ngx_cached_open_file_t    *file, *file_temp;
1151 
1152     for ( ;; ) {
1153 
1154         if (node->key < temp->key) {
1155 
1156             p = &temp->left;
1157 
1158         } else if (node->key > temp->key) {
1159 
1160             p = &temp->right;
1161 
1162         } else { /* node->key == temp->key */
1163 
1164             file = (ngx_cached_open_file_t *) node;
1165             file_temp = (ngx_cached_open_file_t *) temp;
1166 
1167             p = (ngx_strcmp(file->name, file_temp->name) < 0)
1168                     ? &temp->left : &temp->right;
1169         }
1170 
1171         if (*p == sentinel) {
1172             break;
1173         }
1174 
1175         temp = *p;
1176     }
1177 
1178     *p = node;
1179     node->parent = temp;
1180     node->left = sentinel;
1181     node->right = sentinel;
1182     ngx_rbt_red(node);
1183 }
1184 
1185 
1186 static ngx_cached_open_file_t *
1187 ngx_open_file_lookup(ngx_open_file_cache_t *cache, ngx_str_t *name,
1188     uint32_t hash)
1189 {
1190     ngx_int_t                rc;
1191     ngx_rbtree_node_t       *node, *sentinel;
1192     ngx_cached_open_file_t  *file;
1193 
1194     node = cache->rbtree.root;
1195     sentinel = cache->rbtree.sentinel;
1196 
1197     while (node != sentinel) {
1198 
1199         if (hash < node->key) {
1200             node = node->left;
1201             continue;
1202         }
1203 
1204         if (hash > node->key) {
1205             node = node->right;
1206             continue;
1207         }
1208 
1209         /* hash == node->key */
1210 
1211         file = (ngx_cached_open_file_t *) node;
1212 
1213         rc = ngx_strcmp(name->data, file->name);
1214 
1215         if (rc == 0) {
1216             return file;
1217         }
1218 
1219         node = (rc < 0) ? node->left : node->right;
1220     }
1221 
1222     return NULL;
1223 }
1224 
1225 
1226 static void
1227 ngx_open_file_cache_remove(ngx_event_t *ev)
1228 {
1229     ngx_cached_open_file_t       *file;
1230     ngx_open_file_cache_event_t  *fev;
1231 
1232     fev = ev->data;
1233     file = fev->file;
1234 
1235     ngx_queue_remove(&file->queue);
1236 
1237     ngx_rbtree_delete(&fev->cache->rbtree, &file->node);
1238 
1239     fev->cache->current--;
1240 
1241     /* NGX_ONESHOT_EVENT was already deleted */
1242     file->event = NULL;
1243     file->use_event = 0;
1244 
1245     file->close = 1;
1246 
1247     ngx_close_cached_file(fev->cache, file, 0, ev->log);
1248 
1249     /* free memory only when fev->cache and fev->file are already not needed */
1250 
1251     ngx_free(ev->data);
1252     ngx_free(ev);
1253 }