2012-05-11 Stan Shebs <stan@codesourcery.com>
[platform/upstream/binutils.git] / gdb / testsuite / gdb.base / info-os.c
1 /* This testcase is part of GDB, the GNU debugger.
2
3    Copyright 2011 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
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;
39   int shmid, semid, msqid;
40   FILE *fd;
41   pthread_t thread;
42   struct sockaddr_in sock_addr;
43   int sock;
44   unsigned short port;
45   socklen_t size;
46   int status;
47
48   if ((shmid = shmget (3925, 4096, flags | IPC_EXCL)) < 0)
49     {
50       /* Attempt to delete the existing shared-memory region, then
51          recreate it.  */
52       shmctl (shmget (3925, 4096, flags), IPC_RMID, NULL);
53       if ((shmid = shmget (3925, 4096, flags | IPC_EXCL)) < 0)
54         {
55           printf ("Cannot create shared-memory region.\n");
56           return 1;
57         }         
58     }
59
60   semid = semget (7428, 1, flags);
61   msqid = msgget (5294, flags);
62   fd = fopen ("/dev/null", "r");
63
64   /* Lock the mutex to prevent the new thread from finishing immediately.  */
65   pthread_mutex_lock (&mutex);
66   pthread_create (&thread, NULL, thread_proc, 0);
67  
68   sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
69   if (sock < 0)
70     {
71       printf ("Cannot create socket.\n");
72       return 1;
73     }
74  
75   sock_addr.sin_family = AF_INET;
76   sock_addr.sin_port = 0; /* Bind to a free port.  */
77   sock_addr.sin_addr.s_addr = htonl (INADDR_ANY);
78
79   status = bind (sock, (struct sockaddr *) &sock_addr, sizeof (sock_addr));
80   if (status < 0)
81     {
82       printf ("Cannot bind socket.\n");
83       return 1;
84     }
85
86   /* Find the assigned port number of the socket.  */
87   size = sizeof (sock_addr);
88   status = getsockname (sock, (struct sockaddr *) &sock_addr, &size);
89   if (status < 0)
90     {
91       printf ("Cannot find name of socket.\n");
92       return 1;
93     }
94   port = ntohs (sock_addr.sin_port);
95
96   status = listen (sock, 1);
97   if (status < 0)
98     {
99       printf ("Cannot listen on socket.\n");
100       return 1;
101     }
102
103   /* Set breakpoint here.  */
104
105   shmctl (shmid, IPC_RMID, NULL);
106   semctl (semid, 0, IPC_RMID, NULL);
107   msgctl (msqid, IPC_RMID, NULL);
108   fclose (fd);
109   close (sock);
110
111   pthread_mutex_unlock (&mutex);
112   pthread_join (thread, NULL);
113
114   return 0;
115 }