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