2016-04-18 10:10:54 +03:00
# include "tests.h"
# include <stdio.h>
# include <stdlib.h>
# include <sys/shm.h>
static int id = - 1 ;
static void
cleanup ( void )
{
shmctl ( id , IPC_RMID , NULL ) ;
id = - 1 ;
}
# ifdef __alpha__
# define SHMAT "osf_shmat"
# else
# define SHMAT "shmat"
# endif
2017-11-17 14:26:55 +03:00
# ifndef SHM_EXEC
# define SHM_EXEC 0100000
# endif
2016-04-18 10:10:54 +03:00
int
main ( void )
{
2016-09-13 19:18:42 +03:00
static const int bogus_shmid = 0xfdb97531 ;
static const void * const bogus_shmaddr =
( void * ) ( unsigned long ) 0xdec0ded1dec0ded2ULL ;
static const int bogus_shmflg = 0xffffface ;
long rc ;
2016-04-18 10:10:54 +03:00
id = shmget ( IPC_PRIVATE , 1 , 0600 ) ;
if ( id < 0 )
perror_msg_and_skip ( " shmget " ) ;
atexit ( cleanup ) ;
2016-09-13 19:18:42 +03:00
rc = ( long ) shmat ( bogus_shmid , bogus_shmaddr , bogus_shmflg ) ;
2017-11-17 03:24:41 +03:00
printf ( " %s(%d, %p, SHM_RDONLY|SHM_RND|SHM_REMAP|SHM_EXEC|%#x) = %s \n " ,
SHMAT , bogus_shmid , bogus_shmaddr , bogus_shmflg & ~ 0xf000 ,
2016-09-13 19:18:42 +03:00
sprintrc ( rc ) ) ;
2016-04-18 10:10:54 +03:00
shmat ( id , NULL , SHM_REMAP ) ;
printf ( " %s(%d, NULL, SHM_REMAP) = -1 %s (%m) \n " ,
2016-04-21 20:49:32 +03:00
SHMAT , id , errno2name ( ) ) ;
2016-04-18 10:10:54 +03:00
void * shmaddr = shmat ( id , NULL , SHM_RDONLY ) ;
if ( shmaddr = = ( void * ) ( - 1 ) )
perror_msg_and_skip ( " shmat SHM_RDONLY " ) ;
printf ( " %s(%d, NULL, SHM_RDONLY) = %p \n " , SHMAT , id , shmaddr ) ;
2016-09-13 19:18:42 +03:00
rc = shmdt ( NULL ) ;
printf ( " shmdt(NULL) = %s \n " , sprintrc ( rc ) ) ;
2017-11-17 03:24:41 +03:00
rc = shmdt ( shmaddr ) ;
printf ( " shmdt(%p) = %s \n " , shmaddr , sprintrc ( rc ) ) ;
2016-04-18 10:10:54 +03:00
2016-04-23 04:53:43 +03:00
+ + shmaddr ;
void * shmaddr2 = shmat ( id , shmaddr , SHM_RND ) ;
2016-04-18 10:10:54 +03:00
if ( shmaddr2 = = ( void * ) ( - 1 ) )
2016-04-23 04:53:43 +03:00
printf ( " %s(%d, %p, SHM_RND) = -1 %s (%m) \n " ,
SHMAT , id , shmaddr , errno2name ( ) ) ;
2017-11-17 03:24:41 +03:00
else {
2016-04-23 04:53:43 +03:00
printf ( " %s(%d, %p, SHM_RND) = %p \n " ,
SHMAT , id , shmaddr , shmaddr2 ) ;
2017-11-17 03:24:41 +03:00
rc = shmdt ( shmaddr2 ) ;
printf ( " shmdt(%p) = %s \n " , shmaddr2 , sprintrc ( rc ) ) ;
}
shmaddr = shmat ( id , NULL , SHM_RDONLY | SHM_EXEC ) ;
if ( shmaddr = = ( void * ) ( - 1 ) )
printf ( " %s(%d, NULL, SHM_RDONLY|SHM_EXEC) = %s \n " ,
SHMAT , id , sprintrc ( - 1 ) ) ;
else {
printf ( " %s(%d, NULL, SHM_RDONLY|SHM_EXEC) = %p \n " ,
SHMAT , id , shmaddr ) ;
rc = shmdt ( shmaddr ) ;
printf ( " shmdt(%p) = %s \n " , shmaddr , sprintrc ( rc ) ) ;
}
2016-04-18 10:10:54 +03:00
puts ( " +++ exited with 0 +++ " ) ;
return 0 ;
}