10e552da7Schristos /* Copyright libuv project contributors. All rights reserved.
20e552da7Schristos *
30e552da7Schristos * Permission is hereby granted, free of charge, to any person obtaining a copy
40e552da7Schristos * of this software and associated documentation files (the "Software"), to
50e552da7Schristos * deal in the Software without restriction, including without limitation the
60e552da7Schristos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
70e552da7Schristos * sell copies of the Software, and to permit persons to whom the Software is
80e552da7Schristos * furnished to do so, subject to the following conditions:
90e552da7Schristos *
100e552da7Schristos * The above copyright notice and this permission notice shall be included in
110e552da7Schristos * all copies or substantial portions of the Software.
120e552da7Schristos *
130e552da7Schristos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
140e552da7Schristos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
150e552da7Schristos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
160e552da7Schristos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
170e552da7Schristos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
180e552da7Schristos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
190e552da7Schristos * IN THE SOFTWARE.
200e552da7Schristos */
210e552da7Schristos
220e552da7Schristos #include "uv.h"
230e552da7Schristos #include "internal.h"
240e552da7Schristos
250e552da7Schristos #include <sys/types.h>
260e552da7Schristos #include <unistd.h>
270e552da7Schristos
280e552da7Schristos
290e552da7Schristos static uv_mutex_t process_title_mutex;
300e552da7Schristos static uv_once_t process_title_mutex_once = UV_ONCE_INIT;
310e552da7Schristos static char* process_title;
320e552da7Schristos
330e552da7Schristos
init_process_title_mutex_once(void)340e552da7Schristos static void init_process_title_mutex_once(void) {
350e552da7Schristos if (uv_mutex_init(&process_title_mutex))
360e552da7Schristos abort();
370e552da7Schristos }
380e552da7Schristos
390e552da7Schristos
uv__process_title_cleanup(void)400e552da7Schristos void uv__process_title_cleanup(void) {
41*5f2f4271Schristos uv_once(&process_title_mutex_once, init_process_title_mutex_once);
42*5f2f4271Schristos uv_mutex_destroy(&process_title_mutex);
430e552da7Schristos }
440e552da7Schristos
450e552da7Schristos
uv_setup_args(int argc,char ** argv)460e552da7Schristos char** uv_setup_args(int argc, char** argv) {
470e552da7Schristos process_title = argc > 0 ? uv__strdup(argv[0]) : NULL;
480e552da7Schristos return argv;
490e552da7Schristos }
500e552da7Schristos
510e552da7Schristos
uv_set_process_title(const char * title)520e552da7Schristos int uv_set_process_title(const char* title) {
530e552da7Schristos char* new_title;
540e552da7Schristos
550e552da7Schristos new_title = uv__strdup(title);
560e552da7Schristos if (new_title == NULL)
570e552da7Schristos return UV_ENOMEM;
580e552da7Schristos
590e552da7Schristos uv_once(&process_title_mutex_once, init_process_title_mutex_once);
600e552da7Schristos uv_mutex_lock(&process_title_mutex);
610e552da7Schristos
620e552da7Schristos uv__free(process_title);
630e552da7Schristos process_title = new_title;
640e552da7Schristos setproctitle("%s", title);
650e552da7Schristos
660e552da7Schristos uv_mutex_unlock(&process_title_mutex);
670e552da7Schristos
680e552da7Schristos return 0;
690e552da7Schristos }
700e552da7Schristos
710e552da7Schristos
uv_get_process_title(char * buffer,size_t size)720e552da7Schristos int uv_get_process_title(char* buffer, size_t size) {
730e552da7Schristos size_t len;
740e552da7Schristos
750e552da7Schristos if (buffer == NULL || size == 0)
760e552da7Schristos return UV_EINVAL;
770e552da7Schristos
780e552da7Schristos uv_once(&process_title_mutex_once, init_process_title_mutex_once);
790e552da7Schristos uv_mutex_lock(&process_title_mutex);
800e552da7Schristos
810e552da7Schristos if (process_title != NULL) {
820e552da7Schristos len = strlen(process_title) + 1;
830e552da7Schristos
840e552da7Schristos if (size < len) {
850e552da7Schristos uv_mutex_unlock(&process_title_mutex);
860e552da7Schristos return UV_ENOBUFS;
870e552da7Schristos }
880e552da7Schristos
890e552da7Schristos memcpy(buffer, process_title, len);
900e552da7Schristos } else {
910e552da7Schristos len = 0;
920e552da7Schristos }
930e552da7Schristos
940e552da7Schristos uv_mutex_unlock(&process_title_mutex);
950e552da7Schristos
960e552da7Schristos buffer[len] = '\0';
970e552da7Schristos
980e552da7Schristos return 0;
990e552da7Schristos }
100