keyctl01.c revision 354ebb48db8e66a853a58379a4808d5dcd1ceac3
1/******************************************************************************/
2/* Copyright (c) Crackerjack Project., 2007				   */
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;  if not, write to the Free Software	       */
16/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    */
17/*									    */
18/******************************************************************************/
19/******************************************************************************/
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>			*/
39/******************************************************************************/
40
41#include "config.h"
42#include <sys/types.h>
43#if HAVE_KEYCTL_SYSCALL
44#include <linux/keyctl.h>
45#endif
46#include <errno.h>
47#if HAVE_KEYCTL_SYSCALL
48#include <keyutils.h>
49#endif
50#include <limits.h>
51#include <stdio.h>
52
53/* Harness Specific Include Files. */
54#include "test.h"
55#include "usctest.h"
56#include "linux_syscall_numbers.h"
57
58/* Extern Global Variables */
59
60/* Global Variables */
61char *TCID = "keyctl01";	/* Test program identifier. */
62int testno;
63int TST_TOTAL = 2;		/* total number of tests in this file.   */
64
65#if HAVE_KEYCTL_SYSCALL
66/* Extern Global Functions */
67/******************************************************************************/
68/*									    */
69/* Function:    cleanup						       */
70/*									    */
71/* Description: Performs all one time clean up for this test on successful    */
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/*									    */
80/* Return:      On failure - Exits calling tst_exit(). Non '0' return code.   */
81/*	      On success - Exits calling tst_exit(). With '0' return code.  */
82/*									    */
83/******************************************************************************/
84extern void cleanup()
85{
86
87	TEST_CLEANUP;
88	tst_rmdir();
89
90}
91
92/* Local  Functions */
93/******************************************************************************/
94/*									    */
95/* Function:    setup							 */
96/*									    */
97/* Description: Performs all one time setup for this test. This function is   */
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/*									    */
109/******************************************************************************/
110void setup()
111{
112	/* Capture signals if any */
113	/* Create temporary directories */
114	TEST_PAUSE;
115	tst_tmpdir();
116}
117
118int main(int ac, char **av)
119{
120	int ret;
121	int lc;
122	key_serial_t ne_key;
123	char *msg;
124
125	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
126		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
127		tst_exit();
128	}
129
130	setup();
131
132	for (lc = 0; TEST_LOOPING(lc); lc++) {
133
134		Tst_count = 0;
135
136		for (testno = 1; testno < TST_TOTAL; ++testno) {
137
138			/* Call keyctl() and ask for a keyring's ID. */
139			ret = syscall(__NR_keyctl, KEYCTL_GET_KEYRING_ID,
140				      KEY_SPEC_USER_SESSION_KEYRING);
141			if (ret != -1) {
142				tst_resm(TPASS,
143					 "KEYCTL_GET_KEYRING_ID succeeded");
144			} else {
145				tst_resm(TFAIL | TERRNO,
146					 "KEYCTL_GET_KEYRING_ID");
147			}
148
149			for (ne_key = INT32_MAX; ne_key > INT32_MIN; ne_key--) {
150				ret = syscall(__NR_keyctl, KEYCTL_READ, ne_key);
151				if (ret == -1 && errno == ENOKEY)
152					break;
153			}
154
155			/* Call keyctl. */
156			ret = syscall(__NR_keyctl, KEYCTL_REVOKE, ne_key);
157			if (ret != -1) {
158				tst_resm(TFAIL | TERRNO,
159					 "KEYCTL_REVOKE succeeded unexpectedly");
160			} else {
161				/* Check for the correct error num. */
162				if (errno == ENOKEY) {
163					tst_resm(TPASS | TERRNO,
164						 "KEYCTL_REVOKE got expected "
165						 "errno");
166				} else {
167					tst_resm(TFAIL | TERRNO,
168						 "KEYCTL_REVOKE got unexpected "
169						 "errno");
170				}
171
172			}
173
174		}
175
176	}
177	cleanup();
178
179	return (1);
180}
181#else
182int main(void)
183{
184	tst_resm(TCONF, "keyctl syscall support not available on system");
185	tst_exit();
186}
187#endif
188