Module: PhusionPassenger::NativeSupport
- Defined in:
- ext/ruby/passenger_native_support.c
Defined Under Namespace
Classes: FileSystemWatcher
Constant Summary collapse
- UNIX_PATH_MAX =
The maximum length of a Unix socket path, including terminating null.
INT2NUM(sizeof(addr.sun_path))
- SSIZE_MAX =
The maximum size of the data that may be passed to #writev.
LL2NUM(SSIZE_MAX)
Class Method Summary collapse
-
.close_all_file_descriptors(exceptions) ⇒ Object
Close all file descriptors, except those given in the
exceptions
array. -
.create_unix_socket(filename, backlog) ⇒ Object
Create a SOCK_STREAM server Unix socket.
-
.disable_stdio_buffering ⇒ Object
Disables any kind of buffering on the C
stdout
andstderr
variables, so that fprintf() onstdout
andstderr
have immediate effect. - .process_times ⇒ Object
-
.recv_fd(socket_fd) ⇒ Object
Receive a file descriptor from the given Unix socket.
-
.send_fd(socket_fd, fd_to_send) ⇒ Object
Send a file descriptor over the given Unix socket.
-
.split_by_null_into_hash(data) ⇒ Object
Split the given string into an hash.
-
.switch_user(username, uid, gid) ⇒ Object
Ruby’s implementations of initgroups, setgid and setuid are broken various ways, sigh…
-
.writev(fd, components) ⇒ Object
Writes all of the strings in the
components
array into the given file descriptor using the writev() system call. -
.writev2(fd, components1, components2) ⇒ Object
Like #writev, but accepts two arrays.
-
.writev3(fd, components1, components2, components3) ⇒ Object
Like #writev, but accepts three arrays.
Class Method Details
.close_all_file_descriptors(exceptions) ⇒ Object
Close all file descriptors, except those given in the exceptions
array. For example, the following would close all file descriptors except standard input (0) and standard output (1).
close_all_file_descriptors([0, 1])
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'ext/ruby/passenger_native_support.c', line 273
static VALUE
close_all_file_descriptors(VALUE self, VALUE exceptions) {
long i, j;
for (i = sysconf(_SC_OPEN_MAX) - 1; i >= 0; i--) {
int is_exception = 0;
for (j = 0; j < RARRAY_LEN(exceptions) && !is_exception; j++) {
long fd = NUM2INT(rb_ary_entry(exceptions, j));
is_exception = i == fd;
}
if (!is_exception) {
close((int) i);
}
}
return Qnil;
}
|
.create_unix_socket(filename, backlog) ⇒ Object
Create a SOCK_STREAM server Unix socket. Unlike Ruby’s UNIXServer class, this function is also able to create Unix sockets on the abstract namespace by prepending the filename with a null byte.
-
filename
(string): The filename of the Unix socket to create. -
backlog
(integer): The backlog to use for listening on the socket. -
Returns: The file descriptor of the created Unix socket, as an integer.
-
Raises
SystemCallError
if something went wrong.
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 |
# File 'ext/ruby/passenger_native_support.c', line 223
static VALUE
create_unix_socket(VALUE self, VALUE filename, VALUE backlog) {
int fd, ret;
struct sockaddr_un addr;
const char *filename_str;
long filename_length;
filename_str = RSTRING_PTR(filename);
filename_length = RSTRING_LEN(filename);
fd = socket(PF_UNIX, SOCK_STREAM, 0);
if (fd == -1) {
rb_sys_fail("Cannot create a Unix socket");
return Qnil;
}
addr.sun_family = AF_UNIX;
memcpy(addr.sun_path, filename_str,
MIN((long) filename_length, (long) sizeof(addr.sun_path)));
addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
ret = bind(fd, (const struct sockaddr *) &addr, sizeof(addr));
if (ret == -1) {
int e = errno;
close(fd);
errno = e;
rb_sys_fail("Cannot bind Unix socket");
return Qnil;
}
ret = listen(fd, NUM2INT(backlog));
if (ret == -1) {
int e = errno;
close(fd);
errno = e;
rb_sys_fail("Cannot listen on Unix socket");
return Qnil;
}
return INT2NUM(fd);
}
|
.disable_stdio_buffering ⇒ Object
Disables any kind of buffering on the C stdout
and stderr
variables, so that fprintf() on stdout
and stderr
have immediate effect.
296 297 298 299 300 301 |
# File 'ext/ruby/passenger_native_support.c', line 296 static VALUE disable_stdio_buffering(VALUE self) { setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); return Qnil; } |
.process_times ⇒ Object
609 610 611 612 613 614 615 616 617 618 619 620 621 |
# File 'ext/ruby/passenger_native_support.c', line 609
static VALUE
process_times(VALUE self) {
struct rusage usage;
unsigned long long utime, stime;
if (getrusage(RUSAGE_SELF, &usage) == -1) {
rb_sys_fail("getrusage()");
}
utime = (unsigned long long) usage.ru_utime.tv_sec * 1000000 + usage.ru_utime.tv_usec;
stime = (unsigned long long) usage.ru_stime.tv_sec * 1000000 + usage.ru_stime.tv_usec;
return rb_struct_new(S_ProcessTimes, rb_ull2inum(utime), rb_ull2inum(stime));
}
|
.recv_fd(socket_fd) ⇒ Object
Receive a file descriptor from the given Unix socket. Returns the received file descriptor as an integer. Raises SystemCallError
if something went wrong.
You do not have call this method directly. A convenience wrapper is provided by IO#recv_io.
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 |
# File 'ext/ruby/passenger_native_support.c', line 156
static VALUE
recv_fd(VALUE self, VALUE socket_fd) {
struct msghdr msg;
struct iovec vec;
char dummy[1];
#if defined(__APPLE__) || defined(__SOLARIS__) || defined(__arm__)
// File descriptor passing macros (CMSG_*) seem to be broken
// on 64-bit MacOS X. This structure works around the problem.
struct {
struct cmsghdr header;
int fd;
} control_data;
#define EXPECTED_CMSG_LEN sizeof(control_data)
#else
char control_data[CMSG_SPACE(sizeof(int))];
#define EXPECTED_CMSG_LEN CMSG_LEN(sizeof(int))
#endif
struct cmsghdr *control_header;
msg.msg_name = NULL;
msg.msg_namelen = 0;
dummy[0] = '\0';
vec.iov_base = dummy;
vec.iov_len = sizeof(dummy);
msg.msg_iov = &vec;
msg.msg_iovlen = 1;
msg.msg_control = (caddr_t) &control_data;
msg.msg_controllen = sizeof(control_data);
msg.msg_flags = 0;
if (recvmsg(NUM2INT(socket_fd), &msg, 0) == -1) {
rb_sys_fail("Cannot read file descriptor with recvmsg()");
return Qnil;
}
control_header = CMSG_FIRSTHDR(&msg);
if (control_header == NULL) {
rb_raise(rb_eIOError, "No valid file descriptor received.");
return Qnil;
}
if (control_header->cmsg_len != EXPECTED_CMSG_LEN
|| control_header->cmsg_level != SOL_SOCKET
|| control_header->cmsg_type != SCM_RIGHTS) {
rb_raise(rb_eIOError, "No valid file descriptor received.");
return Qnil;
}
#if defined(__APPLE__) || defined(__SOLARIS__) || defined(__arm__)
return INT2NUM(control_data.fd);
#else
return INT2NUM(*((int *) CMSG_DATA(control_header)));
#endif
}
|
.send_fd(socket_fd, fd_to_send) ⇒ Object
Send a file descriptor over the given Unix socket. You do not have to call this function directly. A convenience wrapper is provided by IO#send_io.
-
socket_fd
(integer): The file descriptor of the socket. -
fd_to_send
(integer): The file descriptor to send. -
Raises
SystemCallError
if something went wrong.
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 |
# File 'ext/ruby/passenger_native_support.c', line 96
static VALUE
send_fd(VALUE self, VALUE socket_fd, VALUE fd_to_send) {
struct msghdr msg;
struct iovec vec;
char dummy[1];
#if defined(__APPLE__) || defined(__SOLARIS__) || defined(__arm__)
struct {
struct cmsghdr header;
int fd;
} control_data;
#else
char control_data[CMSG_SPACE(sizeof(int))];
#endif
struct cmsghdr *control_header;
int control_payload;
msg.msg_name = NULL;
msg.msg_namelen = 0;
/* Linux and Solaris require msg_iov to be non-NULL. */
dummy[0] = '\0';
vec.iov_base = dummy;
vec.iov_len = sizeof(dummy);
msg.msg_iov = &vec;
msg.msg_iovlen = 1;
msg.msg_control = (caddr_t) &control_data;
msg.msg_controllen = sizeof(control_data);
msg.msg_flags = 0;
control_header = CMSG_FIRSTHDR(&msg);
control_header->cmsg_level = SOL_SOCKET;
control_header->cmsg_type = SCM_RIGHTS;
control_payload = NUM2INT(fd_to_send);
#if defined(__APPLE__) || defined(__SOLARIS__) || defined(__arm__)
control_header->cmsg_len = sizeof(control_data);
control_data.fd = control_payload;
#else
control_header->cmsg_len = CMSG_LEN(sizeof(int));
memcpy(CMSG_DATA(control_header), &control_payload, sizeof(int));
#endif
if (sendmsg(NUM2INT(socket_fd), &msg, 0) == -1) {
rb_sys_fail("sendmsg(2)");
return Qnil;
}
return Qnil;
}
|
.split_by_null_into_hash(data) ⇒ Object
Split the given string into an hash. Keys and values are obtained by splitting the string using the null character as the delimitor.
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 |
# File 'ext/ruby/passenger_native_support.c', line 307
static VALUE
split_by_null_into_hash(VALUE self, VALUE data) {
const char *cdata = RSTRING_PTR(data);
unsigned long len = RSTRING_LEN(data);
const char *begin = cdata;
const char *current = cdata;
const char *end = cdata + len;
VALUE result, key, value;
result = rb_hash_new();
while (current < end) {
if (*current == '\0') {
key = rb_str_substr(data, begin - cdata, current - begin);
begin = current = current + 1;
while (current < end) {
if (*current == '\0') {
value = rb_str_substr(data, begin - cdata, current - begin);;
begin = current = current + 1;
rb_hash_aset(result, key, value);
break;
} else {
current++;
}
}
} else {
current++;
}
}
return result;
}
|
.switch_user(username, uid, gid) ⇒ Object
Ruby’s implementations of initgroups, setgid and setuid are broken various ways, sigh… Ruby’s setgid and setuid can’t handle negative UIDs and initgroups is just broken. Work around it by using our own implementation.
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 |
# File 'ext/ruby/passenger_native_support.c', line 592
static VALUE
switch_user(VALUE self, VALUE username, VALUE uid, VALUE gid) {
uid_t the_uid = (uid_t) NUM2LL(uid);
gid_t the_gid = (gid_t) NUM2LL(gid);
if (initgroups(RSTRING_PTR(username), the_gid) == -1) {
rb_sys_fail("initgroups");
}
if (setgid(the_gid) == -1) {
rb_sys_fail("setgid");
}
if (setuid(the_uid) == -1) {
rb_sys_fail("setuid");
}
return Qnil;
}
|
.writev(fd, components) ⇒ Object
Writes all of the strings in the components
array into the given file descriptor using the writev() system call. Unlike IO#write, this method does not require one to concatenate all those strings into a single buffer in order to send the data in a single system call. Thus, #writev is a great way to perform zero-copy I/O.
Unlike the raw writev() system call, this method ensures that all given data is written before returning, by performing multiple writev() calls and whatever else is necessary.
writev(@socket.fileno, ["hello ", "world", "\n"])
556 557 558 559 |
# File 'ext/ruby/passenger_native_support.c', line 556
static VALUE
f_writev(VALUE self, VALUE fd, VALUE components) {
return f_generic_writev(fd, &components, 1);
}
|
.writev2(fd, components1, components2) ⇒ Object
Like #writev, but accepts two arrays. The data is written in the given order.
writev2(@socket.fileno, ["hello ", "world", "\n"], ["another ", "message\n"])
566 567 568 569 570 |
# File 'ext/ruby/passenger_native_support.c', line 566
static VALUE
f_writev2(VALUE self, VALUE fd, VALUE components1, VALUE components2) {
VALUE array_of_components[2] = { components1, components2 };
return f_generic_writev(fd, array_of_components, 2);
}
|
.writev3(fd, components1, components2, components3) ⇒ Object
Like #writev, but accepts three arrays. The data is written in the given order.
writev3(@socket.fileno,
["hello ", "world", "\n"],
["another ", "message\n"],
["yet ", "another ", "one", "\n"])
580 581 582 583 584 |
# File 'ext/ruby/passenger_native_support.c', line 580
static VALUE
f_writev3(VALUE self, VALUE fd, VALUE components1, VALUE components2, VALUE components3) {
VALUE array_of_components[3] = { components1, components2, components3 };
return f_generic_writev(fd, array_of_components, 3);
}
|