1 // $Id: testipc.c,v 1.2 2004/01/13 14:56:26 ensc Exp $
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 void *pt = shmat (id,NULL,0);
45 printf ("shmget id %d\n",id);
47 fprintf (stderr,"can't shmat to id %d (%s)\n",id,strerror(errno));
51 strcpy ((char*)pt,"original string");
53 printf ("Letting a sub-program attach to this memory\n");
54 sprintf (tmp,"./testipc accessshm %d",id);
56 printf ("\tSub-program returned %d\n",ok);
58 printf ("\tThe segment now hold :%s:\n",(char*)pt);
61 printf ("A sub-program in another context can't attach\n");
62 sprintf (tmp,"/usr/sbin/chcontext ./testipc accessshm %d",id);
64 printf ("\tSub-program returned %d\n",ok);
66 printf ("Executing a sub-shell\n");
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));
76 }else if(strcmp(argv[1],"accessshm")==0){
77 int id = atoi(argv[2]);
78 void *pt = shmat (id,NULL,0);
80 fprintf (stderr,"can't shmat to id %d (%s)\n",id,strerror(errno));
82 printf ("\tWriting hello in share memory\n");
83 strcpy ((char*)pt,"hello");
86 }else if(strcmp(argv[1],"createsem")==0){
87 int id = semget (1,1,IPC_CREAT|0666);
89 fprintf (stderr,"semget failed (%s)\n",strerror(errno));
93 printf ("semget id %d\n",id);
95 printf ("Letting a sub-program play with this semaphore\n");
96 sprintf (tmp,"./testipc accesssem %d",id);
98 printf ("\tSub-program returned %d\n",ok);
100 printf ("A sub-program in another context can't use the semaphore\n");
101 sprintf (tmp,"/usr/sbin/chcontext ./testipc accesssem %d",id);
103 printf ("\tSub-program returned %d\n",ok);
105 printf ("Executing a sub-shell\n");
108 printf ("Delete the semaphore\n");
109 if (semctl (id,0,IPC_RMID,NULL)==-1){
110 fprintf (stderr,"semctl failed (%s)\n",strerror(errno));
115 }else if(strcmp(argv[1],"accesssem")==0){
116 int id = atoi(argv[2]);
117 struct sembuf ops[]={
120 if (semop (id,ops,1) == -1){
121 fprintf (stderr,"can't semop with id %d (%s)\n",id,strerror(errno));