- moved here from sysv/
[util-vserver.git] / tests / testipc.cc
1 // $Id: testipc.cc,v 1.1 2003/09/29 22:01:57 ensc Exp $
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on tests/testipc.cc by Jacques Gelinas
5 //  
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10 //  
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //  
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 /*
21         Test to see isolation of the various IPC resources
22         between security context
23 */
24 #include <stdio.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <sys/ipc.h>
29 #include <sys/shm.h>
30 #include <sys/sem.h>
31
32 int main (int argc, char *argv[])
33 {
34         int ret = -1;
35         if (argc < 2){
36                 fprintf (stderr,
37                         "testipc createshm\n"
38                         );
39         }else if(strcmp(argv[1],"createshm")==0){
40                 int id = shmget (1,1024,IPC_CREAT|0666);
41                 if (id == -1){
42                         fprintf (stderr,"shmget failed (%s)\n",strerror(errno));
43                 }else{
44                         printf ("shmget id %d\n",id);
45                         void *pt = shmat (id,NULL,0);
46                         if (pt == NULL){
47                                 fprintf (stderr,"can't shmat to id %d (%s)\n",id,strerror(errno));
48                         }else{
49                                 strcpy ((char*)pt,"original string");
50
51                                 printf ("Letting a sub-program attach to this memory\n");
52                                 char tmp[100];
53                                 sprintf (tmp,"./testipc accessshm %d",id);
54                                 int ok = system (tmp);
55                                 printf ("\tSub-program returned %d\n",ok);
56
57                                 printf ("\tThe segment now hold :%s:\n",(char*)pt);
58                                 shmdt (pt);
59
60                                 printf ("A sub-program in another context can't attach\n");
61                                 sprintf (tmp,"/usr/sbin/chcontext ./testipc accessshm %d",id);
62                                 ok = system (tmp);
63                                 printf ("\tSub-program returned %d\n",ok);
64
65                                 printf ("Executing a sub-shell\n");
66                                 system ("/bin/sh");
67                         }
68                         printf ("Delete the share memory segment\n");
69                         if (shmctl (id,IPC_RMID,NULL)==-1){
70                                 fprintf (stderr,"shmctl failed (%s)\n",strerror(errno));
71                         }else{
72                                 ret = 0;
73                         }
74                 }
75         }else if(strcmp(argv[1],"accessshm")==0){
76                 int id = atoi(argv[2]);
77                 void *pt = shmat (id,NULL,0);
78                 if (pt == (void*)-1){
79                         fprintf (stderr,"can't shmat to id %d (%s)\n",id,strerror(errno));
80                 }else{
81                         printf ("\tWriting hello in share memory\n");
82                         strcpy ((char*)pt,"hello");
83                         ret = 0;
84                 }
85         }else if(strcmp(argv[1],"createsem")==0){
86                 int id = semget (1,1,IPC_CREAT|0666);
87                 if (id == -1){
88                         fprintf (stderr,"semget failed (%s)\n",strerror(errno));
89                 }else{
90                         printf ("semget id %d\n",id);
91
92                         printf ("Letting a sub-program play with this semaphore\n");
93                         char tmp[100];
94                         sprintf (tmp,"./testipc accesssem %d",id);
95                         int ok = system (tmp);
96                         printf ("\tSub-program returned %d\n",ok);
97
98                         printf ("A sub-program in another context can't use the semaphore\n");
99                         sprintf (tmp,"/usr/sbin/chcontext ./testipc accesssem %d",id);
100                         ok = system (tmp);
101                         printf ("\tSub-program returned %d\n",ok);
102
103                         printf ("Executing a sub-shell\n");
104                         system ("/bin/sh");
105
106                         printf ("Delete the semaphore\n");
107                         if (semctl (id,0,IPC_RMID,NULL)==-1){
108                                 fprintf (stderr,"semctl failed (%s)\n",strerror(errno));
109                         }else{
110                                 ret = 0;
111                         }
112                 }
113         }else if(strcmp(argv[1],"accesssem")==0){
114                 int id = atoi(argv[2]);
115                 struct sembuf ops[]={
116                         {0,0,0}
117                 };
118                 if (semop (id,ops,1) == -1){
119                         fprintf (stderr,"can't semop with id %d (%s)\n",id,strerror(errno));
120                 }else{
121                         ret = 0;
122                 }
123         }
124         return ret;
125 }
126                 
127