1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001
4f08c3bdfSopenharmony_ci *  07/2001 Ported by John George
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*
8f08c3bdfSopenharmony_ci * umask(2) sets the mask from 0000 to 0777 while we create files,
9f08c3bdfSopenharmony_ci * the previous value of the mask should be returned correctly,
10f08c3bdfSopenharmony_ci * and the file mode should be correct for each creation mask.
11f08c3bdfSopenharmony_ci */
12f08c3bdfSopenharmony_ci
13f08c3bdfSopenharmony_ci#include <errno.h>
14f08c3bdfSopenharmony_ci#include <stdio.h>
15f08c3bdfSopenharmony_ci#include <sys/types.h>
16f08c3bdfSopenharmony_ci#include <sys/stat.h>
17f08c3bdfSopenharmony_ci#include "tst_test.h"
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_cistatic void verify_umask(void)
20f08c3bdfSopenharmony_ci{
21f08c3bdfSopenharmony_ci	struct stat statbuf;
22f08c3bdfSopenharmony_ci	int mskval;
23f08c3bdfSopenharmony_ci	int fd;
24f08c3bdfSopenharmony_ci	int failflag = 0;
25f08c3bdfSopenharmony_ci	unsigned low9mode;
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci	for (mskval = 0000; mskval < 01000; mskval++) {
28f08c3bdfSopenharmony_ci		TEST(umask(mskval));
29f08c3bdfSopenharmony_ci		if (TST_RET < 0 || TST_RET > 0777) {
30f08c3bdfSopenharmony_ci			tst_brk(TFAIL, "umask(%o) result outside range %ld",
31f08c3bdfSopenharmony_ci				mskval, TST_RET);
32f08c3bdfSopenharmony_ci		}
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci		if (mskval > 0000 && TST_RET != mskval - 1) {
35f08c3bdfSopenharmony_ci			failflag = 1;
36f08c3bdfSopenharmony_ci			tst_res(TFAIL, "umask(%o) returned %ld, expected %d",
37f08c3bdfSopenharmony_ci				mskval, TST_RET, mskval - 1);
38f08c3bdfSopenharmony_ci		}
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci		fd = SAFE_CREAT("testfile", 0777);
41f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci		SAFE_STAT("testfile", &statbuf);
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci		low9mode = statbuf.st_mode & 0777;
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci		if (low9mode != (~mskval & 0777)) {
48f08c3bdfSopenharmony_ci			failflag = 1;
49f08c3bdfSopenharmony_ci			tst_res(TFAIL, "File mode got %o, expected %o",
50f08c3bdfSopenharmony_ci				low9mode, ~mskval & 0777);
51f08c3bdfSopenharmony_ci		}
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci		SAFE_UNLINK("testfile");
54f08c3bdfSopenharmony_ci	}
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci	if (!failflag)
57f08c3bdfSopenharmony_ci		tst_res(TPASS, "All files created with correct mode");
58f08c3bdfSopenharmony_ci}
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_cistatic struct tst_test test = {
61f08c3bdfSopenharmony_ci	.test_all = verify_umask,
62f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
63f08c3bdfSopenharmony_ci};
64