377d3a0ebde00692bf827c33adb8545e02381338
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / info-os.c
1 /* This testcase is part of GDB, the GNU debugger.
2
3 Copyright 2011-2019 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
53 int
54 main (void)
55 {
56 const int flags = IPC_CREAT | 0666;
57 key_t shmkey = 3925, semkey = 7428, msgkey = 5294;
58 FILE *fd;
59 pthread_t thread;
60 struct sockaddr_in sock_addr;
61 int sock;
62 unsigned short port;
63 socklen_t size;
64 int status, try, retries = 1000;
65
66 atexit (ipc_cleanup);
67
68 for (try = 0; try < retries; ++try)
69 {
70 shmid = shmget (shmkey, 4096, flags | IPC_EXCL);
71 if (shmid >= 0)
72 break;
73
74 ++shmkey;
75 }
76
77 if (shmid < 0)
78 {
79 printf ("Cannot create shared-memory region after %d tries.\n", retries);
80 return 1;
81 }
82
83 for (try = 0; try < retries; ++try)
84 {
85 semid = semget (semkey, 1, flags | IPC_EXCL);
86 if (semid >= 0)
87 break;
88
89 ++semkey;
90 }
91
92 if (semid < 0)
93 {
94 printf ("Cannot create semaphore after %d tries.\n", retries);
95 return 1;
96 }
97
98 for (try = 0; try < retries; ++try)
99 {
100 msqid = msgget (msgkey, flags | IPC_EXCL);
101 if (msqid >= 0)
102 break;
103
104 ++msgkey;
105 }
106
107 if (msqid < 0)
108 {
109 printf ("Cannot create message queue after %d tries.\n", retries);
110 return 1;
111 }
112
113 fd = fopen ("/dev/null", "r");
114
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);
118
119 sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
120 if (sock < 0)
121 {
122 printf ("Cannot create socket.\n");
123 return 1;
124 }
125
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);
129
130 status = bind (sock, (struct sockaddr *) &sock_addr, sizeof (sock_addr));
131 if (status < 0)
132 {
133 printf ("Cannot bind socket.\n");
134 return 1;
135 }
136
137 /* Find the assigned port number of the socket. */
138 size = sizeof (sock_addr);
139 status = getsockname (sock, (struct sockaddr *) &sock_addr, &size);
140 if (status < 0)
141 {
142 printf ("Cannot find name of socket.\n");
143 return 1;
144 }
145 port = ntohs (sock_addr.sin_port);
146
147 status = listen (sock, 1);
148 if (status < 0)
149 {
150 printf ("Cannot listen on socket.\n");
151 return 1;
152 }
153
154 /* Set breakpoint here. */
155
156 fclose (fd);
157 close (sock);
158
159 pthread_mutex_unlock (&mutex);
160 pthread_join (thread, NULL);
161
162 return 0;
163 }
This page took 0.031227 seconds and 3 git commands to generate.