transmission/third-party/shttpd/compat_unix.c

134 lines
2.4 KiB
C
Raw Normal View History

/*
* 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.
*/
#ifndef _WIN32
#include "defs.h"
void
2008-08-25 19:32:06 +00:00
_shttpd_set_close_on_exec(int fd)
{
(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
}
int
2008-08-25 19:32:06 +00:00
_shttpd_stat(const char *path, struct stat *stp)
{
return (stat(path, stp));
}
int
2008-08-25 19:32:06 +00:00
_shttpd_open(const char *path, int flags, int mode)
{
return (open(path, flags, mode));
}
int
2008-08-25 19:32:06 +00:00
_shttpd_remove(const char *path)
{
return (remove(path));
}
int
2008-08-25 19:32:06 +00:00
_shttpd_rename(const char *path1, const char *path2)
{
return (rename(path1, path2));
}
int
2008-08-25 19:32:06 +00:00
_shttpd_mkdir(const char *path, int mode)
{
return (mkdir(path, mode));
}
char *
2008-08-25 19:32:06 +00:00
_shttpd_getcwd(char *buffer, int maxlen)
{
return (getcwd(buffer, maxlen));
}
int
2008-08-25 19:32:06 +00:00
_shttpd_set_non_blocking_mode(int fd)
{
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,
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));
} 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);
} 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)",
interp, prog);
}
/* UNREACHED */
exit(EXIT_FAILURE);
} else {
/* Parent */
ret = 0;
(void) closesocket(sock);
}
return (ret);
}
#endif /* !NO_CGI */
#endif /* !_WIN32 */