bsd-proctitle.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Copyright libuv project contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "internal.h"
  23. #include <sys/types.h>
  24. #include <unistd.h>
  25. static uv_mutex_t process_title_mutex;
  26. static uv_once_t process_title_mutex_once = UV_ONCE_INIT;
  27. static char* process_title;
  28. static void init_process_title_mutex_once(void) {
  29. if (uv_mutex_init(&process_title_mutex))
  30. abort();
  31. }
  32. void uv__process_title_cleanup(void) {
  33. /* TODO(bnoordhuis) uv_mutex_destroy(&process_title_mutex)
  34. * and reset process_title_mutex_once?
  35. */
  36. }
  37. char** uv_setup_args(int argc, char** argv) {
  38. process_title = argc > 0 ? uv__strdup(argv[0]) : NULL;
  39. return argv;
  40. }
  41. int uv_set_process_title(const char* title) {
  42. char* new_title;
  43. new_title = uv__strdup(title);
  44. if (new_title == NULL)
  45. return UV_ENOMEM;
  46. uv_once(&process_title_mutex_once, init_process_title_mutex_once);
  47. uv_mutex_lock(&process_title_mutex);
  48. uv__free(process_title);
  49. process_title = new_title;
  50. setproctitle("%s", title);
  51. uv_mutex_unlock(&process_title_mutex);
  52. return 0;
  53. }
  54. int uv_get_process_title(char* buffer, size_t size) {
  55. size_t len;
  56. if (buffer == NULL || size == 0)
  57. return UV_EINVAL;
  58. uv_once(&process_title_mutex_once, init_process_title_mutex_once);
  59. uv_mutex_lock(&process_title_mutex);
  60. if (process_title != NULL) {
  61. len = strlen(process_title) + 1;
  62. if (size < len) {
  63. uv_mutex_unlock(&process_title_mutex);
  64. return UV_ENOBUFS;
  65. }
  66. memcpy(buffer, process_title, len);
  67. } else {
  68. len = 0;
  69. }
  70. uv_mutex_unlock(&process_title_mutex);
  71. buffer[len] = '\0';
  72. return 0;
  73. }