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 shm_open does not open the shared memory object for writing when
     11  * calling shm_open with O_RDONLY even if the mode set write permission.
     12  *
     13  * The test use ftruncate to check the object is not open for writing.
     14  */
     15 
     16 /* ftruncate was formerly an XOPEN extension. We define _XOPEN_SOURCE here to
     17    avoid warning if the implementation does not program ftruncate as a base
     18    interface */
     19 #define _XOPEN_SOURCE 600
     20 
     21 #include <stdio.h>
     22 #include <sys/mman.h>
     23 #include <sys/stat.h>
     24 #include <unistd.h>
     25 #include <fcntl.h>
     26 #include <errno.h>
     27 #include "posixtest.h"
     28 
     29 #define BUF_SIZE 8
     30 #define SHM_NAME "posixtest_20-1"
     31 
     32 int main(void)
     33 {
     34 	int fd, result;
     35 
     36 	result = shm_unlink(SHM_NAME);
     37 	if (result != 0 && errno != ENOENT) {
     38 		/* The shared memory object exist and shm_unlink can not
     39 		   remove it. */
     40 		perror("An error occurs when calling shm_unlink()");
     41 		return PTS_UNRESOLVED;
     42 	}
     43 
     44 	fd = shm_open(SHM_NAME, O_RDONLY | O_CREAT, S_IRUSR);
     45 	if (fd == -1) {
     46 		perror("An error occurs when calling shm_open()");
     47 		return PTS_UNRESOLVED;
     48 	}
     49 
     50 	result = ftruncate(fd, BUF_SIZE);
     51 
     52 	if (result == -1 && errno == EINVAL) {
     53 		printf("Test PASSED\n");
     54 		shm_unlink(SHM_NAME);
     55 		return PTS_PASS;
     56 	} else if (result == 0) {
     57 		printf("The shared memory object is opened for writing.\n");
     58 		shm_unlink(SHM_NAME);
     59 		return PTS_FAIL;
     60 	}
     61 
     62 	perror("ftruncate");
     63 	shm_unlink(SHM_NAME);
     64 	return PTS_FAIL;
     65 }
     66