test-helpers.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright © 2012 Collabora, Ltd.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the
  13. * next paragraph) shall be included in all copies or substantial
  14. * portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. #include "config.h"
  26. #include <assert.h>
  27. #include <errno.h>
  28. #include <dirent.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <unistd.h>
  32. #include <time.h>
  33. #include <sys/time.h>
  34. #include <sys/resource.h>
  35. #ifdef HAVE_SYS_PRCTL_H
  36. #include <sys/prctl.h>
  37. #endif
  38. #include "test-runner.h"
  39. #if defined(__FreeBSD__)
  40. #include <sys/sysctl.h>
  41. /*
  42. * On FreeBSD, get file descriptor information using sysctl() since that does
  43. * not depend on a mounted fdescfs (which provides /dev/fd/N for N > 2).
  44. */
  45. int
  46. count_open_fds(void)
  47. {
  48. int error;
  49. int nfds;
  50. int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_NFDS, 0 };
  51. size_t len;
  52. len = sizeof(nfds);
  53. error = sysctl(mib, 4, &nfds, &len, NULL, 0);
  54. assert(error == 0 && "sysctl KERN_PROC_NFDS failed.");
  55. return nfds;
  56. }
  57. #elif defined(__OpenBSD__)
  58. #include <sys/sysctl.h>
  59. /*
  60. * On OpenBSD, get file descriptor information using sysctl()
  61. */
  62. int
  63. count_open_fds(void)
  64. {
  65. int error;
  66. int mib[6];
  67. size_t size;
  68. mib[0] = CTL_KERN;
  69. mib[1] = KERN_FILE;
  70. mib[2] = KERN_FILE_BYPID;
  71. mib[3] = getpid();
  72. mib[4] = sizeof(struct kinfo_file);
  73. mib[5] = 0;
  74. /* find the size required to store all the entries */
  75. error = sysctl(mib, 6, NULL, &size, NULL, 0);
  76. assert(error != -1 && "sysctl KERN_FILE_BYPID failed.");
  77. /* return the current number of entries */
  78. return size / sizeof(struct kinfo_file);
  79. }
  80. #else
  81. int
  82. count_open_fds(void)
  83. {
  84. DIR *dir;
  85. struct dirent *ent;
  86. int count = 0;
  87. /*
  88. * Using /dev/fd instead of /proc/self/fd should allow this code to
  89. * work on non-Linux operating systems.
  90. */
  91. dir = opendir("/dev/fd");
  92. assert(dir && "opening /dev/fd failed.");
  93. errno = 0;
  94. while ((ent = readdir(dir))) {
  95. const char *s = ent->d_name;
  96. if (s[0] == '.' && (s[1] == 0 || (s[1] == '.' && s[2] == 0)))
  97. continue;
  98. count++;
  99. }
  100. assert(errno == 0 && "reading /dev/fd failed.");
  101. closedir(dir);
  102. return count;
  103. }
  104. #endif
  105. void
  106. exec_fd_leak_check(int nr_expected_fds)
  107. {
  108. const char *exe = "exec-fd-leak-checker";
  109. char number[16] = { 0 };
  110. const char *test_build_dir = getenv("TEST_BUILD_DIR");
  111. char exe_path[256] = { 0 };
  112. if (test_build_dir == NULL || test_build_dir[0] == 0) {
  113. test_build_dir = ".";
  114. }
  115. snprintf(exe_path, sizeof exe_path - 1, "%s/%s", test_build_dir, exe);
  116. snprintf(number, sizeof number - 1, "%d", nr_expected_fds);
  117. execl(exe_path, exe, number, (char *)NULL);
  118. assert(0 && "execing fd leak checker failed");
  119. }
  120. #define USEC_TO_NSEC(n) (1000 * (n))
  121. /* our implementation of usleep and sleep functions that are safe to use with
  122. * timeouts (timeouts are implemented using alarm(), so it is not safe use
  123. * usleep and sleep. See man pages of these functions)
  124. */
  125. void
  126. test_usleep(useconds_t usec)
  127. {
  128. struct timespec ts = {
  129. .tv_sec = 0,
  130. .tv_nsec = USEC_TO_NSEC(usec)
  131. };
  132. assert(nanosleep(&ts, NULL) == 0);
  133. }
  134. /* we must write the whole function instead of
  135. * wrapping test_usleep, because useconds_t may not
  136. * be able to contain such a big number of microseconds */
  137. void
  138. test_sleep(unsigned int sec)
  139. {
  140. struct timespec ts = {
  141. .tv_sec = sec,
  142. .tv_nsec = 0
  143. };
  144. assert(nanosleep(&ts, NULL) == 0);
  145. }
  146. /** Try to disable coredumps
  147. *
  148. * Useful for tests that crash on purpose, to avoid creating a core file
  149. * or launching an application crash handler service or cluttering coredumpctl.
  150. *
  151. * NOTE: Calling this may make the process undebuggable.
  152. */
  153. void
  154. test_disable_coredumps(void)
  155. {
  156. struct rlimit r;
  157. if (getrlimit(RLIMIT_CORE, &r) == 0) {
  158. r.rlim_cur = 0;
  159. setrlimit(RLIMIT_CORE, &r);
  160. }
  161. #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
  162. prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
  163. #endif
  164. }