xref: /libuv/src/unix/aix-common.c (revision ea92e9c7)
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 
22 #include "uv.h"
23 #include "internal.h"
24 
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include <sys/types.h>
30 
31 #include <sys/time.h>
32 #include <unistd.h>
33 
34 #include <procinfo.h>
35 
36 #include <ctype.h>
37 
38 extern char* original_exepath;
39 extern uv_mutex_t process_title_mutex;
40 extern uv_once_t process_title_mutex_once;
41 extern void init_process_title_mutex_once(void);
42 
uv__hrtime(uv_clocktype_t type)43 uint64_t uv__hrtime(uv_clocktype_t type) {
44   uint64_t G = 1000000000;
45   timebasestruct_t t;
46   read_wall_time(&t, TIMEBASE_SZ);
47   time_base_to_time(&t, TIMEBASE_SZ);
48   return (uint64_t) t.tb_high * G + t.tb_low;
49 }
50 
51 
52 /*
53  * We could use a static buffer for the path manipulations that we need outside
54  * of the function, but this function could be called by multiple consumers and
55  * we don't want to potentially create a race condition in the use of snprintf.
56  * There is no direct way of getting the exe path in AIX - either through /procfs
57  * or through some libc APIs. The below approach is to parse the argv[0]'s pattern
58  * and use it in conjunction with PATH environment variable to craft one.
59  */
uv_exepath(char * buffer,size_t * size)60 int uv_exepath(char* buffer, size_t* size) {
61   int res;
62   char args[UV__PATH_MAX];
63   size_t cached_len;
64   struct procsinfo pi;
65 
66   if (buffer == NULL || size == NULL || *size == 0)
67     return UV_EINVAL;
68 
69   uv_once(&process_title_mutex_once, init_process_title_mutex_once);
70   uv_mutex_lock(&process_title_mutex);
71   if (original_exepath != NULL) {
72     cached_len = strlen(original_exepath);
73     *size -= 1;
74     if (*size > cached_len)
75       *size = cached_len;
76     memcpy(buffer, original_exepath, *size);
77     buffer[*size] = '\0';
78     uv_mutex_unlock(&process_title_mutex);
79     return 0;
80   }
81   uv_mutex_unlock(&process_title_mutex);
82   pi.pi_pid = getpid();
83   res = getargs(&pi, sizeof(pi), args, sizeof(args));
84 
85   if (res < 0)
86     return UV_EINVAL;
87 
88   return uv__search_path(args, buffer, size);
89 }
90