Home | History | Annotate | Download | only in shm_open
      1 /*
      2  *  This program is free software; you can redistribute it and/or modify
      3  *  it under the terms of the GNU General Public License version 2.
      4  *
      5  *  This program is distributed in the hope that it will be useful,
      6  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      7  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      8  *  GNU General Public License for more details.
      9  *
     10  * Test that the shm_open() function establish a connection between a shared
     11  * memory object and a file descriptor.
     12  *
     13  * Step:
     14  *  1. Create a file a shared memory object using shm_open().
     15  *  2. Write in the file referenced by the file descriptor return by
     16  *     shm_open().
     17  *  3. Reopen the file with shm_open().
     18  *  4. Read in the file.
     19  * The test pass if it read what it was previously written.
     20  */
     21 
     22 /* ftruncate was formerly an XOPEN extension. We define _XOPEN_SOURCE here to
     23    avoid warning if the implementation does not program ftruncate as a base
     24    interface */
     25 #define _XOPEN_SOURCE 600
     26 
     27 #include <stdio.h>
     28 #include <sys/mman.h>
     29 #include <sys/stat.h>
     30 #include <unistd.h>
     31 #include <string.h>
     32 #include <fcntl.h>
     33 #include "posixtest.h"
     34 
     35 #define BUF_SIZE 8
     36 #define SHM_NAME "posixtest_1-1"
     37 
     38 int main(void)
     39 {
     40 	int fd;
     41 	char str[BUF_SIZE] = "qwerty";
     42 	char *buf;
     43 
     44 	fd = shm_open(SHM_NAME, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
     45 	if (fd == -1) {
     46 		perror("An error occurs when calling shm_open()");
     47 		return PTS_UNRESOLVED;
     48 	}
     49 
     50 	if (ftruncate(fd, BUF_SIZE) != 0) {
     51 		perror("An error occurs when calling ftruncate()");
     52 		shm_unlink(SHM_NAME);
     53 		return PTS_UNRESOLVED;
     54 	}
     55 
     56 	buf = mmap(NULL, BUF_SIZE, PROT_WRITE, MAP_SHARED, fd, 0);
     57 	if (buf == MAP_FAILED) {
     58 		perror("An error occurs when calling mmap()");
     59 		shm_unlink(SHM_NAME);
     60 		return PTS_UNRESOLVED;
     61 	}
     62 
     63 	strcpy(buf, str);
     64 
     65 	fd = shm_open(SHM_NAME, O_RDONLY, S_IRUSR | S_IWUSR);
     66 	if (fd == -1) {
     67 		perror("An error occurs when calling shm_open()");
     68 		shm_unlink(SHM_NAME);
     69 		return PTS_UNRESOLVED;
     70 	}
     71 
     72 	buf = mmap(NULL, BUF_SIZE, PROT_READ, MAP_SHARED, fd, 0);
     73 	if (buf == MAP_FAILED) {
     74 		perror("An error occurs when calling mmap()");
     75 		shm_unlink(SHM_NAME);
     76 		return PTS_UNRESOLVED;
     77 	}
     78 
     79 	shm_unlink(SHM_NAME);
     80 	if (strcmp(buf, str) == 0) {
     81 		printf("Test PASSED\n");
     82 		return PTS_PASS;
     83 	}
     84 	printf("Test FAILED\n");
     85 	return PTS_FAIL;
     86 }
     87