subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 1 | /******************************************************************************/ |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 2 | /* Copyright (c) Crackerjack Project., 2007 */ |
| 3 | /* */ |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 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 */ |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 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; if not, write to the Free Software */ |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 16 | /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 17 | /* */ |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 18 | /******************************************************************************/ |
| 19 | /******************************************************************************/ |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 20 | /* */ |
| 21 | /* File: keyctl01.c */ |
| 22 | /* */ |
| 23 | /* Description: This tests the keyctl() syscall */ |
| 24 | /* Manipulate the kernel's key management facility */ |
| 25 | /* Usage: <for command-line> */ |
| 26 | /* keyctl01 [-c n] [-e][-i n] [-I x] [-p x] [-t] */ |
| 27 | /* where, -c n : Run n copies concurrently. */ |
| 28 | /* -e : Turn on errno logging. */ |
| 29 | /* -i n : Execute test n times. */ |
| 30 | /* -I x : Execute test for x seconds. */ |
| 31 | /* -P x : Pause for x seconds between iterations. */ |
| 32 | /* -t : Turn on syscall timing. */ |
| 33 | /* */ |
| 34 | /* Total Tests: 2 */ |
| 35 | /* */ |
| 36 | /* Test Name: keyctl01 */ |
| 37 | /* History: Porting from Crackerjack to LTP is done by */ |
| 38 | /* Manas Kumar Nayak maknayak@in.ibm.com> */ |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 39 | /******************************************************************************/ |
| 40 | |
Garrett Cooper | 020a4da | 2010-11-03 20:57:21 -0700 | [diff] [blame] | 41 | #include "config.h" |
Garrett Cooper | 6340c2a | 2010-11-01 00:16:19 -0700 | [diff] [blame] | 42 | #include <sys/types.h> |
Garrett Cooper | e99ebbc | 2010-11-03 10:15:10 -0700 | [diff] [blame] | 43 | #if HAVE_KEYCTL_SYSCALL |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 44 | #include <linux/keyctl.h> |
Garrett Cooper | e99ebbc | 2010-11-03 10:15:10 -0700 | [diff] [blame] | 45 | #endif |
Garrett Cooper | da90805 | 2010-10-27 06:05:00 -0700 | [diff] [blame] | 46 | #include <errno.h> |
Garrett Cooper | e99ebbc | 2010-11-03 10:15:10 -0700 | [diff] [blame] | 47 | #if HAVE_KEYCTL_SYSCALL |
Garrett Cooper | 4e77264 | 2010-10-31 20:58:03 -0700 | [diff] [blame] | 48 | #include <keyutils.h> |
Garrett Cooper | e99ebbc | 2010-11-03 10:15:10 -0700 | [diff] [blame] | 49 | #endif |
Garrett Cooper | da90805 | 2010-10-27 06:05:00 -0700 | [diff] [blame] | 50 | #include <limits.h> |
| 51 | #include <stdio.h> |
Garrett Cooper | 4e77264 | 2010-10-31 20:58:03 -0700 | [diff] [blame] | 52 | |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 53 | /* Harness Specific Include Files. */ |
| 54 | #include "test.h" |
| 55 | #include "usctest.h" |
| 56 | #include "linux_syscall_numbers.h" |
| 57 | |
| 58 | /* Extern Global Variables */ |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 59 | |
| 60 | /* Global Variables */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 61 | char *TCID = "keyctl01"; /* Test program identifier. */ |
| 62 | int testno; |
| 63 | int TST_TOTAL = 2; /* total number of tests in this file. */ |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 64 | |
Garrett Cooper | e99ebbc | 2010-11-03 10:15:10 -0700 | [diff] [blame] | 65 | #if HAVE_KEYCTL_SYSCALL |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 66 | /* Extern Global Functions */ |
| 67 | /******************************************************************************/ |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 68 | /* */ |
| 69 | /* Function: cleanup */ |
| 70 | /* */ |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 71 | /* Description: Performs all one time clean up for this test on successful */ |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 72 | /* completion, premature exit or failure. Closes all temporary */ |
| 73 | /* files, removes all temporary directories exits the test with */ |
| 74 | /* appropriate return code by calling tst_exit() function. */ |
| 75 | /* */ |
| 76 | /* Input: None. */ |
| 77 | /* */ |
| 78 | /* Output: None. */ |
| 79 | /* */ |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 80 | /* Return: On failure - Exits calling tst_exit(). Non '0' return code. */ |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 81 | /* On success - Exits calling tst_exit(). With '0' return code. */ |
| 82 | /* */ |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 83 | /******************************************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 84 | extern void cleanup() |
| 85 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 86 | |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 87 | TEST_CLEANUP; |
| 88 | tst_rmdir(); |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 89 | |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /* Local Functions */ |
| 93 | /******************************************************************************/ |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 94 | /* */ |
| 95 | /* Function: setup */ |
| 96 | /* */ |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 97 | /* Description: Performs all one time setup for this test. This function is */ |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 98 | /* typically used to capture signals, create temporary dirs */ |
| 99 | /* and temporary files that may be used in the course of this */ |
| 100 | /* test. */ |
| 101 | /* */ |
| 102 | /* Input: None. */ |
| 103 | /* */ |
| 104 | /* Output: None. */ |
| 105 | /* */ |
| 106 | /* Return: On failure - Exits by calling cleanup(). */ |
| 107 | /* On success - returns 0. */ |
| 108 | /* */ |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 109 | /******************************************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 110 | void setup() |
| 111 | { |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 112 | /* Capture signals if any */ |
| 113 | /* Create temporary directories */ |
| 114 | TEST_PAUSE; |
| 115 | tst_tmpdir(); |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 118 | int main(int ac, char **av) |
| 119 | { |
yaberauneya | c2e8839 | 2009-11-24 20:36:37 +0000 | [diff] [blame] | 120 | int ret; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 121 | int lc; |
Garrett Cooper | da90805 | 2010-10-27 06:05:00 -0700 | [diff] [blame] | 122 | key_serial_t ne_key; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 123 | char *msg; |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 124 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 125 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 126 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 127 | tst_exit(); |
| 128 | } |
| 129 | |
| 130 | setup(); |
| 131 | |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 132 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 133 | |
| 134 | Tst_count = 0; |
| 135 | |
| 136 | for (testno = 1; testno < TST_TOTAL; ++testno) { |
Garrett Cooper | da90805 | 2010-10-27 06:05:00 -0700 | [diff] [blame] | 137 | |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 138 | /* Call keyctl() and ask for a keyring's ID. */ |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 139 | ret = ltp_syscall(__NR_keyctl, KEYCTL_GET_KEYRING_ID, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 140 | KEY_SPEC_USER_SESSION_KEYRING); |
yaberauneya | c2e8839 | 2009-11-24 20:36:37 +0000 | [diff] [blame] | 141 | if (ret != -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 142 | tst_resm(TPASS, |
| 143 | "KEYCTL_GET_KEYRING_ID succeeded"); |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 144 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 145 | tst_resm(TFAIL | TERRNO, |
| 146 | "KEYCTL_GET_KEYRING_ID"); |
Garrett Cooper | da90805 | 2010-10-27 06:05:00 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 149 | for (ne_key = INT32_MAX; ne_key > INT32_MIN; ne_key--) { |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 150 | ret = ltp_syscall(__NR_keyctl, KEYCTL_READ, |
| 151 | ne_key); |
Garrett Cooper | da90805 | 2010-10-27 06:05:00 -0700 | [diff] [blame] | 152 | if (ret == -1 && errno == ENOKEY) |
| 153 | break; |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 154 | } |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 155 | |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 156 | /* Call keyctl. */ |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 157 | ret = ltp_syscall(__NR_keyctl, KEYCTL_REVOKE, ne_key); |
yaberauneya | c2e8839 | 2009-11-24 20:36:37 +0000 | [diff] [blame] | 158 | if (ret != -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 159 | tst_resm(TFAIL | TERRNO, |
| 160 | "KEYCTL_REVOKE succeeded unexpectedly"); |
yaberauneya | c2e8839 | 2009-11-24 20:36:37 +0000 | [diff] [blame] | 161 | } else { |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 162 | /* Check for the correct error num. */ |
yaberauneya | c2e8839 | 2009-11-24 20:36:37 +0000 | [diff] [blame] | 163 | if (errno == ENOKEY) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 164 | tst_resm(TPASS | TERRNO, |
| 165 | "KEYCTL_REVOKE got expected " |
| 166 | "errno"); |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 167 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 168 | tst_resm(TFAIL | TERRNO, |
| 169 | "KEYCTL_REVOKE got unexpected " |
| 170 | "errno"); |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 171 | } |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 172 | |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | } |
| 176 | |
| 177 | } |
| 178 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 179 | |
Garrett Cooper | da90805 | 2010-10-27 06:05:00 -0700 | [diff] [blame] | 180 | return (1); |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 181 | } |
Garrett Cooper | e99ebbc | 2010-11-03 10:15:10 -0700 | [diff] [blame] | 182 | #else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 183 | int main(void) |
Garrett Cooper | e99ebbc | 2010-11-03 10:15:10 -0700 | [diff] [blame] | 184 | { |
| 185 | tst_resm(TCONF, "keyctl syscall support not available on system"); |
| 186 | tst_exit(); |
| 187 | } |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 188 | #endif |