• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2016 Xiao Yang <yangx.jy@cn.fujitsu.com>
4  */
5 
6 /*
7  * DESCRIPTION
8  * common routines for the IPC system call tests.
9  */
10 
11 #include <errno.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sys/types.h>
16 #include <sys/ipc.h>
17 #include <sys/msg.h>
18 #include <sys/shm.h>
19 
20 #define	TST_NO_DEFAULT_MAIN
21 
22 #include "tst_test.h"
23 #include "libnewipc.h"
24 #include "tst_safe_stdio.h"
25 #include "tst_safe_sysv_ipc.h"
26 
27 #define BUFSIZE 1024
28 
getipckey(const char * file,const int lineno)29 key_t getipckey(const char *file, const int lineno)
30 {
31 	char buf[BUFSIZE];
32 	key_t key;
33 	int id;
34 	static int count;
35 
36 	safe_getcwd(file, lineno, NULL, buf, BUFSIZE);
37 
38 	id = count % 26 + (int) 'a';
39 	count++;
40 
41 	key = ftok(buf, id);
42 	if (key == -1) {
43 		tst_brk(TBROK | TERRNO,
44 			"ftok() failed at %s:%d", file, lineno);
45 	}
46 
47 	return key;
48 }
49 
get_used_sysvipc(const char * file,const int lineno,const char * sysvipc_file)50 int get_used_sysvipc(const char *file, const int lineno, const char *sysvipc_file)
51 {
52 	FILE *fp;
53 	int used = -1;
54 	char buf[BUFSIZE];
55 
56 	fp = safe_fopen(file, lineno, NULL, sysvipc_file, "r");
57 
58 	while (fgets(buf, BUFSIZE, fp) != NULL)
59 		used++;
60 
61 	fclose(fp);
62 
63 	if (used < 0) {
64 		tst_brk(TBROK, "can't read %s to get used sysvipc resource total at "
65 			"%s:%d", sysvipc_file, file, lineno);
66 	}
67 
68 	return used;
69 }
70 
probe_free_addr(const char * file,const int lineno)71 void *probe_free_addr(const char *file, const int lineno)
72 {
73 	void *addr;
74 	int shm_id = -1;
75 	key_t probe_key = 0;
76 
77 	probe_key = GETIPCKEY();
78 
79 	shm_id = safe_shmget(file, lineno, probe_key, SHMLBA * 2,
80 			     SHM_RW | IPC_CREAT | IPC_EXCL);
81 	addr = safe_shmat(file, lineno, shm_id, NULL, 0);
82 	safe_shmdt(file, lineno, addr);
83 	safe_shmctl(file, lineno, shm_id, IPC_RMID, NULL);
84 
85 	addr = (void *)(((unsigned long)(addr) + (SHMLBA - 1)) & ~(SHMLBA - 1));
86 
87 	return addr;
88 }
89