exec-fd-leak-checker.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 <stdlib.h>
  26. #include <stdio.h>
  27. #include <errno.h>
  28. #include <limits.h>
  29. #include "test-runner.h"
  30. static int
  31. parse_count(const char *str, int *value)
  32. {
  33. char *end;
  34. long v;
  35. errno = 0;
  36. v = strtol(str, &end, 10);
  37. if ((errno == ERANGE && (v == LONG_MAX || v == LONG_MIN)) ||
  38. (errno != 0 && v == 0) ||
  39. (end == str) ||
  40. (*end != '\0')) {
  41. return -1;
  42. }
  43. if (v < 0 || v > INT_MAX) {
  44. return -1;
  45. }
  46. *value = v;
  47. return 0;
  48. }
  49. int main(int argc, char *argv[])
  50. {
  51. int expected;
  52. if (argc != 2)
  53. goto help_out;
  54. if (parse_count(argv[1], &expected) < 0)
  55. goto help_out;
  56. if (count_open_fds() == expected)
  57. return EXIT_SUCCESS;
  58. else
  59. return EXIT_FAILURE;
  60. help_out:
  61. fprintf(stderr, "Usage: %s N\n"
  62. "where N is the expected number of open file descriptors.\n"
  63. "This program exits with a failure if the number "
  64. "does not match exactly.\n", argv[0]);
  65. return EXIT_FAILURE;
  66. }