compat_unix.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
  3. * All rights reserved
  4. *
  5. * "THE BEER-WARE LICENSE" (Revision 42):
  6. * Sergey Lyubka wrote this file. As long as you retain this notice you
  7. * can do whatever you want with this stuff. If we meet some day, and you think
  8. * this stuff is worth it, you can buy me a beer in return.
  9. */
  10. #include "defs.h"
  11. void
  12. _shttpd_set_close_on_exec(int fd)
  13. {
  14. (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
  15. }
  16. int
  17. _shttpd_stat(const char *path, struct stat *stp)
  18. {
  19. return (stat(path, stp));
  20. }
  21. int
  22. _shttpd_open(const char *path, int flags, int mode)
  23. {
  24. return (open(path, flags, mode));
  25. }
  26. int
  27. _shttpd_remove(const char *path)
  28. {
  29. return (remove(path));
  30. }
  31. int
  32. _shttpd_rename(const char *path1, const char *path2)
  33. {
  34. return (rename(path1, path2));
  35. }
  36. int
  37. _shttpd_mkdir(const char *path, int mode)
  38. {
  39. return (mkdir(path, mode));
  40. }
  41. char *
  42. _shttpd_getcwd(char *buffer, int maxlen)
  43. {
  44. return (getcwd(buffer, maxlen));
  45. }
  46. int
  47. _shttpd_set_non_blocking_mode(int fd)
  48. {
  49. int ret = -1;
  50. int flags;
  51. if ((flags = fcntl(fd, F_GETFL, 0)) == -1) {
  52. DBG(("nonblock: fcntl(F_GETFL): %d", ERRNO));
  53. } else if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) != 0) {
  54. DBG(("nonblock: fcntl(F_SETFL): %d", ERRNO));
  55. } else {
  56. ret = 0; /* Success */
  57. }
  58. return (ret);
  59. }
  60. #ifndef NO_CGI
  61. int
  62. _shttpd_spawn_process(struct conn *c, const char *prog, char *envblk,
  63. char *envp[], int sock, const char *dir)
  64. {
  65. int ret;
  66. pid_t pid;
  67. const char *p, *interp = c->ctx->options[OPT_CGI_INTERPRETER];
  68. envblk = NULL; /* unused */
  69. if ((pid = vfork()) == -1) {
  70. ret = -1;
  71. _shttpd_elog(E_LOG, c, "redirect: fork: %s", strerror(errno));
  72. } else if (pid == 0) {
  73. /* Child */
  74. (void) chdir(dir);
  75. (void) dup2(sock, 0);
  76. (void) dup2(sock, 1);
  77. (void) closesocket(sock);
  78. /* If error file is specified, send errors there */
  79. if (c->ctx->error_log)
  80. (void) dup2(fileno(c->ctx->error_log), 2);
  81. if ((p = strrchr(prog, '/')) != NULL)
  82. p++;
  83. else
  84. p = prog;
  85. /* Execute CGI program */
  86. if (interp == NULL) {
  87. (void) execle(p, p, NULL, envp);
  88. _shttpd_elog(E_FATAL, c, "redirect: exec(%s)", prog);
  89. } else {
  90. (void) execle(interp, interp, p, NULL, envp);
  91. _shttpd_elog(E_FATAL, c, "redirect: exec(%s %s)",
  92. interp, prog);
  93. }
  94. /* UNREACHED */
  95. exit(EXIT_FAILURE);
  96. } else {
  97. /* Parent */
  98. ret = 0;
  99. (void) closesocket(sock);
  100. }
  101. return (ret);
  102. }
  103. #endif /* !NO_CGI */