File: tst-setgetname.c

package info (click to toggle)
glibc 2.24-10
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 223,412 kB
  • sloc: ansic: 991,967; asm: 261,800; sh: 10,385; makefile: 9,710; cpp: 4,169; python: 3,971; perl: 2,254; awk: 1,753; pascal: 1,521; yacc: 291; sed: 80
file content (315 lines) | stat: -rw-r--r-- 8,784 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* Test pthread_setname_np and pthread_getname_np.
   Copyright (C) 2013-2016 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation; either version 2.1 of the
   License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; see the file COPYING.LIB.  If
   not, see <http://www.gnu.org/licenses/>.  */
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <kernel-features.h>

/* New name of process.  */
#define NEW_NAME "setname"

/* Name of process which is one byte too big
   e.g. 17 bytes including null-terminator  */
#define BIG_NAME       "....V....X....XV"

/* Longest name of a process
   e.g. 16 bytes including null-terminator.  */
#define LONGEST_NAME   "....V....X....X"

/* One less than longest name with unique
   characters to detect modification.  */
#define CANARY_NAME    "abcdefghijklmn"

/* On Linux the maximum length of the name of a task *including* the null
   terminator.  */
#define TASK_COMM_LEN 16

long
gettid (void)
{
    return syscall(__NR_gettid);
}

/* On Linux we can read this task's name from /proc.  */
int
get_self_comm (long tid, char *buf, size_t len)
{
  int res = 0;
#define FMT "/proc/self/task/%lu/comm"
  char fname[sizeof (FMT) + 8];
  sprintf (fname, FMT, (unsigned long) tid);

  int fd = open (fname, O_RDONLY);
  if (fd == -1)
    return errno;

  ssize_t n = read (fd, (void *) buf, len);
  if (n < 0)
    res = errno;
  else
    {
      if (buf[n - 1] == '\n')
        buf[n - 1] = '\0';
      else if (n == len)
        res = ERANGE;
      else
        buf[n] = '\0';
    }

  close (fd);
  return res;
}

int
do_test (int argc, char **argv)
{
  pthread_t self;
  int res;
  int ret = 0;
  char name[TASK_COMM_LEN];
  char name_check[TASK_COMM_LEN];

  memset (name, '\0', TASK_COMM_LEN);
  memset (name_check, '\0', TASK_COMM_LEN);

  /* Test 1: Get the name of the task via pthread_getname_np and /proc
     and verify that they both match.  */
  self = pthread_self ();
  res = pthread_getname_np (self, name, TASK_COMM_LEN);

  if (res == 0)
    {
      res = get_self_comm (gettid (), name_check, TASK_COMM_LEN);

#ifndef __ASSUME_PROC_PID_TASK_COMM
      /* On this first test we look for ENOENT to be returned from
         get_self_comm to indicate that the kernel is older than
         2.6.33 and doesn't contain comm within the proc structure.
         In that case we skip the entire test.  */
      if (res == ENOENT)
	{
	  printf ("SKIP: The kernel does not have /proc/self/task/%%lu/comm.\n");
	  return 0;
	}
#endif

      if (res == 0)
       {
         if (strncmp (name, name_check, strlen (BIG_NAME)) == 0)
           printf ("PASS: Test 1 - pthread_getname_np and /proc agree.\n");
         else
           {
             printf ("FAIL: Test 1 - pthread_getname_np and /proc differ"
                     " i.e. %s != %s\n", name, name_check);
             ret++;
           }
       }
      else
       {
         printf ("FAIL: Test 1 - unable read task name via proc.\n");
         ret++;
        }
    }
  else
    {
      printf ("FAIL: Test 1 - pthread_getname_np failed with error %d\n", res);
      ret++;
    }

  /* Test 2: Test setting the name and then independently verify it
             was set.  */
  res = pthread_setname_np (self, NEW_NAME);

  if (res == 0)
    {
      res = get_self_comm (gettid (), name_check, TASK_COMM_LEN);
      if (res == 0)
        {
         if (strncmp (NEW_NAME, name_check, strlen (BIG_NAME)) == 0)
           printf ("PASS: Test 2 - Value used in pthread_setname_np and"
                   " /proc agree.\n");
         else
           {
             printf ("FAIL: Test 2 - Value used in pthread_setname_np"
		     " and /proc differ i.e. %s != %s\n",
		     NEW_NAME, name_check);
             ret++;
           }
        }
      else
       {
         printf ("FAIL: Test 2 - unable to read task name via proc.\n");
         ret++;
        }
    }
  else
    {
      printf ("FAIL: Test 2 - pthread_setname_np failed with error %d\n", res);
      ret++;
    }

  /* Test 3: Test setting a name that is one-byte too big.  */
  res = pthread_getname_np (self, name, TASK_COMM_LEN);

  if (res == 0)
    {
      res = pthread_setname_np (self, BIG_NAME);
      if (res != 0)
        {
         if (res == ERANGE)
           {
             printf ("PASS: Test 3 - pthread_setname_np returned ERANGE"
                     " for a process name that was too long.\n");

             /* Verify the old name didn't change.  */
             res = get_self_comm (gettid (), name_check, TASK_COMM_LEN);
             if (res == 0)
               {
                 if (strncmp (name, name_check, strlen (BIG_NAME)) == 0)
                   printf ("PASS: Test 3 - Original name unchanged after"
                           " pthread_setname_np returned ERANGE.\n");
                 else
                   {
                     printf ("FAIL: Test 3 - Original name changed after"
                             " pthread_setname_np returned ERANGE"
                             " i.e. %s != %s\n",
                             name, name_check);
                     ret++;
                   }
               }
             else
               {
                 printf ("FAIL: Test 3 - unable to read task name.\n");
                 ret++;
               }
           }
         else
           {
             printf ("FAIL: Test 3 - Wrong error returned"
		     " i.e. ERANGE != %d\n", res);
             ret++;
           }
        }
      else
        {
         printf ("FAIL: Test 3 - Too-long name accepted by"
	         " pthread_setname_np.\n");
         ret++;
        }
    }
  else
    {
      printf ("FAIL: Test 3 - Unable to get original name.\n");
      ret++;
    }

  /* Test 4: Verify that setting the longest name works.  */
  res = pthread_setname_np (self, LONGEST_NAME);

  if (res == 0)
    {
      res = get_self_comm (gettid (), name_check, TASK_COMM_LEN);
      if (res == 0)
        {
         if (strncmp (LONGEST_NAME, name_check, strlen (BIG_NAME)) == 0)
           printf ("PASS: Test 4 - Longest name set via pthread_setname_np"
                   " agrees with /proc.\n");
         else
           {
             printf ("FAIL: Test 4 - Value used in pthread_setname_np and /proc"
		     " differ i.e. %s != %s\n", LONGEST_NAME, name_check);
             ret++;
           }
        }
      else
       {
         printf ("FAIL: Test 4 - unable to read task name via proc.\n");
         ret++;
        }
    }
  else
    {
      printf ("FAIL: Test 4 - pthread_setname_np failed with error %d\n", res);
      ret++;
    }

  /* Test 5: Verify that getting a long name into a small buffer fails.  */
  strncpy (name, CANARY_NAME, strlen (CANARY_NAME) + 1);

  /* Claim the buffer length is strlen (LONGEST_NAME).  This is one character
     too small to hold LONGEST_NAME *and* the null terminator.  We should get
     back ERANGE and name should be unmodified.  */
  res = pthread_getname_np (self, name, strlen (LONGEST_NAME));

  if (res != 0)
    {
      if (res == ERANGE)
        {
	  if (strncmp (CANARY_NAME, name, strlen (BIG_NAME)) == 0)
	    {
	      printf ("PASS: Test 5 - ERANGE and buffer unmodified.\n");
	    }
	  else
	    {
	      printf ("FAIL: Test 5 - Original buffer modified.\n");
	      ret++;
	    }
        }
      else
        {
	  printf ("FAIL: Test 5 - Did not return ERANGE for small buffer.\n");
	  ret++;
        }
    }
  else
    {
      printf ("FAIL: Test 5 - Returned name longer than buffer.\n");
      ret++;
    }

  /* Test 6: Lastly make sure we can read back the longest name.  */
  res = pthread_getname_np (self, name, strlen (LONGEST_NAME) + 1);

  if (res == 0)
    {
      if (strncmp (LONGEST_NAME, name, strlen (BIG_NAME)) == 0)
        {
	  printf ("PASS: Test 6 - Read back longest name correctly.\n");
        }
      else
        {
	  printf ("FAIL: Test 6 - Read \"%s\" instead of longest name.\n",
		  name);
	  ret++;
        }
    }
  else
    {
      printf ("FAIL: Test 6 - pthread_getname_np failed with error %d\n", res);
      ret++;
    }

  return ret;
}

#include <test-skeleton.c>