2008-05-12 18:33:52 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
|
|
|
|
* All rights reserved
|
|
|
|
*
|
|
|
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
|
|
|
* Sergey Lyubka wrote this file. As long as you retain this notice you
|
|
|
|
* can do whatever you want with this stuff. If we meet some day, and you think
|
|
|
|
* this stuff is worth it, you can buy me a beer in return.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
void
|
2008-08-25 19:32:06 +00:00
|
|
|
_shttpd_set_close_on_exec(int fd)
|
2008-05-12 18:33:52 +00:00
|
|
|
{
|
|
|
|
(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-08-25 19:32:06 +00:00
|
|
|
_shttpd_stat(const char *path, struct stat *stp)
|
2008-05-12 18:33:52 +00:00
|
|
|
{
|
|
|
|
return (stat(path, stp));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-08-25 19:32:06 +00:00
|
|
|
_shttpd_open(const char *path, int flags, int mode)
|
2008-05-12 18:33:52 +00:00
|
|
|
{
|
|
|
|
return (open(path, flags, mode));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-08-25 19:32:06 +00:00
|
|
|
_shttpd_remove(const char *path)
|
2008-05-12 18:33:52 +00:00
|
|
|
{
|
|
|
|
return (remove(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-08-25 19:32:06 +00:00
|
|
|
_shttpd_rename(const char *path1, const char *path2)
|
2008-05-12 18:33:52 +00:00
|
|
|
{
|
|
|
|
return (rename(path1, path2));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-08-25 19:32:06 +00:00
|
|
|
_shttpd_mkdir(const char *path, int mode)
|
2008-05-12 18:33:52 +00:00
|
|
|
{
|
|
|
|
return (mkdir(path, mode));
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2008-08-25 19:32:06 +00:00
|
|
|
_shttpd_getcwd(char *buffer, int maxlen)
|
2008-05-12 18:33:52 +00:00
|
|
|
{
|
|
|
|
return (getcwd(buffer, maxlen));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-08-25 19:32:06 +00:00
|
|
|
_shttpd_set_non_blocking_mode(int fd)
|
2008-05-12 18:33:52 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
if ((flags = fcntl(fd, F_GETFL, 0)) == -1) {
|
|
|
|
DBG(("nonblock: fcntl(F_GETFL): %d", ERRNO));
|
|
|
|
} else if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) != 0) {
|
|
|
|
DBG(("nonblock: fcntl(F_SETFL): %d", ERRNO));
|
|
|
|
} else {
|
|
|
|
ret = 0; /* Success */
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NO_CGI
|
|
|
|
int
|
2008-08-25 19:32:06 +00:00
|
|
|
_shttpd_spawn_process(struct conn *c, const char *prog, char *envblk,
|
2008-05-12 18:33:52 +00:00
|
|
|
char *envp[], int sock, const char *dir)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
pid_t pid;
|
|
|
|
const char *p, *interp = c->ctx->options[OPT_CGI_INTERPRETER];
|
|
|
|
|
|
|
|
envblk = NULL; /* unused */
|
|
|
|
|
|
|
|
if ((pid = vfork()) == -1) {
|
|
|
|
|
|
|
|
ret = -1;
|
2008-08-25 19:32:06 +00:00
|
|
|
_shttpd_elog(E_LOG, c, "redirect: fork: %s", strerror(errno));
|
2008-05-12 18:33:52 +00:00
|
|
|
|
|
|
|
} else if (pid == 0) {
|
|
|
|
|
|
|
|
/* Child */
|
|
|
|
|
|
|
|
(void) chdir(dir);
|
|
|
|
(void) dup2(sock, 0);
|
|
|
|
(void) dup2(sock, 1);
|
|
|
|
(void) closesocket(sock);
|
|
|
|
|
|
|
|
/* If error file is specified, send errors there */
|
|
|
|
if (c->ctx->error_log)
|
|
|
|
(void) dup2(fileno(c->ctx->error_log), 2);
|
|
|
|
|
|
|
|
if ((p = strrchr(prog, '/')) != NULL)
|
|
|
|
p++;
|
|
|
|
else
|
|
|
|
p = prog;
|
|
|
|
|
|
|
|
/* Execute CGI program */
|
|
|
|
if (interp == NULL) {
|
|
|
|
(void) execle(p, p, NULL, envp);
|
2008-08-25 19:32:06 +00:00
|
|
|
_shttpd_elog(E_FATAL, c, "redirect: exec(%s)", prog);
|
2008-05-12 18:33:52 +00:00
|
|
|
} else {
|
|
|
|
(void) execle(interp, interp, p, NULL, envp);
|
2008-08-25 19:32:06 +00:00
|
|
|
_shttpd_elog(E_FATAL, c, "redirect: exec(%s %s)",
|
2008-05-12 18:33:52 +00:00
|
|
|
interp, prog);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* UNREACHED */
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* Parent */
|
|
|
|
ret = 0;
|
|
|
|
(void) closesocket(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
#endif /* !NO_CGI */
|