Changeset 423


Ignore:
Timestamp:
07/11/10 23:03:25 (2 months ago)
Author:
bart
Message:

Changed the way the cache directory is created:

  • The forked process creates the cache dir and makes it world writable
  • The first request creates a subdir with the userid of the process and with permissions 700.
  • TODO: windows and symlink checks
Location:
eaccelerator/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • eaccelerator/trunk/eaccelerator.c

    r420 r423  
    258258  ea_mm_instance->check_mtime_enabled = 1; 
    259259  ea_mm_instance->removed = NULL; 
     260  ea_mm_instance->cache_dir_uid = 0; 
    260261  ea_mm_instance->last_prune = time(NULL);      /* this time() call is harmless since this is init phase */ 
    261262  EACCELERATOR_PROTECT(); 
     
    392393  PHP_MD5Final(digest, &context); 
    393394  make_digest(md5str, digest); 
    394   snprintf(s, MAXPATHLEN-1, "%s/", EAG(cache_dir)); 
     395  snprintf(s, MAXPATHLEN-1, "%s/%d/", EAG(cache_dir), ea_mm_instance->cache_dir_uid); 
    395396  n = strlen(s); 
    396397  for (i = 0; i < EACCELERATOR_HASH_LEVEL && n < MAXPATHLEN - 1; i++) { 
     
    16561657} 
    16571658 
     1659/* 
     1660 * Create a hash directory 
     1661 */ 
    16581662static void make_hash_dirs(char *fullpath, int lvl) { 
    1659   int j; 
    1660   int n = strlen(fullpath); 
    1661   mode_t old_umask = umask(0); 
    1662    
    1663   if (lvl < 1) 
    1664     return; 
    1665   if (fullpath[n-1] != '/') 
    1666     fullpath[n++] = '/'; 
    1667    
    1668   for (j = 0; j < 16; j++) { 
    1669     fullpath[n] = num2hex[j];        
    1670     fullpath[n+1] = 0; 
    1671     mkdir(fullpath, 0777); 
    1672     make_hash_dirs(fullpath, lvl-1); 
    1673   } 
    1674   fullpath[n+2] = 0; 
    1675   umask(old_umask); 
    1676 } 
    1677  
     1663        int j; 
     1664        int n = strlen(fullpath); 
     1665 
     1666        //ea_debug_error("Creating hash in %s at level %d\n", fullpath, lvl); 
     1667 
     1668        if (lvl < 1) { 
     1669                return; 
     1670        } 
     1671 
     1672        if (fullpath[n-1] != '/') { 
     1673                fullpath[n++] = '/'; 
     1674        } 
     1675 
     1676        for (j = 0; j < 16; j++) { 
     1677                fullpath[n] = num2hex[j]; 
     1678                fullpath[n+1] = 0; 
     1679                mkdir(fullpath, 0700); 
     1680                make_hash_dirs(fullpath, lvl-1); 
     1681        } 
     1682        fullpath[n+2] = 0; 
     1683} 
     1684 
     1685/* 
     1686 * Initialise the cache directory for use 
     1687 */ 
     1688static void init_cache_dir(const char *cache_path) { 
     1689        char fullpath[MAXPATHLEN]; 
     1690        uid_t uid = getuid(); 
     1691        mode_t old_umask = umask(077); 
     1692        struct stat buffer; 
     1693 
     1694    snprintf(fullpath, MAXPATHLEN-1, "%s/%d/", cache_path, uid); 
     1695    if (lstat(fullpath, &buffer) != 0) { 
     1696        // error, create the directory 
     1697        if (mkdir(fullpath, 0700) != 0) { 
     1698                ea_debug_error("Unable to create cachedir %s\n", fullpath); 
     1699                return; 
     1700        } 
     1701    } else if (!S_ISDIR(buffer.st_mode)) { 
     1702        // not a directory 
     1703                ea_debug_error("Cachedir %s exists but is not a directory\n", 
     1704                                fullpath); 
     1705                return; 
     1706        } 
     1707 
     1708    // create the hashed dirs 
     1709    make_hash_dirs(fullpath, EACCELERATOR_HASH_LEVEL); 
     1710 
     1711        umask(old_umask); 
     1712 
     1713        ea_mm_instance->cache_dir_uid = uid; 
     1714} 
     1715 
     1716/* 
     1717 * Check if the cache dir exists and is world-writable so the forked process 
     1718 * can create the cache directories 
     1719 */ 
     1720static void check_cache_dir(const char *cache_path) { 
     1721        struct stat buffer; 
     1722        mode_t old_umask = umask(0); 
     1723 
     1724        int status = stat(cache_path, &buffer); 
     1725 
     1726        if (status == 0) { 
     1727                // check permissions 
     1728                if (buffer.st_mode != 777) { 
     1729                        status = chmod(cache_path, 0777); 
     1730                        if (status < 0) { 
     1731                                ea_debug_error( 
     1732                                        "Unable to change cache cache directory %s permissions\n", 
     1733                                        cache_path); 
     1734                        } 
     1735                } 
     1736        } else { 
     1737                // create the cache directory if possible 
     1738                status = mkdir(cache_path, 0777); 
     1739                if (status < 0) { 
     1740                        ea_debug_error("Unable to create cache directory %s\n", cache_path); 
     1741                } 
     1742        } 
     1743 
     1744        umask(old_umask); 
     1745} 
    16781746 
    16791747PHP_MINIT_FUNCTION(eaccelerator) { 
    1680   char fullpath[MAXPATHLEN]; 
    1681  
    16821748  if (type == MODULE_PERSISTENT) { 
    16831749#ifndef ZEND_WIN32 
     
    17111777  ea_debug_init(TSRMLS_C); 
    17121778 
    1713   if(!ea_scripts_shm_only) { 
    1714     snprintf(fullpath, MAXPATHLEN-1, "%s/", EAG(cache_dir)); 
    1715     make_hash_dirs(fullpath, EACCELERATOR_HASH_LEVEL); 
    1716   } 
     1779  check_cache_dir(EAG(cache_dir)); 
    17171780 
    17181781  if (type == MODULE_PERSISTENT && 
     
    18101873        DBG(ea_debug_printf, (EA_DEBUG, "[%d] Leave RINIT\n",getpid())); 
    18111874         
     1875        if (ea_mm_instance->cache_dir_uid != getuid()) { 
     1876                // lock this operation with a global eA lock and do the check again 
     1877                // to avoid multiple calls during startup 
     1878                EACCELERATOR_LOCK_RW(); 
     1879                if (ea_mm_instance->cache_dir_uid != getuid()) { 
     1880                        init_cache_dir(EAG(cache_dir)); 
     1881                } 
     1882                EACCELERATOR_UNLOCK(); 
     1883        } 
     1884 
    18121885        return SUCCESS; 
    18131886} 
     
    18711944  PHP_FE(eaccelerator_caching, NULL) 
    18721945  PHP_FE(eaccelerator_clear, NULL) 
    1873         PHP_FE(eaccelerator_clean, NULL) 
     1946  PHP_FE(eaccelerator_clean, NULL) 
    18741947  PHP_FE(eaccelerator_info, NULL) 
    18751948  PHP_FE(eaccelerator_purge, NULL) 
     
    18781951  PHP_FE(eaccelerator_check_mtime, NULL) 
    18791952  #ifdef WITH_EACCELERATOR_OPTIMIZER 
    1880     PHP_FE(eaccelerator_optimizer, NULL) 
     1953  PHP_FE(eaccelerator_optimizer, NULL) 
    18811954  #endif 
    18821955#endif 
  • eaccelerator/trunk/eaccelerator.h

    r394 r423  
    333333        time_t last_prune; 
    334334        ea_cache_entry *removed; 
     335        uid_t cache_dir_uid; 
    335336 
    336337        ea_cache_entry *hash[EA_HASH_SIZE]; 
Note: See TracChangeset for help on using the changeset viewer.