18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci#define _GNU_SOURCE
48c2ecf20Sopenharmony_ci#include <errno.h>
58c2ecf20Sopenharmony_ci#include <fcntl.h>
68c2ecf20Sopenharmony_ci#include <inttypes.h>
78c2ecf20Sopenharmony_ci#include <limits.h>
88c2ecf20Sopenharmony_ci#include <linux/types.h>
98c2ecf20Sopenharmony_ci#include <sched.h>
108c2ecf20Sopenharmony_ci#include <signal.h>
118c2ecf20Sopenharmony_ci#include <stdbool.h>
128c2ecf20Sopenharmony_ci#include <stdio.h>
138c2ecf20Sopenharmony_ci#include <stdlib.h>
148c2ecf20Sopenharmony_ci#include <string.h>
158c2ecf20Sopenharmony_ci#include <syscall.h>
168c2ecf20Sopenharmony_ci#include <sys/mount.h>
178c2ecf20Sopenharmony_ci#include <sys/prctl.h>
188c2ecf20Sopenharmony_ci#include <sys/wait.h>
198c2ecf20Sopenharmony_ci#include <unistd.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include "pidfd.h"
228c2ecf20Sopenharmony_ci#include "../kselftest.h"
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic int safe_int(const char *numstr, int *converted)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	char *err = NULL;
278c2ecf20Sopenharmony_ci	long sli;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	errno = 0;
308c2ecf20Sopenharmony_ci	sli = strtol(numstr, &err, 0);
318c2ecf20Sopenharmony_ci	if (errno == ERANGE && (sli == LONG_MAX || sli == LONG_MIN))
328c2ecf20Sopenharmony_ci		return -ERANGE;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	if (errno != 0 && sli == 0)
358c2ecf20Sopenharmony_ci		return -EINVAL;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	if (err == numstr || *err != '\0')
388c2ecf20Sopenharmony_ci		return -EINVAL;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	if (sli > INT_MAX || sli < INT_MIN)
418c2ecf20Sopenharmony_ci		return -ERANGE;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	*converted = (int)sli;
448c2ecf20Sopenharmony_ci	return 0;
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic int char_left_gc(const char *buffer, size_t len)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	size_t i;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	for (i = 0; i < len; i++) {
528c2ecf20Sopenharmony_ci		if (buffer[i] == ' ' ||
538c2ecf20Sopenharmony_ci		    buffer[i] == '\t')
548c2ecf20Sopenharmony_ci			continue;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci		return i;
578c2ecf20Sopenharmony_ci	}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	return 0;
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic int char_right_gc(const char *buffer, size_t len)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	int i;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	for (i = len - 1; i >= 0; i--) {
678c2ecf20Sopenharmony_ci		if (buffer[i] == ' '  ||
688c2ecf20Sopenharmony_ci		    buffer[i] == '\t' ||
698c2ecf20Sopenharmony_ci		    buffer[i] == '\n' ||
708c2ecf20Sopenharmony_ci		    buffer[i] == '\0')
718c2ecf20Sopenharmony_ci			continue;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci		return i + 1;
748c2ecf20Sopenharmony_ci	}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	return 0;
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic char *trim_whitespace_in_place(char *buffer)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	buffer += char_left_gc(buffer, strlen(buffer));
828c2ecf20Sopenharmony_ci	buffer[char_right_gc(buffer, strlen(buffer))] = '\0';
838c2ecf20Sopenharmony_ci	return buffer;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic pid_t get_pid_from_fdinfo_file(int pidfd, const char *key, size_t keylen)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	int ret;
898c2ecf20Sopenharmony_ci	char path[512];
908c2ecf20Sopenharmony_ci	FILE *f;
918c2ecf20Sopenharmony_ci	size_t n = 0;
928c2ecf20Sopenharmony_ci	pid_t result = -1;
938c2ecf20Sopenharmony_ci	char *line = NULL;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", pidfd);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	f = fopen(path, "re");
988c2ecf20Sopenharmony_ci	if (!f)
998c2ecf20Sopenharmony_ci		return -1;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	while (getline(&line, &n, f) != -1) {
1028c2ecf20Sopenharmony_ci		char *numstr;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci		if (strncmp(line, key, keylen))
1058c2ecf20Sopenharmony_ci			continue;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci		numstr = trim_whitespace_in_place(line + 4);
1088c2ecf20Sopenharmony_ci		ret = safe_int(numstr, &result);
1098c2ecf20Sopenharmony_ci		if (ret < 0)
1108c2ecf20Sopenharmony_ci			goto out;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci		break;
1138c2ecf20Sopenharmony_ci	}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ciout:
1168c2ecf20Sopenharmony_ci	free(line);
1178c2ecf20Sopenharmony_ci	fclose(f);
1188c2ecf20Sopenharmony_ci	return result;
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ciint main(int argc, char **argv)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	int pidfd = -1, ret = 1;
1248c2ecf20Sopenharmony_ci	pid_t pid;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	ksft_set_plan(3);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	pidfd = sys_pidfd_open(-1, 0);
1298c2ecf20Sopenharmony_ci	if (pidfd >= 0) {
1308c2ecf20Sopenharmony_ci		ksft_print_msg(
1318c2ecf20Sopenharmony_ci			"%s - succeeded to open pidfd for invalid pid -1\n",
1328c2ecf20Sopenharmony_ci			strerror(errno));
1338c2ecf20Sopenharmony_ci		goto on_error;
1348c2ecf20Sopenharmony_ci	}
1358c2ecf20Sopenharmony_ci	ksft_test_result_pass("do not allow invalid pid test: passed\n");
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	pidfd = sys_pidfd_open(getpid(), 1);
1388c2ecf20Sopenharmony_ci	if (pidfd >= 0) {
1398c2ecf20Sopenharmony_ci		ksft_print_msg(
1408c2ecf20Sopenharmony_ci			"%s - succeeded to open pidfd with invalid flag value specified\n",
1418c2ecf20Sopenharmony_ci			strerror(errno));
1428c2ecf20Sopenharmony_ci		goto on_error;
1438c2ecf20Sopenharmony_ci	}
1448c2ecf20Sopenharmony_ci	ksft_test_result_pass("do not allow invalid flag test: passed\n");
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	pidfd = sys_pidfd_open(getpid(), 0);
1478c2ecf20Sopenharmony_ci	if (pidfd < 0) {
1488c2ecf20Sopenharmony_ci		ksft_print_msg("%s - failed to open pidfd\n", strerror(errno));
1498c2ecf20Sopenharmony_ci		goto on_error;
1508c2ecf20Sopenharmony_ci	}
1518c2ecf20Sopenharmony_ci	ksft_test_result_pass("open a new pidfd test: passed\n");
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	pid = get_pid_from_fdinfo_file(pidfd, "Pid:", sizeof("Pid:") - 1);
1548c2ecf20Sopenharmony_ci	ksft_print_msg("pidfd %d refers to process with pid %d\n", pidfd, pid);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	ret = 0;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cion_error:
1598c2ecf20Sopenharmony_ci	if (pidfd >= 0)
1608c2ecf20Sopenharmony_ci		close(pidfd);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	return !ret ? ksft_exit_pass() : ksft_exit_fail();
1638c2ecf20Sopenharmony_ci}
164