1e66f31c5Sopenharmony_ci/* Copyright libuv project contributors. All rights reserved.
2e66f31c5Sopenharmony_ci *
3e66f31c5Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
4e66f31c5Sopenharmony_ci * of this software and associated documentation files (the "Software"), to
5e66f31c5Sopenharmony_ci * deal in the Software without restriction, including without limitation the
6e66f31c5Sopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7e66f31c5Sopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is
8e66f31c5Sopenharmony_ci * furnished to do so, subject to the following conditions:
9e66f31c5Sopenharmony_ci *
10e66f31c5Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
11e66f31c5Sopenharmony_ci * all copies or substantial portions of the Software.
12e66f31c5Sopenharmony_ci *
13e66f31c5Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14e66f31c5Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15e66f31c5Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16e66f31c5Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17e66f31c5Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18e66f31c5Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19e66f31c5Sopenharmony_ci * IN THE SOFTWARE.
20e66f31c5Sopenharmony_ci */
21e66f31c5Sopenharmony_ci
22e66f31c5Sopenharmony_ci#include "uv.h"
23e66f31c5Sopenharmony_ci#include "internal.h"
24e66f31c5Sopenharmony_ci
25e66f31c5Sopenharmony_ci#include <stdint.h>
26e66f31c5Sopenharmony_ci#include <stdlib.h>
27e66f31c5Sopenharmony_ci#include <string.h>
28e66f31c5Sopenharmony_ci
29e66f31c5Sopenharmony_ci#include <sys/types.h>
30e66f31c5Sopenharmony_ci
31e66f31c5Sopenharmony_ci#include <sys/time.h>
32e66f31c5Sopenharmony_ci#include <unistd.h>
33e66f31c5Sopenharmony_ci
34e66f31c5Sopenharmony_ci#include <procinfo.h>
35e66f31c5Sopenharmony_ci
36e66f31c5Sopenharmony_ci#include <ctype.h>
37e66f31c5Sopenharmony_ci
38e66f31c5Sopenharmony_ciextern char* original_exepath;
39e66f31c5Sopenharmony_ciextern uv_mutex_t process_title_mutex;
40e66f31c5Sopenharmony_ciextern uv_once_t process_title_mutex_once;
41e66f31c5Sopenharmony_ciextern void init_process_title_mutex_once(void);
42e66f31c5Sopenharmony_ci
43e66f31c5Sopenharmony_ciuint64_t uv__hrtime(uv_clocktype_t type) {
44e66f31c5Sopenharmony_ci  uint64_t G = 1000000000;
45e66f31c5Sopenharmony_ci  timebasestruct_t t;
46e66f31c5Sopenharmony_ci  read_wall_time(&t, TIMEBASE_SZ);
47e66f31c5Sopenharmony_ci  time_base_to_time(&t, TIMEBASE_SZ);
48e66f31c5Sopenharmony_ci  return (uint64_t) t.tb_high * G + t.tb_low;
49e66f31c5Sopenharmony_ci}
50e66f31c5Sopenharmony_ci
51e66f31c5Sopenharmony_ci
52e66f31c5Sopenharmony_ci/*
53e66f31c5Sopenharmony_ci * We could use a static buffer for the path manipulations that we need outside
54e66f31c5Sopenharmony_ci * of the function, but this function could be called by multiple consumers and
55e66f31c5Sopenharmony_ci * we don't want to potentially create a race condition in the use of snprintf.
56e66f31c5Sopenharmony_ci * There is no direct way of getting the exe path in AIX - either through /procfs
57e66f31c5Sopenharmony_ci * or through some libc APIs. The below approach is to parse the argv[0]'s pattern
58e66f31c5Sopenharmony_ci * and use it in conjunction with PATH environment variable to craft one.
59e66f31c5Sopenharmony_ci */
60e66f31c5Sopenharmony_ciint uv_exepath(char* buffer, size_t* size) {
61e66f31c5Sopenharmony_ci  int res;
62e66f31c5Sopenharmony_ci  char args[UV__PATH_MAX];
63e66f31c5Sopenharmony_ci  size_t cached_len;
64e66f31c5Sopenharmony_ci  struct procsinfo pi;
65e66f31c5Sopenharmony_ci
66e66f31c5Sopenharmony_ci  if (buffer == NULL || size == NULL || *size == 0)
67e66f31c5Sopenharmony_ci    return UV_EINVAL;
68e66f31c5Sopenharmony_ci
69e66f31c5Sopenharmony_ci  uv_once(&process_title_mutex_once, init_process_title_mutex_once);
70e66f31c5Sopenharmony_ci  uv_mutex_lock(&process_title_mutex);
71e66f31c5Sopenharmony_ci  if (original_exepath != NULL) {
72e66f31c5Sopenharmony_ci    cached_len = strlen(original_exepath);
73e66f31c5Sopenharmony_ci    *size -= 1;
74e66f31c5Sopenharmony_ci    if (*size > cached_len)
75e66f31c5Sopenharmony_ci      *size = cached_len;
76e66f31c5Sopenharmony_ci    memcpy(buffer, original_exepath, *size);
77e66f31c5Sopenharmony_ci    buffer[*size] = '\0';
78e66f31c5Sopenharmony_ci    uv_mutex_unlock(&process_title_mutex);
79e66f31c5Sopenharmony_ci    return 0;
80e66f31c5Sopenharmony_ci  }
81e66f31c5Sopenharmony_ci  uv_mutex_unlock(&process_title_mutex);
82e66f31c5Sopenharmony_ci  pi.pi_pid = getpid();
83e66f31c5Sopenharmony_ci  res = getargs(&pi, sizeof(pi), args, sizeof(args));
84e66f31c5Sopenharmony_ci
85e66f31c5Sopenharmony_ci  if (res < 0)
86e66f31c5Sopenharmony_ci    return UV_EINVAL;
87e66f31c5Sopenharmony_ci
88e66f31c5Sopenharmony_ci  return uv__search_path(args, buffer, size);
89e66f31c5Sopenharmony_ci}
90