(root)/
strace-6.5/
tests/
prctl-name.c
       1  /*
       2   * Check decoding of prctl PR_GET_NAME/PR_SET_NAME operations.
       3   *
       4   * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
       5   * Copyright (c) 2016 Dmitry V. Levin <ldv@strace.io>
       6   * Copyright (c) 2016-2021 The strace developers.
       7   * All rights reserved.
       8   *
       9   * SPDX-License-Identifier: GPL-2.0-or-later
      10   */
      11  
      12  #include "tests.h"
      13  #include "scno.h"
      14  #include <stdio.h>
      15  #include <string.h>
      16  #include <unistd.h>
      17  #include <sys/prctl.h>
      18  
      19  int
      20  main(void)
      21  {
      22  	static const char str[] = "0123456789abcdef";
      23  	static const int len = sizeof(str) - 1;
      24  	char *name = tail_memdup(str, sizeof(str));
      25  	int rc;
      26  
      27  	prctl_marker();
      28  
      29  	rc = prctl(PR_SET_NAME, NULL);
      30  	printf("prctl(PR_SET_NAME, NULL) = %s\n", sprintrc(rc));
      31  
      32  	for (int i = 0; i <= len; ++i) {
      33  		rc = prctl(PR_SET_NAME, name + len - i);
      34  		printf("prctl(PR_SET_NAME, \"%.*s\"%s) = %s\n",
      35  		       i < len - 1 ? i : len - 1,
      36  		       str + len - i,
      37  		       i < len - 1 ? "" : "...",
      38  		       sprintrc(rc));
      39  	}
      40  
      41  	*name = -1;
      42  	++name;
      43  	memcpy(name, str, len);
      44  
      45  	for (int i = 0; i <= len; ++i) {
      46  		rc = prctl(PR_SET_NAME, name + len - i);
      47  		if (i < len - 1)
      48  			printf("prctl(PR_SET_NAME, %p) = %s\n",
      49  			       name + len - i, sprintrc(rc));
      50  		else
      51  			printf("prctl(PR_SET_NAME, \"%.*s\"...) = %s\n",
      52  			       len - 1, str + len - i, sprintrc(rc));
      53  	}
      54  
      55  	rc = prctl(PR_GET_NAME, NULL);
      56  	printf("prctl(PR_GET_NAME, NULL) = %s\n", sprintrc(rc));
      57  
      58  	for (int i = 0; i < len; ++i) {
      59  		rc = prctl(PR_GET_NAME, name + len - i);
      60  		printf("prctl(PR_GET_NAME, %p) = %s\n",
      61  		       name + len - i, sprintrc(rc));
      62  	}
      63  
      64  	rc = prctl(PR_GET_NAME, name);
      65  	if (rc)
      66  		printf("prctl(PR_GET_NAME, %p) = %s\n",
      67  		       name, sprintrc(rc));
      68  	else
      69  		printf("prctl(PR_GET_NAME, \"%.*s\") = %s\n",
      70  		       len - 1, name, sprintrc(rc));
      71  
      72  	puts("+++ exited with 0 +++");
      73  	return 0;
      74  }