1 /* This testcase is part of GDB, the GNU debugger. 2 3 Copyright 2011-2023 Free Software Foundation, Inc. 4 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. 9 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. 14 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/>. */ 17 18 #include <sys/shm.h> 19 #include <sys/sem.h> 20 #include <sys/msg.h> 21 #include <stdio.h> 22 #include <pthread.h> 23 #include <arpa/inet.h> 24 #include <sys/socket.h> 25 #include <unistd.h> 26 #include <stdlib.h> 27 28 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 29 30 /* System V IPC identifiers. */ 31 static int shmid = -1, semid = -1, msqid = -1; 32 33 /* Delete any System V IPC resources that were allocated. */ 34 35 static void 36 ipc_cleanup (void) 37 { 38 if (shmid >= 0) 39 shmctl (shmid, IPC_RMID, NULL); 40 if (semid >= 0) 41 semctl (semid, 0, IPC_RMID, NULL); 42 if (msqid >= 0) 43 msgctl (msqid, IPC_RMID, NULL); 44 } 45 46 void * 47 thread_proc (void *args) 48 { 49 pthread_mutex_lock (&mutex); 50 pthread_mutex_unlock (&mutex); 51 52 return NULL; 53 } 54 55 int 56 main (void) 57 { 58 const int flags = IPC_CREAT | 0666; 59 key_t shmkey = 3925, semkey = 7428, msgkey = 5294; 60 FILE *fd; 61 pthread_t thread; 62 struct sockaddr_in sock_addr; 63 int sock; 64 unsigned short port; 65 socklen_t size; 66 int status, try, retries = 1000; 67 68 atexit (ipc_cleanup); 69 70 for (try = 0; try < retries; ++try) 71 { 72 shmid = shmget (shmkey, 4096, flags | IPC_EXCL); 73 if (shmid >= 0) 74 break; 75 76 ++shmkey; 77 } 78 79 if (shmid < 0) 80 { 81 printf ("Cannot create shared-memory region after %d tries.\n", retries); 82 return 1; 83 } 84 85 for (try = 0; try < retries; ++try) 86 { 87 semid = semget (semkey, 1, flags | IPC_EXCL); 88 if (semid >= 0) 89 break; 90 91 ++semkey; 92 } 93 94 if (semid < 0) 95 { 96 printf ("Cannot create semaphore after %d tries.\n", retries); 97 return 1; 98 } 99 100 for (try = 0; try < retries; ++try) 101 { 102 msqid = msgget (msgkey, flags | IPC_EXCL); 103 if (msqid >= 0) 104 break; 105 106 ++msgkey; 107 } 108 109 if (msqid < 0) 110 { 111 printf ("Cannot create message queue after %d tries.\n", retries); 112 return 1; 113 } 114 115 fd = fopen ("/dev/null", "r"); 116 117 /* Lock the mutex to prevent the new thread from finishing immediately. */ 118 pthread_mutex_lock (&mutex); 119 pthread_create (&thread, NULL, thread_proc, 0); 120 121 sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); 122 if (sock < 0) 123 { 124 printf ("Cannot create socket.\n"); 125 return 1; 126 } 127 128 sock_addr.sin_family = AF_INET; 129 sock_addr.sin_port = 0; /* Bind to a free port. */ 130 sock_addr.sin_addr.s_addr = htonl (INADDR_ANY); 131 132 status = bind (sock, (struct sockaddr *) &sock_addr, sizeof (sock_addr)); 133 if (status < 0) 134 { 135 printf ("Cannot bind socket.\n"); 136 return 1; 137 } 138 139 /* Find the assigned port number of the socket. */ 140 size = sizeof (sock_addr); 141 status = getsockname (sock, (struct sockaddr *) &sock_addr, &size); 142 if (status < 0) 143 { 144 printf ("Cannot find name of socket.\n"); 145 return 1; 146 } 147 port = ntohs (sock_addr.sin_port); 148 149 status = listen (sock, 1); 150 if (status < 0) 151 { 152 printf ("Cannot listen on socket.\n"); 153 return 1; 154 } 155 156 /* Set breakpoint here. */ 157 158 fclose (fd); 159 close (sock); 160 161 pthread_mutex_unlock (&mutex); 162 pthread_join (thread, NULL); 163 164 return 0; 165 } 166