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