keyctl01.c revision 45e285d46ab47b0ff76c88acb5ba97b0bd5f753d
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 */
59extern int Tst_count;	/* counter for tst_xxx routines.	 */
60extern char *TESTDIR;	/* temporary dir created by tst_tmpdir() */
61
62/* Global Variables */
63char *TCID = "keyctl01";/* Test program identifier.*/
64int  testno;
65int  TST_TOTAL = 2;	/* total number of tests in this file.   */
66
67#if HAVE_KEYCTL_SYSCALL
68/* Extern Global Functions */
69/******************************************************************************/
70/*									    */
71/* Function:    cleanup						       */
72/*									    */
73/* Description: Performs all one time clean up for this test on successful    */
74/*	      completion,  premature exit or  failure. Closes all temporary */
75/*	      files, removes all temporary directories exits the test with  */
76/*	      appropriate return code by calling tst_exit() function.       */
77/*									    */
78/* Input:       None.							 */
79/*									    */
80/* Output:      None.							 */
81/*									    */
82/* Return:      On failure - Exits calling tst_exit(). Non '0' return code.   */
83/*	      On success - Exits calling tst_exit(). With '0' return code.  */
84/*									    */
85/******************************************************************************/
86extern void cleanup() {
87	/* Remove tmp dir and all files in it */
88	TEST_CLEANUP;
89	tst_rmdir();
90
91	/* Exit with appropriate return code. */
92	tst_exit();
93}
94
95/* Local  Functions */
96/******************************************************************************/
97/*									    */
98/* Function:    setup							 */
99/*									    */
100/* Description: Performs all one time setup for this test. This function is   */
101/*	      typically used to capture signals, create temporary dirs      */
102/*	      and temporary files that may be used in the course of this    */
103/*	      test.							 */
104/*									    */
105/* Input:       None.							 */
106/*									    */
107/* Output:      None.							 */
108/*									    */
109/* Return:      On failure - Exits by calling cleanup().		      */
110/*	      On success - returns 0.				       */
111/*									    */
112/******************************************************************************/
113void setup() {
114	/* Capture signals if any */
115	/* Create temporary directories */
116	TEST_PAUSE;
117	tst_tmpdir();
118}
119
120int main(int ac, char **av) {
121	int ret;
122	int lc;		/* loop counter */
123	key_serial_t ne_key;
124	char *msg;	/* message returned from parse_opts */
125
126	/* parse standard options */
127	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL){
128		tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
129		tst_exit();
130	}
131
132	setup();
133
134	/* Check looping state if -i option given */
135	for (lc = 0; TEST_LOOPING(lc); lc++) {
136
137		Tst_count = 0;
138
139		for (testno = 1; testno < TST_TOTAL; ++testno) {
140
141			/* Call keyctl() and ask for a keyring's ID. */
142			ret = syscall(__NR_keyctl, KEYCTL_GET_KEYRING_ID,
143					KEY_SPEC_USER_SESSION_KEYRING);
144			if (ret != -1) {
145				tst_resm(TPASS,"KEYCTL_GET_KEYRING_ID succeeded");
146			} else {
147		 		tst_resm(TFAIL|TERRNO, "KEYCTL_GET_KEYRING_ID");
148			}
149
150			for (ne_key = INT32_MAX; ne_key > INT32_MIN;
151			    ne_key--) {
152				ret = syscall(__NR_keyctl, KEYCTL_READ,
153					ne_key);
154				if (ret == -1 && errno == ENOKEY)
155					break;
156			}
157
158			/* Call keyctl. */
159			ret = syscall(__NR_keyctl, KEYCTL_REVOKE, ne_key);
160			if (ret != -1) {
161				tst_resm(TFAIL|TERRNO,
162					"KEYCTL_REVOKE succeeded unexpectedly");
163			} else {
164				/* Check for the correct error num. */
165				if (errno == ENOKEY) {
166					tst_resm(TPASS|TERRNO,
167						"KEYCTL_REVOKE got expected "
168						"errno");
169				} else {
170					tst_resm(TFAIL|TERRNO,
171						"KEYCTL_REVOKE got unexpected "
172						"errno");
173				}
174
175			}
176
177		}
178
179	}
180	cleanup();
181	/* NOTREACHED */
182	return (1);
183}
184#else
185int
186main(void)
187{
188	tst_resm(TCONF, "keyctl syscall support not available on system");
189	tst_exit();
190}
191#endif
192