1 1.1 christos /* This testcase is part of GDB, the GNU debugger. 2 1.1 christos 3 1.11 christos Copyright 2011-2024 Free Software Foundation, Inc. 4 1.1 christos 5 1.1 christos This program is free software; you can redistribute it and/or modify 6 1.1 christos it under the terms of the GNU General Public License as published by 7 1.1 christos the Free Software Foundation; either version 3 of the License, or 8 1.1 christos (at your option) any later version. 9 1.1 christos 10 1.1 christos This program is distributed in the hope that it will be useful, 11 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 12 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 1.1 christos GNU General Public License for more details. 14 1.1 christos 15 1.1 christos You should have received a copy of the GNU General Public License 16 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 17 1.1 christos 18 1.1 christos #include <sys/shm.h> 19 1.1 christos #include <sys/sem.h> 20 1.1 christos #include <sys/msg.h> 21 1.1 christos #include <stdio.h> 22 1.1 christos #include <pthread.h> 23 1.1 christos #include <arpa/inet.h> 24 1.1 christos #include <sys/socket.h> 25 1.3 christos #include <unistd.h> 26 1.5 christos #include <stdlib.h> 27 1.1 christos 28 1.1 christos static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 29 1.1 christos 30 1.5 christos /* System V IPC identifiers. */ 31 1.5 christos static int shmid = -1, semid = -1, msqid = -1; 32 1.5 christos 33 1.5 christos /* Delete any System V IPC resources that were allocated. */ 34 1.5 christos 35 1.5 christos static void 36 1.5 christos ipc_cleanup (void) 37 1.5 christos { 38 1.5 christos if (shmid >= 0) 39 1.5 christos shmctl (shmid, IPC_RMID, NULL); 40 1.5 christos if (semid >= 0) 41 1.5 christos semctl (semid, 0, IPC_RMID, NULL); 42 1.5 christos if (msqid >= 0) 43 1.5 christos msgctl (msqid, IPC_RMID, NULL); 44 1.5 christos } 45 1.5 christos 46 1.1 christos void * 47 1.1 christos thread_proc (void *args) 48 1.1 christos { 49 1.1 christos pthread_mutex_lock (&mutex); 50 1.1 christos pthread_mutex_unlock (&mutex); 51 1.9 christos 52 1.9 christos return NULL; 53 1.1 christos } 54 1.1 christos 55 1.1 christos int 56 1.1 christos main (void) 57 1.1 christos { 58 1.1 christos const int flags = IPC_CREAT | 0666; 59 1.1 christos key_t shmkey = 3925, semkey = 7428, msgkey = 5294; 60 1.1 christos FILE *fd; 61 1.1 christos pthread_t thread; 62 1.1 christos struct sockaddr_in sock_addr; 63 1.1 christos int sock; 64 1.1 christos unsigned short port; 65 1.1 christos socklen_t size; 66 1.1 christos int status, try, retries = 1000; 67 1.1 christos 68 1.5 christos atexit (ipc_cleanup); 69 1.5 christos 70 1.1 christos for (try = 0; try < retries; ++try) 71 1.1 christos { 72 1.1 christos shmid = shmget (shmkey, 4096, flags | IPC_EXCL); 73 1.1 christos if (shmid >= 0) 74 1.1 christos break; 75 1.1 christos 76 1.1 christos ++shmkey; 77 1.1 christos } 78 1.1 christos 79 1.1 christos if (shmid < 0) 80 1.1 christos { 81 1.1 christos printf ("Cannot create shared-memory region after %d tries.\n", retries); 82 1.1 christos return 1; 83 1.1 christos } 84 1.1 christos 85 1.1 christos for (try = 0; try < retries; ++try) 86 1.1 christos { 87 1.1 christos semid = semget (semkey, 1, flags | IPC_EXCL); 88 1.1 christos if (semid >= 0) 89 1.1 christos break; 90 1.1 christos 91 1.1 christos ++semkey; 92 1.1 christos } 93 1.1 christos 94 1.1 christos if (semid < 0) 95 1.1 christos { 96 1.1 christos printf ("Cannot create semaphore after %d tries.\n", retries); 97 1.1 christos return 1; 98 1.1 christos } 99 1.1 christos 100 1.1 christos for (try = 0; try < retries; ++try) 101 1.1 christos { 102 1.1 christos msqid = msgget (msgkey, flags | IPC_EXCL); 103 1.1 christos if (msqid >= 0) 104 1.1 christos break; 105 1.1 christos 106 1.1 christos ++msgkey; 107 1.1 christos } 108 1.1 christos 109 1.1 christos if (msqid < 0) 110 1.1 christos { 111 1.1 christos printf ("Cannot create message queue after %d tries.\n", retries); 112 1.1 christos return 1; 113 1.1 christos } 114 1.1 christos 115 1.1 christos fd = fopen ("/dev/null", "r"); 116 1.1 christos 117 1.1 christos /* Lock the mutex to prevent the new thread from finishing immediately. */ 118 1.1 christos pthread_mutex_lock (&mutex); 119 1.1 christos pthread_create (&thread, NULL, thread_proc, 0); 120 1.1 christos 121 1.1 christos sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); 122 1.1 christos if (sock < 0) 123 1.1 christos { 124 1.1 christos printf ("Cannot create socket.\n"); 125 1.1 christos return 1; 126 1.1 christos } 127 1.1 christos 128 1.1 christos sock_addr.sin_family = AF_INET; 129 1.1 christos sock_addr.sin_port = 0; /* Bind to a free port. */ 130 1.1 christos sock_addr.sin_addr.s_addr = htonl (INADDR_ANY); 131 1.1 christos 132 1.1 christos status = bind (sock, (struct sockaddr *) &sock_addr, sizeof (sock_addr)); 133 1.1 christos if (status < 0) 134 1.1 christos { 135 1.1 christos printf ("Cannot bind socket.\n"); 136 1.1 christos return 1; 137 1.1 christos } 138 1.1 christos 139 1.1 christos /* Find the assigned port number of the socket. */ 140 1.1 christos size = sizeof (sock_addr); 141 1.1 christos status = getsockname (sock, (struct sockaddr *) &sock_addr, &size); 142 1.1 christos if (status < 0) 143 1.1 christos { 144 1.1 christos printf ("Cannot find name of socket.\n"); 145 1.1 christos return 1; 146 1.1 christos } 147 1.1 christos port = ntohs (sock_addr.sin_port); 148 1.1 christos 149 1.1 christos status = listen (sock, 1); 150 1.1 christos if (status < 0) 151 1.1 christos { 152 1.1 christos printf ("Cannot listen on socket.\n"); 153 1.1 christos return 1; 154 1.1 christos } 155 1.1 christos 156 1.1 christos /* Set breakpoint here. */ 157 1.1 christos 158 1.1 christos fclose (fd); 159 1.1 christos close (sock); 160 1.1 christos 161 1.1 christos pthread_mutex_unlock (&mutex); 162 1.1 christos pthread_join (thread, NULL); 163 1.1 christos 164 1.1 christos return 0; 165 1.1 christos } 166