keyctl01.c revision ec6edca7aa42b6affd989ef91b5897f96795e40f
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 */
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	TEST_CLEANUP;
87	tst_rmdir();
88
89}
90
91/* Local  Functions */
92/******************************************************************************/
93/*									    */
94/* Function:    setup							 */
95/*									    */
96/* Description: Performs all one time setup for this test. This function is   */
97/*	      typically used to capture signals, create temporary dirs      */
98/*	      and temporary files that may be used in the course of this    */
99/*	      test.							 */
100/*									    */
101/* Input:       None.							 */
102/*									    */
103/* Output:      None.							 */
104/*									    */
105/* Return:      On failure - Exits by calling cleanup().		      */
106/*	      On success - returns 0.				       */
107/*									    */
108/******************************************************************************/
109void setup() {
110	/* Capture signals if any */
111	/* Create temporary directories */
112	TEST_PAUSE;
113	tst_tmpdir();
114}
115
116int main(int ac, char **av) {
117	int ret;
118	int lc;		/* loop counter */
119	key_serial_t ne_key;
120	char *msg;	/* message returned from parse_opts */
121
122	/* parse standard options */
123	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
124		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
125		tst_exit();
126	}
127
128	setup();
129
130	for (lc = 0; TEST_LOOPING(lc); lc++) {
131
132		Tst_count = 0;
133
134		for (testno = 1; testno < TST_TOTAL; ++testno) {
135
136			/* Call keyctl() and ask for a keyring's ID. */
137			ret = syscall(__NR_keyctl, KEYCTL_GET_KEYRING_ID,
138					KEY_SPEC_USER_SESSION_KEYRING);
139			if (ret != -1) {
140				tst_resm(TPASS,"KEYCTL_GET_KEYRING_ID succeeded");
141			} else {
142		 		tst_resm(TFAIL|TERRNO, "KEYCTL_GET_KEYRING_ID");
143			}
144
145			for (ne_key = INT32_MAX; ne_key > INT32_MIN;
146			    ne_key--) {
147				ret = syscall(__NR_keyctl, KEYCTL_READ,
148					ne_key);
149				if (ret == -1 && errno == ENOKEY)
150					break;
151			}
152
153			/* Call keyctl. */
154			ret = syscall(__NR_keyctl, KEYCTL_REVOKE, ne_key);
155			if (ret != -1) {
156				tst_resm(TFAIL|TERRNO,
157					"KEYCTL_REVOKE succeeded unexpectedly");
158			} else {
159				/* Check for the correct error num. */
160				if (errno == ENOKEY) {
161					tst_resm(TPASS|TERRNO,
162						"KEYCTL_REVOKE got expected "
163						"errno");
164				} else {
165					tst_resm(TFAIL|TERRNO,
166						"KEYCTL_REVOKE got unexpected "
167						"errno");
168				}
169
170			}
171
172		}
173
174	}
175	cleanup();
176
177	return (1);
178}
179#else
180int
181main(void)
182{
183	tst_resm(TCONF, "keyctl syscall support not available on system");
184	tst_exit();
185}
186#endif
187