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