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 
23 #include "uv.h"
24 #include "task.h"
25 
26 #include <string.h>
27 
28 #ifdef __APPLE__
29 # define NUM_ITERATIONS 5
30 #else
31 # define NUM_ITERATIONS 50
32 #endif
33 
34 static const char* titles[] = {
35   "8L2NY0Kdj0XyNFZnmUZigIOfcWjyNr0SkMmUhKw99VLUsZFrvCQQC3XIRfNR8pjyMjXObllled",
36   "jUAcscJN49oLSN8GdmXj2Wo34XX2T2vp2j5khfajNQarlOulp57cE130yiY53ipJFnPyTn5i82",
37   "9niCI5icXGFS72XudhXqo5alftmZ1tpE7B3cwUmrq0CCDjC84FzBNB8XAHqvpNQfI2QAQG6ztT",
38   "n8qXVXuG6IEHDpabJgTEiwtpY6LHMZ8MgznnMpdHARu5EywufA6hcBaQfetb0YhEsK0ykDd7JU"
39 };
40 
getter_thread_body(void * arg)41 static void getter_thread_body(void* arg) {
42   uv_sem_t* getter_sem;
43   char buffer[512];
44   size_t len;
45 
46   getter_sem = arg;
47 
48   while (UV_EAGAIN == uv_sem_trywait(getter_sem)) {
49     ASSERT_OK(uv_get_process_title(buffer, sizeof(buffer)));
50 
51     /* The maximum size of the process title on some platforms depends on
52      * the total size of the argv vector. It's therefore possible to read
53      * back a title that's shorter than what we submitted.
54      */
55     len = strlen(buffer);
56     ASSERT_GT(len, 0);
57 
58     ASSERT(
59       0 == strncmp(buffer, titles[0], len) ||
60       0 == strncmp(buffer, titles[1], len) ||
61       0 == strncmp(buffer, titles[2], len) ||
62       0 == strncmp(buffer, titles[3], len));
63 
64     uv_sleep(0);
65   }
66 }
67 
68 
setter_thread_body(void * arg)69 static void setter_thread_body(void* arg) {
70   int i;
71 
72   for (i = 0; i < NUM_ITERATIONS; i++) {
73     ASSERT_OK(uv_set_process_title(titles[0]));
74     ASSERT_OK(uv_set_process_title(titles[1]));
75     ASSERT_OK(uv_set_process_title(titles[2]));
76     ASSERT_OK(uv_set_process_title(titles[3]));
77   }
78 }
79 
80 
TEST_IMPL(process_title_threadsafe)81 TEST_IMPL(process_title_threadsafe) {
82   uv_thread_t setter_threads[4];
83   uv_thread_t getter_thread;
84   uv_sem_t getter_sem;
85   int i;
86 
87 #if defined(__sun) || defined(__CYGWIN__) || defined(__MSYS__) || \
88     defined(__MVS__) || defined(__PASE__)
89   RETURN_SKIP("uv_(get|set)_process_title is not implemented.");
90 #endif
91 
92   ASSERT_OK(uv_set_process_title(titles[0]));
93 
94   ASSERT_OK(uv_sem_init(&getter_sem, 0));
95   ASSERT_OK(uv_thread_create(&getter_thread, getter_thread_body, &getter_sem));
96 
97   for (i = 0; i < (int) ARRAY_SIZE(setter_threads); i++)
98     ASSERT_OK(uv_thread_create(&setter_threads[i], setter_thread_body, NULL));
99 
100   for (i = 0; i < (int) ARRAY_SIZE(setter_threads); i++)
101     ASSERT_OK(uv_thread_join(&setter_threads[i]));
102 
103   uv_sem_post(&getter_sem);
104   ASSERT_OK(uv_thread_join(&getter_thread));
105   uv_sem_destroy(&getter_sem);
106 
107   return 0;
108 }
109