3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on tests/testipc.cc by Jacques Gelinas
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)
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.
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.
21 Test to see isolation of the various IPC resources
22 between security context
32 int main (int argc, char *argv[])
39 }else if(strcmp(argv[1],"createshm")==0){
40 int id = shmget (1,1024,IPC_CREAT|0666);
42 fprintf (stderr,"shmget failed (%s)\n",strerror(errno));
44 printf ("shmget id %d\n",id);
45 void *pt = shmat (id,NULL,0);
47 fprintf (stderr,"can't shmat to id %d (%s)\n",id,strerror(errno));
49 strcpy ((char*)pt,"original string");
51 printf ("Letting a sub-program attach to this memory\n");
53 sprintf (tmp,"./testipc accessshm %d",id);
54 int ok = system (tmp);
55 printf ("\tSub-program returned %d\n",ok);
57 printf ("\tThe segment now hold :%s:\n",(char*)pt);
60 printf ("A sub-program in another context can't attach\n");
61 sprintf (tmp,"/usr/sbin/chcontext ./testipc accessshm %d",id);
63 printf ("\tSub-program returned %d\n",ok);
65 printf ("Executing a sub-shell\n");
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));
75 }else if(strcmp(argv[1],"accessshm")==0){
76 int id = atoi(argv[2]);
77 void *pt = shmat (id,NULL,0);
79 fprintf (stderr,"can't shmat to id %d (%s)\n",id,strerror(errno));
81 printf ("\tWriting hello in share memory\n");
82 strcpy ((char*)pt,"hello");
85 }else if(strcmp(argv[1],"createsem")==0){
86 int id = semget (1,1,IPC_CREAT|0666);
88 fprintf (stderr,"semget failed (%s)\n",strerror(errno));
90 printf ("semget id %d\n",id);
92 printf ("Letting a sub-program play with this semaphore\n");
94 sprintf (tmp,"./testipc accesssem %d",id);
95 int ok = system (tmp);
96 printf ("\tSub-program returned %d\n",ok);
98 printf ("A sub-program in another context can't use the semaphore\n");
99 sprintf (tmp,"/usr/sbin/chcontext ./testipc accesssem %d",id);
101 printf ("\tSub-program returned %d\n",ok);
103 printf ("Executing a sub-shell\n");
106 printf ("Delete the semaphore\n");
107 if (semctl (id,0,IPC_RMID,NULL)==-1){
108 fprintf (stderr,"semctl failed (%s)\n",strerror(errno));
113 }else if(strcmp(argv[1],"accesssem")==0){
114 int id = atoi(argv[2]);
115 struct sembuf ops[]={
118 if (semop (id,ops,1) == -1){
119 fprintf (stderr,"can't semop with id %d (%s)\n",id,strerror(errno));