00001 /* 00002 * Phusion Passenger - http://www.modrails.com/ 00003 * Copyright (c) 2010 Phusion 00004 * 00005 * "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui. 00006 * 00007 * Permission is hereby granted, free of charge, to any person obtaining a copy 00008 * of this software and associated documentation files (the "Software"), to deal 00009 * in the Software without restriction, including without limitation the rights 00010 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00011 * copies of the Software, and to permit persons to whom the Software is 00012 * furnished to do so, subject to the following conditions: 00013 * 00014 * The above copyright notice and this permission notice shall be included in 00015 * all copies or substantial portions of the Software. 00016 * 00017 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00020 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00021 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00022 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00023 * THE SOFTWARE. 00024 */ 00025 #ifndef _PASSENGER_UTILS_H_ 00026 #define _PASSENGER_UTILS_H_ 00027 00028 #include <boost/shared_ptr.hpp> 00029 #include <sys/types.h> 00030 #include <sys/stat.h> 00031 #include <string> 00032 #include <vector> 00033 #include <utility> 00034 #include <sstream> 00035 #include <cstdio> 00036 #include <climits> 00037 #include <cstdlib> 00038 #include <cstring> 00039 #include <errno.h> 00040 #include <unistd.h> 00041 #include "StaticString.h" 00042 #include "Exceptions.h" 00043 00044 namespace Passenger { 00045 00046 using namespace std; 00047 using namespace boost; 00048 00049 static const uid_t USER_NOT_GIVEN = (uid_t) -1; 00050 static const gid_t GROUP_NOT_GIVEN = (gid_t) -1; 00051 00052 typedef struct CachedFileStat CachedFileStat; 00053 class ResourceLocator; 00054 00055 /** Enumeration which indicates what kind of file a file is. */ 00056 typedef enum { 00057 /** The file doesn't exist. */ 00058 FT_NONEXISTANT, 00059 /** A regular file or a symlink to a regular file. */ 00060 FT_REGULAR, 00061 /** A directory. */ 00062 FT_DIRECTORY, 00063 /** Something else, e.g. a pipe or a socket. */ 00064 FT_OTHER 00065 } FileType; 00066 00067 /** 00068 * Convenience shortcut for creating a <tt>shared_ptr</tt>. 00069 * Instead of: 00070 * @code 00071 * shared_ptr<Foo> foo; 00072 * ... 00073 * foo = shared_ptr<Foo>(new Foo()); 00074 * @endcode 00075 * one can write: 00076 * @code 00077 * shared_ptr<Foo> foo; 00078 * ... 00079 * foo = ptr(new Foo()); 00080 * @endcode 00081 * 00082 * @param pointer The item to put in the shared_ptr object. 00083 * @ingroup Support 00084 */ 00085 template<typename T> shared_ptr<T> 00086 ptr(T *pointer) { 00087 return shared_ptr<T>(pointer); 00088 } 00089 00090 /** 00091 * Check whether the specified file exists. 00092 * 00093 * @param filename The filename to check. 00094 * @param cstat A CachedFileStat object, if you want to use cached statting. 00095 * @param throttleRate A throttle rate for cstat. Only applicable if cstat is not NULL. 00096 * @return Whether the file exists. 00097 * @throws FileSystemException Unable to check because of a filesystem error. 00098 * @throws TimeRetrievalException 00099 * @throws boost::thread_interrupted 00100 * @ingroup Support 00101 */ 00102 bool fileExists(const StaticString &filename, CachedFileStat *cstat = 0, 00103 unsigned int throttleRate = 0); 00104 00105 /** 00106 * Check whether 'filename' exists and what kind of file it is. 00107 * 00108 * @param filename The filename to check. 00109 * @param mstat A CachedFileStat object, if you want to use cached statting. 00110 * @param throttleRate A throttle rate for cstat. Only applicable if cstat is not NULL. 00111 * @return The file type. 00112 * @throws FileSystemException Unable to check because of a filesystem error. 00113 * @throws TimeRetrievalException 00114 * @throws boost::thread_interrupted 00115 * @ingroup Support 00116 */ 00117 FileType getFileType(const StaticString &filename, CachedFileStat *cstat = 0, 00118 unsigned int throttleRate = 0); 00119 00120 /** 00121 * Create the given file with the given contents, permissions and ownership. 00122 * This function does not leave behind junk files: if the ownership cannot be set 00123 * or if not all data can be written then then the file will be deleted. 00124 * 00125 * @param filename The file to create. 00126 * @param contents The contents to write to the file. 00127 * @param permissions The desired file permissions. 00128 * @param owner The desired file owner. Specify USER_NOT_GIVEN if you want to use the current 00129 * process's owner as the file owner. 00130 * @param group The desired file group. Specify GROUP_NOT_GIVEN if you want to use the current 00131 * process's group as the file group. 00132 * @param overwrite Whether to overwrite the file if it exists. If set to false 00133 * and the file exists then nothing will happen. 00134 * @throws FileSystemException Something went wrong. 00135 * @ingroup Support 00136 */ 00137 void createFile(const string &filename, const StaticString &contents, 00138 mode_t permissions = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, 00139 uid_t owner = USER_NOT_GIVEN, gid_t group = GROUP_NOT_GIVEN, 00140 bool overwrite = true); 00141 00142 /** 00143 * Returns a canonical version of the specified path. All symbolic links 00144 * and relative path elements are resolved. 00145 * 00146 * @throws FileSystemException Something went wrong. 00147 * @ingroup Support 00148 */ 00149 string canonicalizePath(const string &path); 00150 00151 /** 00152 * If <em>path</em> refers to a symlink, then this function resolves the 00153 * symlink for 1 level. That is, if the symlink points to another symlink, 00154 * then the other symlink will not be resolved. The resolved path is returned. 00155 * 00156 * If the symlink doesn't point to an absolute path, then this function will 00157 * prepend <em>path</em>'s directory to the result. 00158 * 00159 * If <em>path</em> doesn't refer to a symlink then this method will return 00160 * <em>path</em>. 00161 * 00162 * @throws FileSystemException Something went wrong. 00163 * @ingroup Support 00164 */ 00165 string resolveSymlink(const string &path); 00166 00167 /** 00168 * Given a path, extracts its directory name. 00169 * 00170 * @ingroup Support 00171 */ 00172 string extractDirName(const StaticString &path); 00173 00174 /** 00175 * Given a path, extracts its base name. 00176 * 00177 * @ingroup Support 00178 */ 00179 string extractBaseName(const StaticString &path); 00180 00181 /** 00182 * Escape the given raw string into an XML value. 00183 * 00184 * @throws std::bad_alloc Something went wrong. 00185 * @ingroup Support 00186 */ 00187 string escapeForXml(const string &input); 00188 00189 /** 00190 * Returns the username of the user that the current process is running as. 00191 * If the user has no associated username, then "UID xxxx" is returned, 00192 * where xxxx is the current UID. 00193 */ 00194 string getProcessUsername(); 00195 00196 /** 00197 * Converts a mode string into a mode_t value. 00198 * 00199 * At this time only the symbolic mode strings are supported, e.g. something like looks 00200 * this: "u=rwx,g=w,o=rx". The grammar is as follows: 00201 * @code 00202 * mode ::= (clause ("," clause)*)? 00203 * clause ::= who "=" permission* 00204 * who ::= "u" | "g" | "o" 00205 * permission ::= "r" | "w" | "x" | "s" 00206 * @endcode 00207 * 00208 * Notes: 00209 * - The mode value starts with 0. So if you specify "u=rwx", then the group and world 00210 * permissions will be empty (set to 0). 00211 * - The "s" permission is only allowed for who == "u" or who == "g". 00212 * - The return value does not depend on the umask. 00213 * 00214 * @throws InvalidModeStringException The mode string cannot be parsed. 00215 */ 00216 mode_t parseModeString(const StaticString &mode); 00217 00218 /** 00219 * Return the path name for the directory in which the system stores general 00220 * temporary files. This is usually "/tmp", but might be something else depending 00221 * on some environment variables. 00222 * 00223 * @ensure result != NULL 00224 * @ingroup Support 00225 */ 00226 const char *getSystemTempDir(); 00227 00228 /* Create a temporary directory for storing Phusion Passenger instance-specific 00229 * temp files, such as temporarily buffered uploads, sockets for backend 00230 * processes, etc. 00231 * The directory that will be created is the one returned by 00232 * <tt>getPassengerTempDir(false, parentDir)</tt>. This call stores the path to 00233 * this temp directory in an internal variable, so that subsequent calls to 00234 * getPassengerTempDir() will return the same path. 00235 * 00236 * The created temp directory will have several subdirectories: 00237 * - webserver_private - for storing the web server's buffered uploads. 00238 * - info - for storing files that allow external tools to query information 00239 * about a running Phusion Passenger instance. 00240 * - backends - for storing Unix sockets created by backend processes. 00241 * - master - for storing files such as the Passenger HelperServer socket. 00242 * 00243 * If a (sub)directory already exists, then it will not result in an error. 00244 * 00245 * The <em>userSwitching</em> and <em>lowestUser</em> arguments passed to 00246 * this method are used for determining the optimal permissions for the 00247 * (sub)directories. The permissions will be set as tightly as possible based 00248 * on the values. The <em>workerUid</em> and <em>workerGid</em> arguments 00249 * will be used for determining the owner of certain subdirectories. 00250 * 00251 * @note You should only call this method inside the web server's master 00252 * process. In case of Apache, this is the Apache control process, 00253 * the one that tends to run as root. This is because this function 00254 * will set directory permissions and owners/groups, which may require 00255 * root privileges. 00256 * 00257 * @param parentDir The directory under which the Phusion Passenger-specific 00258 * temp directory should be created. This argument may be the 00259 * empty string, in which case getSystemTempDir() will be used 00260 * as the parent directory. 00261 * @param userSwitching Whether user switching is turned on. 00262 * @param lowestUser The user that the spawn manager and the pool server will 00263 * run as, if user switching is turned off. 00264 * @param workerUid The UID that the web server's worker processes are running 00265 * as. On Apache, this is the UID that's associated with the 00266 * 'User' directive. 00267 * @param workerGid The GID that the web server's worker processes are running 00268 * as. On Apache, this is the GID that's associated with the 00269 * 'Group' directive. 00270 * @throws IOException Something went wrong. 00271 * @throws SystemException Something went wrong. 00272 * @throws FileSystemException Something went wrong. 00273 */ 00274 /* void createPassengerTempDir(const string &parentDir, bool userSwitching, 00275 const string &lowestUser, 00276 uid_t workerUid, gid_t workerGid); */ 00277 00278 /** 00279 * Create the directory at the given path, creating intermediate directories 00280 * if necessary. The created directories' permissions are exactly as specified 00281 * by the 'mode' parameter (i.e. the umask will be ignored). You can specify 00282 * this directory's owner and group through the 'owner' and 'group' parameters. 00283 * A value of USER_NOT_GIVEN for 'owner' and/or GROUP_NOT_GIVEN 'group' means 00284 * that the owner/group should not be changed. 00285 * 00286 * If 'path' already exists, then nothing will happen. 00287 * 00288 * @param mode A mode string, as supported by parseModeString(). 00289 * @throws FileSystemException Something went wrong. 00290 * @throws InvalidModeStringException The mode string cannot be parsed. 00291 */ 00292 void makeDirTree(const string &path, const StaticString &mode = "u=rwx,g=,o=", 00293 uid_t owner = USER_NOT_GIVEN, gid_t group = GROUP_NOT_GIVEN); 00294 00295 /** 00296 * Remove an entire directory tree recursively. If the directory doesn't exist then this 00297 * function does nothing. 00298 * 00299 * @throws FileSystemException Something went wrong. 00300 */ 00301 void removeDirTree(const string &path); 00302 00303 /** 00304 * Check whether the specified directory is a valid Ruby on Rails 00305 * application root directory. 00306 * 00307 * @param cstat A CachedFileStat object, if you want to use cached statting. 00308 * @param throttleRate A throttle rate for cstat. Only applicable if cstat is not NULL. 00309 * @throws FileSystemException Unable to check because of a system error. 00310 * @throws TimeRetrievalException 00311 * @throws boost::thread_interrupted 00312 * @ingroup Support 00313 */ 00314 bool verifyRailsDir(const string &dir, CachedFileStat *cstat = 0, 00315 unsigned int throttleRate = 0); 00316 00317 /** 00318 * Check whether the specified directory is a valid Rack application 00319 * root directory. 00320 * 00321 * @param cstat A CachedFileStat object, if you want to use cached statting. 00322 * @param throttleRate A throttle rate for cstat. Only applicable if cstat is not NULL. 00323 * @throws FileSystemException Unable to check because of a filesystem error. 00324 * @throws TimeRetrievalException 00325 * @throws boost::thread_interrupted 00326 * @ingroup Support 00327 */ 00328 bool verifyRackDir(const string &dir, CachedFileStat *cstat = 0, 00329 unsigned int throttleRate = 0); 00330 00331 /** 00332 * Check whether the specified directory is a valid WSGI application 00333 * root directory. 00334 * 00335 * @param cstat A CachedFileStat object, if you want to use cached statting. 00336 * @param throttleRate A throttle rate for cstat. Only applicable if cstat is not NULL. 00337 * @throws FileSystemException Unable to check because of a filesystem error. 00338 * @throws TimeRetrievalException 00339 * @throws boost::thread_interrupted 00340 * @ingroup Support 00341 */ 00342 bool verifyWSGIDir(const string &dir, CachedFileStat *cstat = 0, 00343 unsigned int throttleRate = 0); 00344 00345 void prestartWebApps(const ResourceLocator &locator, const string &serializedprestartURLs); 00346 00347 /** 00348 * Returns the system's host name. 00349 * 00350 * @throws SystemException The host name cannot be retrieved. 00351 */ 00352 string getHostName(); 00353 00354 /** 00355 * Convert a signal number to its associated name. 00356 */ 00357 string getSignalName(int sig); 00358 00359 /** 00360 * Resets the current process's signal handler disposition and signal mask 00361 * to default values. One should call this every time one forks a child process; 00362 * non-default signal masks/handler dispositions can cause all kinds of weird quirks, 00363 * like waitpid() malfunctioning on OS X. 00364 * 00365 * This function is async-signal safe. 00366 */ 00367 void resetSignalHandlersAndMask(); 00368 00369 /** 00370 * Close all file descriptors that are higher than <em>lastToKeepOpen</em>. 00371 * This function is async-signal safe. But make sure there are no other 00372 * threads running that might open file descriptors! 00373 */ 00374 void closeAllFileDescriptors(int lastToKeepOpen); 00375 00376 00377 /** 00378 * Represents a buffered upload file. 00379 * 00380 * @ingroup Support 00381 */ 00382 class BufferedUpload { 00383 public: 00384 /** The file handle. */ 00385 FILE *handle; 00386 00387 /** 00388 * Create an empty upload bufer file, and open it for reading and writing. 00389 * 00390 * @throws SystemException Something went wrong. 00391 */ 00392 BufferedUpload(const string &dir, const char *identifier = "temp") { 00393 char templ[PATH_MAX]; 00394 int fd; 00395 00396 snprintf(templ, sizeof(templ), "%s/%s.XXXXXX", dir.c_str(), identifier); 00397 templ[sizeof(templ) - 1] = '\0'; 00398 fd = mkstemp(templ); 00399 if (fd == -1) { 00400 char message[1024]; 00401 int e = errno; 00402 00403 snprintf(message, sizeof(message), "Cannot create a temporary file '%s'", templ); 00404 message[sizeof(message) - 1] = '\0'; 00405 throw SystemException(message, e); 00406 } 00407 00408 /* We use a POSIX trick here: the file's permissions are set to "u=,g=,o=" 00409 * and the file is deleted immediately from the filesystem, while we 00410 * keep its file handle open. The result is that no other processes 00411 * will be able to access this file's contents anymore, except us. 00412 * We now have an anonymous disk-backed buffer. 00413 */ 00414 fchmod(fd, 0000); 00415 unlink(templ); 00416 00417 handle = fdopen(fd, "w+"); 00418 } 00419 00420 ~BufferedUpload() { 00421 fclose(handle); 00422 } 00423 }; 00424 00425 } // namespace Passenger 00426 00427 #endif /* _PASSENGER_UTILS_H_ */ 00428