1251607Sdim/* This testcase is part of GDB, the GNU debugger.
2251607Sdim
3251607Sdim   Copyright 2008-2020 Free Software Foundation, Inc.
4251607Sdim
5251607Sdim   This program is free software; you can redistribute it and/or modify
6251607Sdim   it under the terms of the GNU General Public License as published by
7251607Sdim   the Free Software Foundation; either version 3 of the License, or
8251607Sdim   (at your option) any later version.
9251607Sdim
10251607Sdim   This program is distributed in the hope that it will be useful,
11251607Sdim   but WITHOUT ANY WARRANTY; without even the implied warranty of
12251607Sdim   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13251607Sdim   GNU General Public License for more details.
14251607Sdim
15251607Sdim   You should have received a copy of the GNU General Public License
16251607Sdim   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17251607Sdim
18251607Sdim#define _GNU_SOURCE
19251607Sdim#include <sys/socket.h>
20263509Sdim#include <sys/types.h>
21263509Sdim#include <sys/un.h>
22251607Sdim#include <unistd.h>
23251607Sdim
24251607Sdimvoid
25251607Sdimmarker1 (void)
26251607Sdim{
27251607Sdim}
28251607Sdim
29251607Sdimvoid
30251607Sdimmarker2 (void)
31251607Sdim{
32251607Sdim}
33251607Sdim
34251607Sdimchar wdata[] = "abcdef";
35251607Sdim
36251607Sdimstruct iovec wv[1] = {
37251607Sdim  { wdata, 6 },
38251607Sdim};
39251607Sdim
40251607Sdimchar wc[CMSG_SPACE (sizeof (struct ucred)) + CMSG_SPACE (sizeof (int))];
41251607Sdim
42251607Sdimstruct msghdr wmsg = {
43251607Sdim  0, 0,
44251607Sdim  wv, 1,
45263509Sdim  wc, sizeof wc,
46263509Sdim  0,
47251607Sdim};
48251607Sdim
49251607Sdimchar rdata[5] = "xxxx";
50251607Sdim
51251607Sdimstruct iovec rv[2] = {
52263509Sdim  {&rdata[2], 2},
53263509Sdim  {&rdata[0], 2},
54251607Sdim};
55251607Sdim
56251607Sdimchar rc[CMSG_SPACE (sizeof (struct ucred)) + 7];
57251607Sdim
58251607Sdimstruct msghdr rmsg = {
59251607Sdim  0, 0,
60251607Sdim  rv, 2,
61251607Sdim  rc, sizeof rc,
62251607Sdim  0,
63251607Sdim};
64251607Sdim
65251607Sdimint fds[2];
66263509Sdim
67263509Sdimint
68251607Sdimmain (void)
69251607Sdim{
70251607Sdim  int itrue = 1;
71251607Sdim  /* prepare cmsg to send */
72251607Sdim  struct cmsghdr *cm1 = CMSG_FIRSTHDR (&wmsg);
73251607Sdim  cm1->cmsg_len = CMSG_LEN (sizeof (struct ucred));
74251607Sdim  cm1->cmsg_level = AF_UNIX;
75251607Sdim  cm1->cmsg_type = SCM_CREDENTIALS;
76251607Sdim  struct ucred *uc = (void *) CMSG_DATA (cm1);
77251607Sdim  uc->pid = getpid ();
78251607Sdim  uc->uid = getuid ();
79251607Sdim  uc->gid = getgid ();
80251607Sdim  struct cmsghdr *cm2 = CMSG_NXTHDR (&wmsg, cm1);
81251607Sdim  cm2->cmsg_len = CMSG_LEN (sizeof (int));
82251607Sdim  cm2->cmsg_level = AF_UNIX;
83251607Sdim  cm2->cmsg_type = SCM_RIGHTS;
84251607Sdim  int *pfd = (void *) CMSG_DATA (cm2);
85251607Sdim  *pfd = 2;
86251607Sdim  /* do the syscalls */
87251607Sdim  marker1 ();
88251607Sdim  socketpair (AF_UNIX, SOCK_DGRAM, 0, fds);
89251607Sdim  setsockopt (fds[0], SOL_SOCKET, SO_PASSCRED, &itrue, sizeof itrue);
90251607Sdim  sendmsg (fds[1], &wmsg, 0);
91251607Sdim  recvmsg (fds[0], &rmsg, 0);
92251607Sdim  marker2 ();
93251607Sdim  return 0;
94251607Sdim}
95251607Sdim