Path: blob/master/Utilities/cmlibuv/src/unix/aix-common.c
3156 views
/* Copyright libuv project contributors. All rights reserved.1*2* Permission is hereby granted, free of charge, to any person obtaining a copy3* of this software and associated documentation files (the "Software"), to4* deal in the Software without restriction, including without limitation the5* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or6* sell copies of the Software, and to permit persons to whom the Software is7* furnished to do so, subject to the following conditions:8*9* The above copyright notice and this permission notice shall be included in10* all copies or substantial portions of the Software.11*12* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR13* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,14* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE15* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER16* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING17* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS18* IN THE SOFTWARE.19*/2021#include "uv.h"22#include "internal.h"2324#include <stdint.h>25#include <stdlib.h>26#include <string.h>2728#include <sys/types.h>2930#include <sys/time.h>31#include <unistd.h>3233#include <procinfo.h>3435#include <ctype.h>3637extern char* original_exepath;38extern uv_mutex_t process_title_mutex;39extern uv_once_t process_title_mutex_once;40extern void init_process_title_mutex_once(void);4142uint64_t uv__hrtime(uv_clocktype_t type) {43uint64_t G = 1000000000;44timebasestruct_t t;45read_wall_time(&t, TIMEBASE_SZ);46time_base_to_time(&t, TIMEBASE_SZ);47return (uint64_t) t.tb_high * G + t.tb_low;48}495051/*52* We could use a static buffer for the path manipulations that we need outside53* of the function, but this function could be called by multiple consumers and54* we don't want to potentially create a race condition in the use of snprintf.55* There is no direct way of getting the exe path in AIX - either through /procfs56* or through some libc APIs. The below approach is to parse the argv[0]'s pattern57* and use it in conjunction with PATH environment variable to craft one.58*/59int uv_exepath(char* buffer, size_t* size) {60int res;61char args[UV__PATH_MAX];62size_t cached_len;63struct procsinfo pi;6465if (buffer == NULL || size == NULL || *size == 0)66return UV_EINVAL;6768uv_once(&process_title_mutex_once, init_process_title_mutex_once);69uv_mutex_lock(&process_title_mutex);70if (original_exepath != NULL) {71cached_len = strlen(original_exepath);72*size -= 1;73if (*size > cached_len)74*size = cached_len;75memcpy(buffer, original_exepath, *size);76buffer[*size] = '\0';77uv_mutex_unlock(&process_title_mutex);78return 0;79}80uv_mutex_unlock(&process_title_mutex);81pi.pi_pid = getpid();82res = getargs(&pi, sizeof(pi), args, sizeof(args));8384if (res < 0)85return UV_EINVAL;8687return uv__search_path(args, buffer, size);88}89909192