1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2011-2019 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include <arpa/inet.h>
24 #include <sys/socket.h>
28 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
30 /* System V IPC identifiers. */
31 static int shmid
= -1, semid
= -1, msqid
= -1;
33 /* Delete any System V IPC resources that were allocated. */
39 shmctl (shmid
, IPC_RMID
, NULL
);
41 semctl (semid
, 0, IPC_RMID
, NULL
);
43 msgctl (msqid
, IPC_RMID
, NULL
);
47 thread_proc (void *args
)
49 pthread_mutex_lock (&mutex
);
50 pthread_mutex_unlock (&mutex
);
56 const int flags
= IPC_CREAT
| 0666;
57 key_t shmkey
= 3925, semkey
= 7428, msgkey
= 5294;
60 struct sockaddr_in sock_addr
;
64 int status
, try, retries
= 1000;
68 for (try = 0; try < retries
; ++try)
70 shmid
= shmget (shmkey
, 4096, flags
| IPC_EXCL
);
79 printf ("Cannot create shared-memory region after %d tries.\n", retries
);
83 for (try = 0; try < retries
; ++try)
85 semid
= semget (semkey
, 1, flags
| IPC_EXCL
);
94 printf ("Cannot create semaphore after %d tries.\n", retries
);
98 for (try = 0; try < retries
; ++try)
100 msqid
= msgget (msgkey
, flags
| IPC_EXCL
);
109 printf ("Cannot create message queue after %d tries.\n", retries
);
113 fd
= fopen ("/dev/null", "r");
115 /* Lock the mutex to prevent the new thread from finishing immediately. */
116 pthread_mutex_lock (&mutex
);
117 pthread_create (&thread
, NULL
, thread_proc
, 0);
119 sock
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
122 printf ("Cannot create socket.\n");
126 sock_addr
.sin_family
= AF_INET
;
127 sock_addr
.sin_port
= 0; /* Bind to a free port. */
128 sock_addr
.sin_addr
.s_addr
= htonl (INADDR_ANY
);
130 status
= bind (sock
, (struct sockaddr
*) &sock_addr
, sizeof (sock_addr
));
133 printf ("Cannot bind socket.\n");
137 /* Find the assigned port number of the socket. */
138 size
= sizeof (sock_addr
);
139 status
= getsockname (sock
, (struct sockaddr
*) &sock_addr
, &size
);
142 printf ("Cannot find name of socket.\n");
145 port
= ntohs (sock_addr
.sin_port
);
147 status
= listen (sock
, 1);
150 printf ("Cannot listen on socket.\n");
154 /* Set breakpoint here. */
159 pthread_mutex_unlock (&mutex
);
160 pthread_join (thread
, NULL
);