1
0

proctitle.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. * Permission is hereby granted, free of charge, to any person obtaining a copy
  3. * of this software and associated documentation files (the "Software"), to
  4. * deal in the Software without restriction, including without limitation the
  5. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  6. * sell copies of the Software, and to permit persons to whom the Software is
  7. * furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in
  10. * all copies or substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  17. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  18. * IN THE SOFTWARE.
  19. */
  20. #include "uv.h"
  21. #include "internal.h"
  22. #include <stdlib.h>
  23. #include <string.h>
  24. extern void uv__set_process_title(const char* title);
  25. static void* args_mem;
  26. static struct {
  27. char* str;
  28. size_t len;
  29. } process_title;
  30. char** uv_setup_args(int argc, char** argv) {
  31. char** new_argv;
  32. size_t size;
  33. char* s;
  34. int i;
  35. if (argc <= 0)
  36. return argv;
  37. /* Calculate how much memory we need for the argv strings. */
  38. size = 0;
  39. for (i = 0; i < argc; i++)
  40. size += strlen(argv[i]) + 1;
  41. #if defined(__MVS__)
  42. /* argv is not adjacent. So just use argv[0] */
  43. process_title.str = argv[0];
  44. process_title.len = strlen(argv[0]);
  45. #else
  46. process_title.str = argv[0];
  47. process_title.len = argv[argc - 1] + strlen(argv[argc - 1]) - argv[0];
  48. assert(process_title.len + 1 == size); /* argv memory should be adjacent. */
  49. #endif
  50. /* Add space for the argv pointers. */
  51. size += (argc + 1) * sizeof(char*);
  52. new_argv = uv__malloc(size);
  53. if (new_argv == NULL)
  54. return argv;
  55. args_mem = new_argv;
  56. /* Copy over the strings and set up the pointer table. */
  57. s = (char*) &new_argv[argc + 1];
  58. for (i = 0; i < argc; i++) {
  59. size = strlen(argv[i]) + 1;
  60. memcpy(s, argv[i], size);
  61. new_argv[i] = s;
  62. s += size;
  63. }
  64. new_argv[i] = NULL;
  65. return new_argv;
  66. }
  67. int uv_set_process_title(const char* title) {
  68. if (process_title.len == 0)
  69. return 0;
  70. /* No need to terminate, byte after is always '\0'. */
  71. strncpy(process_title.str, title, process_title.len);
  72. uv__set_process_title(title);
  73. return 0;
  74. }
  75. int uv_get_process_title(char* buffer, size_t size) {
  76. if (buffer == NULL || size == 0)
  77. return -EINVAL;
  78. else if (size <= process_title.len)
  79. return -ENOBUFS;
  80. memcpy(buffer, process_title.str, process_title.len + 1);
  81. buffer[process_title.len] = '\0';
  82. return 0;
  83. }
  84. UV_DESTRUCTOR(static void free_args_mem(void)) {
  85. uv__free(args_mem); /* Keep valgrind happy. */
  86. args_mem = NULL;
  87. }