1 /*
2 * Copyright (c) 2016 Xiao Yang <yangx.jy@cn.fujitsu.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.
16 */
17
18 /*
19 * DESCRIPTION
20 * common routines for the IPC system call tests.
21 */
22
23 #include <errno.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <sys/ipc.h>
29 #include <sys/msg.h>
30 #include <sys/shm.h>
31
32 #define TST_NO_DEFAULT_MAIN
33
34 #include "tst_test.h"
35 #include "libnewipc.h"
36
37 #define BUFSIZE 1024
38
getipckey(const char * file,const int lineno)39 key_t getipckey(const char *file, const int lineno)
40 {
41 char buf[BUFSIZE];
42 key_t key;
43 int id;
44 static int count;
45
46 SAFE_GETCWD(buf, BUFSIZE);
47
48 id = count % 26 + (int) 'a';
49 count++;
50
51 key = ftok(buf, id);
52 if (key == -1) {
53 tst_brk(TBROK | TERRNO,
54 "ftok() failed at %s:%d", file, lineno);
55 }
56
57 return key;
58 }
59
get_used_queues(const char * file,const int lineno)60 int get_used_queues(const char *file, const int lineno)
61 {
62 FILE *fp;
63 int used_queues = -1;
64 char buf[BUFSIZE];
65
66 fp = fopen("/proc/sysvipc/msg", "r");
67 if (fp == NULL) {
68 tst_brk(TBROK | TERRNO,
69 "fopen() failed at %s:%d", file, lineno);
70 }
71
72 while (fgets(buf, BUFSIZE, fp) != NULL)
73 used_queues++;
74
75 fclose(fp);
76
77 if (used_queues < 0) {
78 tst_brk(TBROK, "can't read /proc/sysvipc/msg to get "
79 "used message queues at %s:%d", file, lineno);
80 }
81
82 return used_queues;
83 }
84
probe_free_addr(const char * file,const int lineno)85 void *probe_free_addr(const char *file, const int lineno)
86 {
87 void *addr;
88 int shm_id = -1;
89 key_t probe_key = 0;
90
91 probe_key = GETIPCKEY();
92
93 shm_id = shmget(probe_key, SHMLBA * 2, SHM_RW | IPC_CREAT | IPC_EXCL);
94 if (shm_id == -1)
95 tst_brk(TBROK, "probe: shmget() failed at %s:%d", file, lineno);
96
97 addr = shmat(shm_id, NULL, 0);
98 if (addr == (void *) -1)
99 tst_brk(TBROK, "probe: shmat() failed at %s:%d", file, lineno);
100
101 if (shmdt(addr) == -1)
102 tst_brk(TBROK, "probe: shmdt() failed at %s:%d", file, lineno);
103
104 if (shmctl(shm_id, IPC_RMID, NULL) == -1)
105 tst_brk(TBROK, "probe: shmctl() failed at %s:%d", file, lineno);
106
107 addr = (void *)(((unsigned long)(addr) + (SHMLBA - 1)) & ~(SHMLBA - 1));
108
109 return addr;
110 }
111