The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/tools/tests/xnu_quick_test/shared_memory_tests.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  *  shared_memory_tests.c
    3  *  xnu_quick_test
    4  *
    5  *  Created by Jerry Cottingham on 6/2/2005.
    6  *  Copyright 2005 Apple Computer Inc. All rights reserved.
    7  *
    8  */
    9 
   10 #include "tests.h"
   11 #include <sys/ipc.h>
   12 #include <sys/mman.h>
   13 #include <sys/shm.h>
   14 
   15 extern char  g_target_path[ PATH_MAX ];
   16 
   17 
   18 /*  **************************************************************************************************************
   19  *      Test shmat, shmctl, shmdt, shmget system calls.
   20  *  **************************************************************************************************************
   21  */
   22 int shm_tests( void * the_argp )
   23 {
   24         int                                     my_err;
   25         int                                     my_shm_id;
   26         void *                          my_shm_addr = NULL;
   27         struct shmid_ds         my_shmid_ds;
   28 
   29         my_shm_id = shmget( IPC_PRIVATE, 4096, (IPC_CREAT | IPC_R | IPC_W) );
   30         if ( my_shm_id == -1 ) {
   31                 printf( "shmget failed with error %d - \"%s\" \n", errno, strerror( errno) );
   32                 goto test_failed_exit;
   33         }
   34 
   35         my_shm_addr = shmat( my_shm_id, NULL, SHM_RND );
   36         if ( my_shm_addr == (void *) -1 ) {
   37                 my_shm_addr = NULL;
   38                 printf( "shmat failed with error %d - \"%s\" \n", errno, strerror( errno) );
   39                 goto test_failed_exit;
   40         }
   41 
   42         /* try writing to the shared segment */
   43         *((char *) my_shm_addr) = 'A';
   44 
   45         my_err = shmctl( my_shm_id, IPC_STAT, &my_shmid_ds );
   46         if ( my_err == -1 ) {
   47                 printf( "shmctl failed with error %d - \"%s\" \n", errno, strerror( errno) );
   48                 goto test_failed_exit;
   49         }
   50         if ( my_shmid_ds.shm_segsz != 4096 ) {
   51                 printf( "shmctl failed get correct shared segment size \n" );
   52                 goto test_failed_exit;
   53         }
   54         if ( getpid( ) != my_shmid_ds.shm_cpid ) {
   55                 printf( "shmctl failed get correct creator pid \n" );
   56                 goto test_failed_exit;
   57         }
   58 
   59         my_err = shmdt( my_shm_addr );
   60         if ( my_err == -1 ) {
   61                 printf( "shmdt failed with error %d - \"%s\" \n", errno, strerror( errno) );
   62                 goto test_failed_exit;
   63         }
   64         my_shm_addr = NULL;
   65          
   66         my_err = 0;
   67         goto test_passed_exit;
   68 
   69 test_failed_exit:
   70         my_err = -1;
   71         
   72 test_passed_exit:
   73         if ( my_shm_addr != NULL ) {
   74                 shmdt( my_shm_addr );
   75         }
   76         return( my_err );
   77 }
   78 
   79 
   80 /*  **************************************************************************************************************
   81  *      Test BSD shared memory system calls.
   82  *  **************************************************************************************************************
   83  */
   84 int bsd_shm_tests( void * the_argp )
   85 {
   86         int                     my_err, i;
   87         int                     my_fd = -1;
   88         char *          my_addr = NULL;
   89         char            my_name[ 64 ];
   90 
   91         for ( i = 0; i < 100; i++ ) {
   92                 sprintf( &my_name[0], "bsd_shm_tests_%d", i );
   93                 my_fd = shm_open( &my_name[0], (O_RDWR | O_CREAT | O_EXCL), S_IRWXU );
   94                 if ( my_fd != -1 ) 
   95                         break;
   96                 my_err = errno;
   97                 if ( my_err != EEXIST ) {
   98                         printf( "shm_open failed with error %d - \"%s\" \n", my_err, strerror( my_err) );
   99                         goto test_failed_exit;
  100                 }
  101         }
  102         if ( my_fd == -1 ) {
  103                 printf( "shm_open failed to open a shared memory object with name \"%s\" \n", &my_name[0] );
  104                 goto test_failed_exit;
  105         }
  106         
  107         /* grow shared memory object */
  108         my_err = ftruncate( my_fd, 4096 );              
  109         if ( my_err == -1 ) {
  110                 printf( "ftruncate call failed with error %d - \"%s\" \n", errno, strerror( errno) );
  111                 goto test_failed_exit;
  112         }
  113 
  114         my_err = shm_unlink( &my_name[0] );
  115         if ( my_err == -1 ) {
  116                 printf( "shm_unlink failed with error %d - \"%s\" \n", errno, strerror( errno) );
  117                 goto test_failed_exit;
  118         }
  119         
  120         my_addr = (char *) mmap( NULL, 4096, (PROT_READ | PROT_WRITE), (MAP_FILE | MAP_SHARED), my_fd, 0 );
  121         if ( my_addr == (char *) -1 ) {
  122                 printf( "mmap call failed with error %d - \"%s\" \n", errno, strerror( errno) );
  123                 goto test_failed_exit;
  124         }
  125         
  126         my_err = 0;
  127         goto test_passed_exit;
  128 
  129 test_failed_exit:
  130         my_err = -1;
  131         
  132 test_passed_exit:
  133         if ( my_fd != -1 )
  134                 close( my_fd );
  135         return( my_err );
  136 }
  137 

Cache object: 95d861de01f9715d84e6b8bcfa3615a4


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.