1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
|
/*
* Phusion Passenger - http://www.modrails.com/
* Copyright (c) 2010 Phusion
*
* "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <oxt/system_calls.hpp>
#include <boost/thread.hpp>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <sys/uio.h>
#include <libgen.h>
#include <fcntl.h>
#include <poll.h>
#include <dirent.h>
#include <limits.h>
#include <unistd.h>
#include <signal.h>
#include <FileDescriptor.h>
#include <MessageServer.h>
#include <ResourceLocator.h>
#include <Exceptions.h>
#include <Utils.h>
#include <Utils/Base64.h>
#include <Utils/CachedFileStat.hpp>
#include <Utils/StrIntUtils.h>
#include <Utils/HttpHeaderBufferer.h>
#ifndef HOST_NAME_MAX
#if defined(_POSIX_HOST_NAME_MAX)
#define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
#elif defined(_SC_HOST_NAME_MAX)
#define HOST_NAME_MAX sysconf(_SC_HOST_NAME_MAX)
#else
#define HOST_NAME_MAX 255
#endif
#endif
#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__sun)
// Introduced in Solaris 9. Let's hope nobody actually uses
// a version that doesn't support this.
#define HAS_CLOSEFROM
#endif
namespace Passenger {
static string passengerTempDir;
HttpHeaderBufferer::StaticData HttpHeaderBufferer::staticData;
namespace {
/**
* Given a filename, FileGuard will unlink the file in its destructor, unless
* commit() was called. Used in file operation functions that don't want to
* leave behind half-finished files after error conditions.
*/
struct FileGuard {
string filename;
bool committed;
FileGuard(const string &filename) {
this->filename = filename;
committed = false;
}
~FileGuard() {
if (!committed) {
int ret;
do {
ret = unlink(filename.c_str());
} while (ret == -1 && errno == EINTR);
}
}
void commit() {
committed = true;
}
};
}
bool
fileExists(const StaticString &filename, CachedFileStat *cstat, unsigned int throttleRate) {
return getFileType(filename, cstat, throttleRate) == FT_REGULAR;
}
FileType
getFileType(const StaticString &filename, CachedFileStat *cstat, unsigned int throttleRate) {
struct stat buf;
int ret;
if (cstat != NULL) {
ret = cstat->stat(filename.toString(), &buf, throttleRate);
} else {
ret = stat(filename.c_str(), &buf);
}
if (ret == 0) {
if (S_ISREG(buf.st_mode)) {
return FT_REGULAR;
} else if (S_ISDIR(buf.st_mode)) {
return FT_DIRECTORY;
} else {
return FT_OTHER;
}
} else {
if (errno == ENOENT) {
return FT_NONEXISTANT;
} else {
int e = errno;
string message("Cannot stat '");
message.append(filename);
message.append("'");
throw FileSystemException(message, e, filename);
}
}
}
void
createFile(const string &filename, const StaticString &contents, mode_t permissions, uid_t owner,
gid_t group, bool overwrite)
{
FileDescriptor fd;
int ret, e, options;
options = O_WRONLY | O_CREAT | O_TRUNC;
if (!overwrite) {
options |= O_EXCL;
}
do {
fd = open(filename.c_str(), options, permissions);
} while (fd == -1 && errno == EINTR);
if (fd != -1) {
FileGuard guard(filename);
// The file permission may not be as expected because of the active
// umask, so fchmod() it here to ensure correct permissions.
do {
ret = fchmod(fd, permissions);
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
e = errno;
throw FileSystemException("Cannot set permissions on " + filename,
e, filename);
}
if (owner != USER_NOT_GIVEN && group != GROUP_NOT_GIVEN) {
if (owner == USER_NOT_GIVEN) {
owner = (uid_t) -1; // Don't let fchown change file owner.
}
if (group == GROUP_NOT_GIVEN) {
group = (gid_t) -1; // Don't let fchown change file group.
}
do {
ret = fchown(fd, owner, group);
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
e = errno;
throw FileSystemException("Cannot set ownership for " + filename,
e, filename);
}
}
try {
writeExact(fd, contents);
fd.close();
} catch (const SystemException &e) {
throw FileSystemException("Cannot write to file " + filename,
e.code(), filename);
}
guard.commit();
} else {
e = errno;
if (overwrite || e != EEXIST) {
throw FileSystemException("Cannot create file " + filename,
e, filename);
}
}
}
string
canonicalizePath(const string &path) {
#ifdef __GLIBC__
// We're using a GNU extension here. See the 'BUGS'
// section of the realpath(3) Linux manpage for
// rationale.
char *tmp = realpath(path.c_str(), NULL);
if (tmp == NULL) {
int e = errno;
string message;
message = "Cannot resolve the path '";
message.append(path);
message.append("'");
throw FileSystemException(message, e, path);
} else {
string result(tmp);
free(tmp);
return result;
}
#else
char tmp[PATH_MAX];
if (realpath(path.c_str(), tmp) == NULL) {
int e = errno;
string message;
message = "Cannot resolve the path '";
message.append(path);
message.append("'");
throw FileSystemException(message, e, path);
} else {
return tmp;
}
#endif
}
string
resolveSymlink(const string &path) {
char buf[PATH_MAX];
ssize_t size;
size = readlink(path.c_str(), buf, sizeof(buf) - 1);
if (size == -1) {
if (errno == EINVAL) {
return path;
} else {
int e = errno;
string message = "Cannot resolve possible symlink '";
message.append(path);
message.append("'");
throw FileSystemException(message, e, path);
}
} else {
buf[size] = '\0';
if (buf[0] == '\0') {
string message = "The file '";
message.append(path);
message.append("' is a symlink, and it refers to an empty filename. This is not allowed.");
throw FileSystemException(message, ENOENT, path);
} else if (buf[0] == '/') {
// Symlink points to an absolute path.
return buf;
} else {
return extractDirName(path) + "/" + buf;
}
}
}
string
extractDirName(const StaticString &path) {
char *path_copy = strdup(path.c_str());
char *result = dirname(path_copy);
string result_string(result);
free(path_copy);
return result_string;
}
string
extractBaseName(const StaticString &path) {
char *path_copy = strdup(path.c_str());
string result_string = basename(path_copy);
free(path_copy);
return result_string;
}
string
escapeForXml(const string &input) {
string result(input);
string::size_type input_pos = 0;
string::size_type input_end_pos = input.size();
string::size_type result_pos = 0;
while (input_pos < input_end_pos) {
const unsigned char ch = input[input_pos];
if ((ch >= 'A' && ch <= 'z')
|| (ch >= '0' && ch <= '9')
|| ch == '/' || ch == ' ' || ch == '_' || ch == '.'
|| ch == ':' || ch == '+' || ch == '-') {
// This is an ASCII character. Ignore it and
// go to next character.
result_pos++;
} else {
// Not an ASCII character; escape it.
char escapedCharacter[sizeof("ÿ") + 1];
int size;
size = snprintf(escapedCharacter,
sizeof(escapedCharacter) - 1,
"&#%d;",
(int) ch);
if (size < 0) {
throw std::bad_alloc();
}
escapedCharacter[sizeof(escapedCharacter) - 1] = '\0';
result.replace(result_pos, 1, escapedCharacter, size);
result_pos += size;
}
input_pos++;
}
return result;
}
string
getProcessUsername() {
struct passwd pwd, *result;
char strings[1024];
int ret;
result = (struct passwd *) NULL;
do {
ret = getpwuid_r(getuid(), &pwd, strings, sizeof(strings), &result);
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
result = (struct passwd *) NULL;
}
if (result == (struct passwd *) NULL) {
snprintf(strings, sizeof(strings), "UID %lld", (long long) getuid());
strings[sizeof(strings) - 1] = '\0';
return strings;
} else {
return result->pw_name;
}
}
mode_t
parseModeString(const StaticString &mode) {
mode_t modeBits = 0;
vector<string> clauses;
vector<string>::iterator it;
split(mode, ',', clauses);
for (it = clauses.begin(); it != clauses.end(); it++) {
const string &clause = *it;
if (clause.empty()) {
continue;
} else if (clause.size() < 2 || clause[1] != '=') {
throw InvalidModeStringException("Invalid mode clause specification '" + clause + "'");
}
switch (clause[0]) {
case 'u':
for (string::size_type i = 2; i < clause.size(); i++) {
switch (clause[i]) {
case 'r':
modeBits |= S_IRUSR;
break;
case 'w':
modeBits |= S_IWUSR;
break;
case 'x':
modeBits |= S_IXUSR;
break;
case 's':
modeBits |= S_ISUID;
break;
default:
throw InvalidModeStringException("Invalid permission '" +
string(1, clause[i]) +
"' in mode clause specification '" +
clause + "'");
}
}
break;
case 'g':
for (string::size_type i = 2; i < clause.size(); i++) {
switch (clause[i]) {
case 'r':
modeBits |= S_IRGRP;
break;
case 'w':
modeBits |= S_IWGRP;
break;
case 'x':
modeBits |= S_IXGRP;
break;
case 's':
modeBits |= S_ISGID;
break;
default:
throw InvalidModeStringException("Invalid permission '" +
string(1, clause[i]) +
"' in mode clause specification '" +
clause + "'");
}
}
break;
case 'o':
for (string::size_type i = 2; i < clause.size(); i++) {
switch (clause[i]) {
case 'r':
modeBits |= S_IROTH;
break;
case 'w':
modeBits |= S_IWOTH;
break;
case 'x':
modeBits |= S_IXOTH;
break;
default:
throw InvalidModeStringException("Invalid permission '" +
string(1, clause[i]) +
"' in mode clause specification '" +
clause + "'");
}
}
break;
default:
throw InvalidModeStringException("Invalid owner '" + string(1, clause[0]) +
"' in mode clause specification '" + clause + "'");
}
}
return modeBits;
}
const char *
getSystemTempDir() {
const char *temp_dir = getenv("PASSENGER_TEMP_DIR");
if (temp_dir == NULL || *temp_dir == '\0') {
temp_dir = getenv("PASSENGER_TMPDIR");
if (temp_dir == NULL || *temp_dir == '\0') {
temp_dir = "/tmp";
}
}
return temp_dir;
}
void
makeDirTree(const string &path, const StaticString &mode, uid_t owner, gid_t group) {
struct stat buf;
vector<string> paths;
vector<string>::reverse_iterator rit;
string current = path;
mode_t modeBits;
int ret;
if (stat(path.c_str(), &buf) == 0) {
return;
}
modeBits = parseModeString(mode);
/* Create a list of parent paths that don't exist. For example, given
* path == "/a/b/c/d/e" and that only /a exists, the list will become
* as follows:
*
* /a/b/c/d
* /a/b/c
* /a/b
*/
while (current != "/" && current != "." && getFileType(current) == FT_NONEXISTANT) {
paths.push_back(current);
current = extractDirName(current);
}
/* Now traverse the list in reverse order and create directories that don't exist. */
for (rit = paths.rbegin(); rit != paths.rend(); rit++) {
current = *rit;
do {
ret = mkdir(current.c_str(), modeBits);
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
if (errno == EEXIST) {
// Ignore error and don't chmod/chown.
continue;
} else {
int e = errno;
throw FileSystemException("Cannot create directory '" + current + "'",
e, current);
}
}
/* Chmod in order to override the umask. */
do {
ret = chmod(current.c_str(), modeBits);
} while (ret == -1 && errno == EINTR);
if (owner != USER_NOT_GIVEN && group != GROUP_NOT_GIVEN) {
if (owner == USER_NOT_GIVEN) {
owner = (uid_t) -1; // Don't let chown change file owner.
}
if (group == GROUP_NOT_GIVEN) {
group = (gid_t) -1; // Don't let chown change file group.
}
do {
ret = chown(current.c_str(), owner, group);
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
char message[1024];
int e = errno;
snprintf(message, sizeof(message) - 1,
"Cannot change the directory '%s' its UID to %lld and GID to %lld",
current.c_str(), (long long) owner, (long long) group);
message[sizeof(message) - 1] = '\0';
throw FileSystemException(message, e, path);
}
}
}
}
void
removeDirTree(const string &path) {
char command[PATH_MAX + 30];
int result;
snprintf(command, sizeof(command), "chmod -R u+rwx \"%s\" 2>/dev/null", path.c_str());
command[sizeof(command) - 1] = '\0';
do {
result = system(command);
} while (result == -1 && errno == EINTR);
snprintf(command, sizeof(command), "rm -rf \"%s\"", path.c_str());
command[sizeof(command) - 1] = '\0';
do {
result = system(command);
} while (result == -1 && errno == EINTR);
if (result == -1) {
char message[1024];
int e = errno;
snprintf(message, sizeof(message) - 1, "Cannot remove directory '%s'", path.c_str());
message[sizeof(message) - 1] = '\0';
throw FileSystemException(message, e, path);
}
}
bool
verifyRailsDir(const string &dir, CachedFileStat *cstat, unsigned int throttleRate) {
string temp(dir);
temp.append("/config/environment.rb");
return fileExists(temp, cstat, throttleRate);
}
bool
verifyRackDir(const string &dir, CachedFileStat *cstat, unsigned int throttleRate) {
string temp(dir);
temp.append("/config.ru");
return fileExists(temp, cstat, throttleRate);
}
bool
verifyWSGIDir(const string &dir, CachedFileStat *cstat, unsigned int throttleRate) {
string temp(dir);
temp.append("/passenger_wsgi.py");
return fileExists(temp, cstat, throttleRate);
}
void
prestartWebApps(const ResourceLocator &locator, const string &serializedprestartURLs) {
/* Apache calls the initialization routines twice during startup, and
* as a result it starts two helper servers, where the first one exits
* after a short idle period. We want any prespawning requests to reach
* the second helper server, so we sleep for a short period before
* executing the prespawning scripts.
*/
syscalls::sleep(2);
this_thread::disable_interruption di;
this_thread::disable_syscall_interruption dsi;
vector<string> prestartURLs;
vector<string>::const_iterator it;
string prespawnScript = locator.getHelperScriptsDir() + "/prespawn";
split(Base64::decode(serializedprestartURLs), '\0', prestartURLs);
it = prestartURLs.begin();
while (it != prestartURLs.end() && !this_thread::interruption_requested()) {
if (it->empty()) {
it++;
continue;
}
pid_t pid;
pid = fork();
if (pid == 0) {
long max_fds, i;
int e;
// Close all unnecessary file descriptors.
max_fds = sysconf(_SC_OPEN_MAX);
for (i = 3; i < max_fds; i++) {
syscalls::close(i);
}
execlp(prespawnScript.c_str(),
prespawnScript.c_str(),
it->c_str(),
(char *) 0);
e = errno;
fprintf(stderr, "Cannot execute '%s %s': %s (%d)\n",
prespawnScript.c_str(), it->c_str(),
strerror(e), e);
fflush(stderr);
_exit(1);
} else if (pid == -1) {
perror("fork()");
} else {
try {
this_thread::restore_interruption si(di);
this_thread::restore_syscall_interruption ssi(dsi);
syscalls::waitpid(pid, NULL, 0);
} catch (const thread_interrupted &) {
syscalls::kill(SIGKILL, pid);
syscalls::waitpid(pid, NULL, 0);
throw;
}
}
this_thread::restore_interruption si(di);
this_thread::restore_syscall_interruption ssi(dsi);
syscalls::sleep(1);
it++;
}
}
void
runAndPrintExceptions(const function<void ()> &func, bool toAbort) {
try {
func();
} catch (const boost::thread_interrupted &) {
throw;
} catch (const tracable_exception &e) {
P_ERROR("Exception: " << e.what() << "\n" << e.backtrace());
if (toAbort) {
abort();
}
}
}
void
runAndPrintExceptions(const function<void ()> &func) {
runAndPrintExceptions(func, true);
}
string
getHostName() {
long hostNameMax = HOST_NAME_MAX;
if (hostNameMax < 255) {
// https://bugzilla.redhat.com/show_bug.cgi?id=130733
hostNameMax = 255;
}
char hostname[hostNameMax + 1];
if (gethostname(hostname, hostNameMax) == 0) {
hostname[hostNameMax] = '\0';
return hostname;
} else {
int e = errno;
throw SystemException("Unable to query the system's host name", e);
}
}
string
getSignalName(int sig) {
switch (sig) {
case SIGHUP:
return "SIGHUP";
case SIGINT:
return "SIGINT";
case SIGQUIT:
return "SIGQUIT";
case SIGILL:
return "SIGILL";
case SIGTRAP:
return "SIGTRAP";
case SIGABRT:
return "SIGABRT";
case SIGFPE:
return "SIGFPE";
case SIGKILL:
return "SIGKILL";
case SIGBUS:
return "SIGBUS";
case SIGSEGV:
return "SIGSEGV";
case SIGPIPE:
return "SIGPIPE";
case SIGALRM:
return "SIGARLM";
case SIGTERM:
return "SIGTERM";
case SIGUSR1:
return "SIGUSR1";
case SIGUSR2:
return "SIGUSR2";
#ifdef SIGEMT
case SIGEMT:
return "SIGEMT";
#endif
#ifdef SIGINFO
case SIGINFO:
return "SIGINFO";
#endif
default:
return toString(sig);
}
}
void
resetSignalHandlersAndMask() {
sigset_t signal_set;
int ret;
sigemptyset(&signal_set);
do {
ret = sigprocmask(SIG_SETMASK, &signal_set, NULL);
} while (ret == -1 && errno == EINTR);
struct sigaction action;
action.sa_handler = SIG_DFL;
action.sa_flags = SA_RESTART;
sigemptyset(&action.sa_mask);
sigaction(SIGHUP, &action, NULL);
sigaction(SIGINT, &action, NULL);
sigaction(SIGQUIT, &action, NULL);
sigaction(SIGILL, &action, NULL);
sigaction(SIGTRAP, &action, NULL);
sigaction(SIGABRT, &action, NULL);
#ifdef SIGEMT
sigaction(SIGEMT, &action, NULL);
#endif
sigaction(SIGFPE, &action, NULL);
sigaction(SIGBUS, &action, NULL);
sigaction(SIGSEGV, &action, NULL);
sigaction(SIGSYS, &action, NULL);
sigaction(SIGPIPE, &action, NULL);
sigaction(SIGALRM, &action, NULL);
sigaction(SIGTERM, &action, NULL);
sigaction(SIGURG, &action, NULL);
sigaction(SIGSTOP, &action, NULL);
sigaction(SIGTSTP, &action, NULL);
sigaction(SIGCONT, &action, NULL);
sigaction(SIGCHLD, &action, NULL);
#ifdef SIGINFO
sigaction(SIGINFO, &action, NULL);
#endif
sigaction(SIGUSR1, &action, NULL);
sigaction(SIGUSR2, &action, NULL);
}
// Async-signal safe way to get the current process's hard file descriptor limit.
static int
getFileDescriptorLimit() {
long long sysconfResult = sysconf(_SC_OPEN_MAX);
struct rlimit rl;
long long rlimitResult;
if (getrlimit(RLIMIT_NOFILE, &rl) == -1) {
rlimitResult = 0;
} else {
rlimitResult = (long long) rl.rlim_max;
}
long result;
if (sysconfResult > rlimitResult) {
result = sysconfResult;
} else {
result = rlimitResult;
}
if (result < 0) {
// Both calls returned errors.
result = 9999;
} else if (result < 2) {
// The calls reported broken values.
result = 2;
}
return result;
}
// Async-signal safe function to get the highest file
// descriptor that the process is currently using.
// See also http://stackoverflow.com/questions/899038/getting-the-highest-allocated-file-descriptor
static int
getHighestFileDescriptor() {
#if defined(F_MAXFD)
int ret;
do {
ret = fcntl(0, F_MAXFD);
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
ret = getFileDescriptorLimit();
}
return ret;
#else
int p[2], ret, flags;
pid_t pid = -1;
int result = -1;
/* Since opendir() may not be async signal safe and thus may lock up
* or crash, we use it in a child process which we kill if we notice
* that things are going wrong.
*/
// Make a pipe.
p[0] = p[1] = -1;
do {
ret = pipe(p);
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
goto done;
}
// Make the read side non-blocking.
do {
flags = fcntl(p[0], F_GETFL);
} while (flags == -1 && errno == EINTR);
if (flags == -1) {
goto done;
}
do {
fcntl(p[0], F_SETFL, flags | O_NONBLOCK);
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
goto done;
}
do {
pid = fork();
} while (pid == -1 && errno == EINTR);
if (pid == 0) {
// Don't close p[0] here or it might affect the result.
resetSignalHandlersAndMask();
struct sigaction action;
action.sa_handler = _exit;
action.sa_flags = SA_RESTART;
sigemptyset(&action.sa_mask);
sigaction(SIGSEGV, &action, NULL);
sigaction(SIGPIPE, &action, NULL);
sigaction(SIGBUS, &action, NULL);
sigaction(SIGILL, &action, NULL);
sigaction(SIGFPE, &action, NULL);
sigaction(SIGABRT, &action, NULL);
DIR *dir = NULL;
#ifdef __APPLE__
/* /dev/fd can always be trusted on OS X. */
dir = opendir("/dev/fd");
#else
/* On FreeBSD and possibly other operating systems, /dev/fd only
* works if fdescfs is mounted. If it isn't mounted then /dev/fd
* still exists but always returns [0, 1, 2] and thus can't be
* trusted. If /dev and /dev/fd are on different filesystems
* then that probably means fdescfs is mounted.
*/
struct stat dirbuf1, dirbuf2;
if (stat("/dev", &dirbuf1) == -1
|| stat("/dev/fd", &dirbuf2) == -1) {
_exit(1);
}
if (dirbuf1.st_dev != dirbuf2.st_dev) {
dir = opendir("/dev/fd");
}
#endif
if (dir == NULL) {
dir = opendir("/proc/self/fd");
if (dir == NULL) {
_exit(1);
}
}
struct dirent *ent;
union {
int highest;
char data[sizeof(int)];
} u;
u.highest = -1;
while ((ent = readdir(dir)) != NULL) {
if (ent->d_name[0] != '.') {
int number = atoi(ent->d_name);
if (number > u.highest) {
u.highest = number;
}
}
}
if (u.highest != -1) {
ssize_t ret, written = 0;
do {
ret = write(p[1], u.data + written, sizeof(int) - written);
if (ret == -1) {
_exit(1);
}
written += ret;
} while (written < (ssize_t) sizeof(int));
}
closedir(dir);
_exit(0);
} else if (pid == -1) {
goto done;
} else {
close(p[1]); // Do not retry on EINTR: http://news.ycombinator.com/item?id=3363819
p[1] = -1;
union {
int highest;
char data[sizeof(int)];
} u;
ssize_t ret, bytesRead = 0;
struct pollfd pfd;
pfd.fd = p[0];
pfd.events = POLLIN;
do {
do {
// The child process must finish within 30 ms, otherwise
// we might as well query sysconf.
ret = poll(&pfd, 1, 30);
} while (ret == -1 && errno == EINTR);
if (ret <= 0) {
goto done;
}
do {
ret = read(p[0], u.data + bytesRead, sizeof(int) - bytesRead);
} while (ret == -1 && ret == EINTR);
if (ret == -1) {
if (errno != EAGAIN) {
goto done;
}
} else if (ret == 0) {
goto done;
} else {
bytesRead += ret;
}
} while (bytesRead < (ssize_t) sizeof(int));
result = u.highest;
goto done;
}
done:
// Do not retry on EINTR: http://news.ycombinator.com/item?id=3363819
if (p[0] != -1) {
close(p[0]);
}
if (p[1] != -1) {
close(p[1]);
}
if (pid != -1) {
do {
ret = kill(pid, SIGKILL);
} while (ret == -1 && errno == EINTR);
do {
ret = waitpid(pid, NULL, 0);
} while (ret == -1 && errno == EINTR);
}
if (result == -1) {
result = getFileDescriptorLimit();
}
return result;
#endif
}
void
closeAllFileDescriptors(int lastToKeepOpen) {
#if defined(F_CLOSEM)
int ret;
do {
ret = fcntl(lastToKeepOpen + 1, F_CLOSEM);
} while (ret == -1 && errno == EINTR);
if (ret != -1) {
return;
}
#elif defined(HAS_CLOSEFROM)
closefrom(lastToKeepOpen + 1);
return;
#endif
for (int i = getHighestFileDescriptor(); i > lastToKeepOpen; i--) {
/* Even though we normally shouldn't retry on EINTR
* (http://news.ycombinator.com/item?id=3363819)
* it's okay to do that here because because this function
* may only be called in a single-threaded environment.
*/
int ret;
do {
ret = close(i);
} while (ret == -1 && errno == EINTR);
}
}
} // namespace Passenger
|