compat.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * compat.c
  3. *
  4. * Compatibility functions for different OSes
  5. *
  6. * $Id: compat.c,v 1.6 2005/08/22 00:48:34 quozl Exp $
  7. */
  8. #if HAVE_CONFIG_H
  9. #include "config.h"
  10. #endif
  11. #include "compat.h"
  12. #ifndef HAVE_STRLCPY
  13. #include <string.h>
  14. #include <stdio.h>
  15. void strlcpy(char *dst, const char *src, size_t size)
  16. {
  17. strncpy(dst, src, size - 1);
  18. dst[size - 1] = '\0';
  19. }
  20. #endif
  21. #ifndef HAVE_MEMMOVE
  22. void *memmove(void *dst, const void *src, size_t size)
  23. {
  24. bcopy(src, dst, size);
  25. return dst;
  26. }
  27. #endif
  28. #ifndef HAVE_OPENPTY
  29. /*
  30. * Finds a free PTY/TTY pair.
  31. *
  32. * This is derived from C.S. Ananian's pty.c that was with his pptp client.
  33. *
  34. *************************************************************************
  35. * pty.c - find a free pty/tty pair.
  36. * inspired by the xterm source.
  37. * NOTE: This is very likely to be highly non-portable.
  38. * C. Scott Ananian <cananian@alumni.princeton.edu>
  39. *
  40. * Heavily modified to chage from getpseudopty() to openpty().
  41. */
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. #include <fcntl.h>
  45. #if HAVE_SYSLOG_H
  46. #include <syslog.h>
  47. #else
  48. #include "our_syslog.h"
  49. #endif
  50. int openpty(int *master, int *slave, char *name, void *unused1, void *unused2)
  51. {
  52. int devindex = 0, letter = 0;
  53. int fd1, fd2;
  54. char ttydev[PTYMAX], ptydev[TTYMAX];
  55. syslog(LOG_DEBUG, "CTRL: Allocating pty/tty pair");
  56. strcpy(ttydev, TTYDEV);
  57. strcpy(ptydev, PTYDEV);
  58. while (PTYCHAR1[letter]) {
  59. ttydev[TTYMAX - 3] = ptydev[PTYMAX - 3] = PTYCHAR1[letter];
  60. while (PTYCHAR2[devindex]) {
  61. ttydev[TTYMAX - 2] = ptydev[PTYMAX - 2] = PTYCHAR2[devindex];
  62. if ((fd1 = open(ptydev, O_RDWR)) >= 0) {
  63. if ((fd2 = open(ttydev, O_RDWR)) >= 0) {
  64. goto out;
  65. } else {
  66. close(fd1);
  67. }
  68. }
  69. devindex++;
  70. }
  71. devindex = 0;
  72. letter++;
  73. }
  74. syslog(LOG_ERR, "CTRL: Failed to allocate pty");
  75. return -1; /* Unable to allocate pty */
  76. out:
  77. syslog(LOG_INFO, "CTRL: Allocated pty/tty pair (%s,%s)", ptydev, ttydev);
  78. if (master)
  79. *master = fd1;
  80. if (slave)
  81. *slave = fd2;
  82. if (name)
  83. strcpy(name, ttydev); /* no way to bounds check */
  84. return 0;
  85. }
  86. #endif
  87. #ifndef HAVE_STRERROR
  88. char *strerror(int errnum) {
  89. static char buf[16];
  90. sprintf(buf, "Error %d", errnum);
  91. return buf;
  92. }
  93. #endif
  94. #ifndef HAVE_SETPROCTITLE
  95. #include "inststr.h"
  96. #endif
  97. #define __USE_BSD 1
  98. #include <stdarg.h>
  99. #include <stdio.h>
  100. void my_setproctitle(int argc, char **argv, const char *format, ...) {
  101. char proctitle[64];
  102. va_list parms;
  103. va_start(parms, format);
  104. vsnprintf(proctitle, sizeof(proctitle), format, parms);
  105. #ifndef HAVE_SETPROCTITLE
  106. inststr(argc, argv, proctitle);
  107. #else
  108. setproctitle(proctitle);
  109. #endif
  110. va_end(parms);
  111. }
  112. /* signal to pipe delivery implementation */
  113. #include <unistd.h>
  114. #include <fcntl.h>
  115. #include <signal.h>
  116. /* pipe private to process */
  117. static int sigpipe[2];
  118. /* create a signal pipe, returns 0 for success, -1 with errno for failure */
  119. int sigpipe_create()
  120. {
  121. int rc;
  122. rc = pipe(sigpipe);
  123. if (rc < 0) return rc;
  124. fcntl(sigpipe[0], F_SETFD, FD_CLOEXEC);
  125. fcntl(sigpipe[1], F_SETFD, FD_CLOEXEC);
  126. #ifdef O_NONBLOCK
  127. #define FLAG_TO_SET O_NONBLOCK
  128. #else
  129. #ifdef SYSV
  130. #define FLAG_TO_SET O_NDELAY
  131. #else /* BSD */
  132. #define FLAG_TO_SET FNDELAY
  133. #endif
  134. #endif
  135. rc = fcntl(sigpipe[1], F_GETFL);
  136. if (rc != -1)
  137. rc = fcntl(sigpipe[1], F_SETFL, rc | FLAG_TO_SET);
  138. if (rc < 0) return rc;
  139. return 0;
  140. #undef FLAG_TO_SET
  141. }
  142. /* generic handler for signals, writes signal number to pipe */
  143. void sigpipe_handler(int signum)
  144. {
  145. write(sigpipe[1], &signum, sizeof(signum));
  146. signal(signum, sigpipe_handler);
  147. }
  148. /* assign a signal number to the pipe */
  149. void sigpipe_assign(int signum)
  150. {
  151. struct sigaction sa;
  152. memset(&sa, 0, sizeof(sa));
  153. sa.sa_handler = sigpipe_handler;
  154. sigaction(signum, &sa, NULL);
  155. }
  156. /* return the signal pipe read file descriptor for select(2) */
  157. int sigpipe_fd()
  158. {
  159. return sigpipe[0];
  160. }
  161. /* read and return the pending signal from the pipe */
  162. int sigpipe_read()
  163. {
  164. int signum;
  165. read(sigpipe[0], &signum, sizeof(signum));
  166. return signum;
  167. }
  168. void sigpipe_close()
  169. {
  170. close(sigpipe[0]);
  171. close(sigpipe[1]);
  172. }