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.

164 lines
4.4 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 <stdio.h>
  34. #include <stdarg.h>
  35. #include <stdlib.h>
  36. #include <unistd.h>
  37. #ifdef HAVE_SYS_PSTAT_H
  38. #include <sys/pstat.h>
  39. #endif
  40. #include <string.h>
  41. #define SPT_NONE 0 /* don't use it at all */
  42. #define SPT_PSTAT 1 /* use pstat(PSTAT_SETCMD, ...) */
  43. #define SPT_REUSEARGV 2 /* cover argv with title information */
  44. #ifndef SPT_TYPE
  45. # define SPT_TYPE SPT_NONE
  46. #endif
  47. #ifndef SPT_PADCHAR
  48. # define SPT_PADCHAR '\0'
  49. #endif
  50. #if SPT_TYPE == SPT_REUSEARGV
  51. static char *argv_start = NULL;
  52. static size_t argv_env_len = 0;
  53. #endif
  54. #endif /* HAVE_SETPROCTITLE */
  55. void
  56. compat_init_setproctitle(int argc, char *argv[])
  57. {
  58. #if !defined(HAVE_SETPROCTITLE) && \
  59. defined(SPT_TYPE) && SPT_TYPE == SPT_REUSEARGV
  60. extern char **environ;
  61. char *lastargv = NULL;
  62. char **envp = environ;
  63. int i;
  64. /*
  65. * NB: This assumes that argv has already been copied out of the
  66. * way. This is true for sshd, but may not be true for other
  67. * programs. Beware.
  68. */
  69. if (argc == 0 || argv[0] == NULL)
  70. return;
  71. /* Fail if we can't allocate room for the new environment */
  72. for (i = 0; envp[i] != NULL; i++)
  73. ;
  74. if ((environ = calloc(i + 1, sizeof(*environ))) == NULL) {
  75. environ = envp; /* put it back */
  76. return;
  77. }
  78. /*
  79. * Find the last argv string or environment variable within
  80. * our process memory area.
  81. */
  82. for (i = 0; i < argc; i++) {
  83. if (lastargv == NULL || lastargv + 1 == argv[i])
  84. lastargv = argv[i] + strlen(argv[i]);
  85. }
  86. for (i = 0; envp[i] != NULL; i++) {
  87. if (lastargv + 1 == envp[i])
  88. lastargv = envp[i] + strlen(envp[i]);
  89. }
  90. argv[1] = NULL;
  91. argv_start = argv[0];
  92. argv_env_len = lastargv - argv[0] - 1;
  93. /*
  94. * Copy environment
  95. * XXX - will truncate env on strdup fail
  96. */
  97. for (i = 0; envp[i] != NULL; i++)
  98. environ[i] = strdup(envp[i]);
  99. environ[i] = NULL;
  100. #endif /* SPT_REUSEARGV */
  101. }
  102. #ifndef HAVE_SETPROCTITLE
  103. void
  104. setproctitle(const char *fmt, ...)
  105. {
  106. #if SPT_TYPE != SPT_NONE
  107. va_list ap;
  108. char buf[1024];
  109. size_t len;
  110. int r;
  111. extern char *__progname;
  112. #if SPT_TYPE == SPT_PSTAT
  113. union pstun pst;
  114. #endif
  115. #if SPT_TYPE == SPT_REUSEARGV
  116. if (argv_env_len <= 0)
  117. return;
  118. #endif
  119. strlcpy(buf, __progname, sizeof(buf));
  120. r = -1;
  121. va_start(ap, fmt);
  122. if (fmt != NULL) {
  123. len = strlcat(buf, ": ", sizeof(buf));
  124. if (len < sizeof(buf))
  125. r = vsnprintf(buf + len, sizeof(buf) - len , fmt, ap);
  126. }
  127. va_end(ap);
  128. if (r == -1 || (size_t)r >= sizeof(buf) - len)
  129. return;
  130. #if SPT_TYPE == SPT_PSTAT
  131. pst.pst_command = buf;
  132. pstat(PSTAT_SETCMD, pst, strlen(buf), 0, 0);
  133. #elif SPT_TYPE == SPT_REUSEARGV
  134. /* debug("setproctitle: copy \"%s\" into len %d",
  135. buf, argv_env_len); */
  136. len = strlcpy(argv_start, buf, argv_env_len);
  137. for(; len < argv_env_len; len++)
  138. argv_start[len] = SPT_PADCHAR;
  139. #endif
  140. #endif /* SPT_NONE */
  141. }
  142. #endif /* HAVE_SETPROCTITLE */