Portable build framework for OpenNTPD
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.5 KiB

  1. /* Based on conf.c from UCB sendmail 8.8.8 */
  2. /*
  3. * Copyright 2003 Damien Miller
  4. * Copyright (c) 1983, 1995-1997 Eric P. Allman
  5. * Copyright (c) 1988, 1993
  6. * The Regents of the University of California. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the University nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. */
  32. #ifndef HAVE_SETPROCTITLE
  33. #include <stdarg.h>
  34. #include <stdlib.h>
  35. #include <unistd.h>
  36. #ifdef HAVE_SYS_PSTAT_H
  37. #include <sys/pstat.h>
  38. #endif
  39. #include <string.h>
  40. #define SPT_NONE 0 /* don't use it at all */
  41. #define SPT_PSTAT 1 /* use pstat(PSTAT_SETCMD, ...) */
  42. #define SPT_REUSEARGV 2 /* cover argv with title information */
  43. #ifndef SPT_TYPE
  44. # define SPT_TYPE SPT_NONE
  45. #endif
  46. #ifndef SPT_PADCHAR
  47. # define SPT_PADCHAR '\0'
  48. #endif
  49. #if SPT_TYPE == SPT_REUSEARGV
  50. static char *argv_start = NULL;
  51. static size_t argv_env_len = 0;
  52. #endif
  53. #endif /* HAVE_SETPROCTITLE */
  54. void
  55. compat_init_setproctitle(int argc, char *argv[])
  56. {
  57. #if !defined(HAVE_SETPROCTITLE) && \
  58. defined(SPT_TYPE) && SPT_TYPE == SPT_REUSEARGV
  59. extern char **environ;
  60. char *lastargv = NULL;
  61. char **envp = environ;
  62. int i;
  63. /*
  64. * NB: This assumes that argv has already been copied out of the
  65. * way. This is true for sshd, but may not be true for other
  66. * programs. Beware.
  67. */
  68. if (argc == 0 || argv[0] == NULL)
  69. return;
  70. /* Fail if we can't allocate room for the new environment */
  71. for (i = 0; envp[i] != NULL; i++)
  72. ;
  73. if ((environ = calloc(i + 1, sizeof(*environ))) == NULL) {
  74. environ = envp; /* put it back */
  75. return;
  76. }
  77. /*
  78. * Find the last argv string or environment variable within
  79. * our process memory area.
  80. */
  81. for (i = 0; i < argc; i++) {
  82. if (lastargv == NULL || lastargv + 1 == argv[i])
  83. lastargv = argv[i] + strlen(argv[i]);
  84. }
  85. for (i = 0; envp[i] != NULL; i++) {
  86. if (lastargv + 1 == envp[i])
  87. lastargv = envp[i] + strlen(envp[i]);
  88. }
  89. argv[1] = NULL;
  90. argv_start = argv[0];
  91. argv_env_len = lastargv - argv[0] - 1;
  92. /*
  93. * Copy environment
  94. * XXX - will truncate env on strdup fail
  95. */
  96. for (i = 0; envp[i] != NULL; i++)
  97. environ[i] = strdup(envp[i]);
  98. environ[i] = NULL;
  99. #endif /* SPT_REUSEARGV */
  100. }
  101. #ifndef HAVE_SETPROCTITLE
  102. void
  103. setproctitle(const char *fmt, ...)
  104. {
  105. #if SPT_TYPE != SPT_NONE
  106. va_list ap;
  107. char buf[1024], ptitle[1024];
  108. size_t len;
  109. int r;
  110. extern char *__progname;
  111. #if SPT_TYPE == SPT_PSTAT
  112. union pstun pst;
  113. #endif
  114. #if SPT_TYPE == SPT_REUSEARGV
  115. if (argv_env_len <= 0)
  116. return;
  117. #endif
  118. strlcpy(buf, __progname, sizeof(buf));
  119. r = -1;
  120. va_start(ap, fmt);
  121. if (fmt != NULL) {
  122. len = strlcat(buf, ": ", sizeof(buf));
  123. if (len < sizeof(buf))
  124. r = vsnprintf(buf + len, sizeof(buf) - len , fmt, ap);
  125. }
  126. va_end(ap);
  127. if (r == -1 || (size_t)r >= sizeof(buf) - len)
  128. return;
  129. strnvis(ptitle, buf, sizeof(ptitle),
  130. VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL);
  131. #if SPT_TYPE == SPT_PSTAT
  132. pst.pst_command = ptitle;
  133. pstat(PSTAT_SETCMD, pst, strlen(ptitle), 0, 0);
  134. #elif SPT_TYPE == SPT_REUSEARGV
  135. /* debug("setproctitle: copy \"%s\" into len %d",
  136. buf, argv_env_len); */
  137. len = strlcpy(argv_start, ptitle, argv_env_len);
  138. for(; len < argv_env_len; len++)
  139. argv_start[len] = SPT_PADCHAR;
  140. #endif
  141. #endif /* SPT_NONE */
  142. }
  143. #endif /* HAVE_SETPROCTITLE */