1d9a28b23b0c13e8ed559311c6375500064e3b56
[util-vserver.git] / tests / testipc.c
1 // $Id: testipc.c,v 1.2 2004/01/13 14:56:26 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                         void *pt = shmat (id,NULL,0);
45                         printf ("shmget id %d\n",id);
46                         if (pt == NULL){
47                                 fprintf (stderr,"can't shmat to id %d (%s)\n",id,strerror(errno));
48                         }else{
49                                 char tmp[100];
50                                 int  ok;
51                                 strcpy ((char*)pt,"original string");
52
53                                 printf ("Letting a sub-program attach to this memory\n");
54                                 sprintf (tmp,"./testipc accessshm %d",id);
55                                 ok = system (tmp);
56                                 printf ("\tSub-program returned %d\n",ok);
57
58                                 printf ("\tThe segment now hold :%s:\n",(char*)pt);
59                                 shmdt (pt);
60
61                                 printf ("A sub-program in another context can't attach\n");
62                                 sprintf (tmp,"/usr/sbin/chcontext ./testipc accessshm %d",id);
63                                 ok = system (tmp);
64                                 printf ("\tSub-program returned %d\n",ok);
65
66                                 printf ("Executing a sub-shell\n");
67                                 system ("/bin/sh");
68                         }
69                         printf ("Delete the share memory segment\n");
70                         if (shmctl (id,IPC_RMID,NULL)==-1){
71                                 fprintf (stderr,"shmctl failed (%s)\n",strerror(errno));
72                         }else{
73                                 ret = 0;
74                         }
75                 }
76         }else if(strcmp(argv[1],"accessshm")==0){
77                 int id = atoi(argv[2]);
78                 void *pt = shmat (id,NULL,0);
79                 if (pt == (void*)-1){
80                         fprintf (stderr,"can't shmat to id %d (%s)\n",id,strerror(errno));
81                 }else{
82                         printf ("\tWriting hello in share memory\n");
83                         strcpy ((char*)pt,"hello");
84                         ret = 0;
85                 }
86         }else if(strcmp(argv[1],"createsem")==0){
87                 int id = semget (1,1,IPC_CREAT|0666);
88                 if (id == -1){
89                         fprintf (stderr,"semget failed (%s)\n",strerror(errno));
90                 }else{
91                         char tmp[100];
92                         int  ok;
93                         printf ("semget id %d\n",id);
94
95                         printf ("Letting a sub-program play with this semaphore\n");
96                         sprintf (tmp,"./testipc accesssem %d",id);
97                         ok = system (tmp);
98                         printf ("\tSub-program returned %d\n",ok);
99
100                         printf ("A sub-program in another context can't use the semaphore\n");
101                         sprintf (tmp,"/usr/sbin/chcontext ./testipc accesssem %d",id);
102                         ok = system (tmp);
103                         printf ("\tSub-program returned %d\n",ok);
104
105                         printf ("Executing a sub-shell\n");
106                         system ("/bin/sh");
107
108                         printf ("Delete the semaphore\n");
109                         if (semctl (id,0,IPC_RMID,NULL)==-1){
110                                 fprintf (stderr,"semctl failed (%s)\n",strerror(errno));
111                         }else{
112                                 ret = 0;
113                         }
114                 }
115         }else if(strcmp(argv[1],"accesssem")==0){
116                 int id = atoi(argv[2]);
117                 struct sembuf ops[]={
118                         {0,0,0}
119                 };
120                 if (semop (id,ops,1) == -1){
121                         fprintf (stderr,"can't semop with id %d (%s)\n",id,strerror(errno));
122                 }else{
123                         ret = 0;
124                 }
125         }
126         return ret;
127 }
128                 
129