dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / pkcs11 / libpkcs11 / common / pkcs11Sessionlist.c
blob55ba162e2f842a68215fd8eff9ddc702e56037d7
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <pthread.h>
30 #include <stdlib.h>
31 #include <security/cryptoki.h>
32 #include "pkcs11Global.h"
33 #include "pkcs11Slot.h"
34 #include "pkcs11Session.h"
38 * pkcs11_session_add:
39 * Create a session and add it to the list of sessions associated
40 * with the slot it is being opened on. The session handle, fwsessionp,
41 * will be the memory address of the session, typecast to a CK_SESSION_HANDLE.
43 * Assumptions: slotp is a valid slot, mutexes are not held, and
44 * the provider already successfully opened related session.
46 CK_RV
47 pkcs11_session_add(pkcs11_slot_t *slotp, CK_SLOT_ID slot_id,
48 CK_SESSION_HANDLE_PTR fwsessionp, CK_SESSION_HANDLE prov_sess)
51 pkcs11_session_t *newhandle = malloc(sizeof (pkcs11_session_t));
53 if (newhandle == NULL) {
54 return (CKR_HOST_MEMORY);
57 newhandle->se_magic = PKCS11_SESSION_MAGIC;
58 newhandle->se_handle = prov_sess;
59 newhandle->se_slotid = slot_id;
61 (void) pthread_mutex_lock(&slotp->sl_mutex);
63 /* Insert the new session in the front of the slot's session list */
64 if (slotp->sl_sess_list == NULL) {
65 slotp->sl_sess_list = newhandle;
66 newhandle->se_prev = NULL;
67 newhandle->se_next = NULL;
68 } else {
69 slotp->sl_sess_list->se_prev = newhandle;
70 newhandle->se_next = slotp->sl_sess_list;
71 newhandle->se_prev = NULL;
72 slotp->sl_sess_list = newhandle;
75 /* Typecast the address of session structure to a session handle */
76 *fwsessionp = (CK_SESSION_HANDLE)newhandle;
78 (void) pthread_mutex_unlock(&slotp->sl_mutex);
80 return (CKR_OK);
84 * pkcs11_session_delete:
85 * Delete a session from a particular slot's session list.
87 * Assumptions: slotp is a valid slot, sessp is a valid session,
88 * provider has already successfully closed this session, and
89 * mutexes are not held.
91 void
92 pkcs11_session_delete(pkcs11_slot_t *slotp, pkcs11_session_t *sessp)
95 /* Acquire the slot's lock */
96 (void) pthread_mutex_lock(&slotp->sl_mutex);
98 if (slotp->sl_sess_list == sessp) {
99 /* This is the first session in the list */
100 if (sessp->se_next != NULL) {
101 slotp->sl_sess_list = sessp->se_next;
102 sessp->se_next->se_prev = NULL;
103 } else {
104 /* Session is the only one in the list */
105 slotp->sl_sess_list = NULL;
107 } else {
108 /* Session is not the first one in the list */
109 if (sessp->se_next != NULL) {
110 /* Session is in the middle of the list */
111 sessp->se_prev->se_next = sessp->se_next;
112 sessp->se_next->se_prev = sessp->se_prev;
113 } else {
114 /* Session is the last one in the list */
115 sessp->se_prev->se_next = NULL;
119 /* Mark session as no longer valid */
120 sessp->se_magic = 0;
122 free(sessp);
124 (void) pthread_mutex_unlock(&slotp->sl_mutex);
129 * pkcs11_sessionlist_delete:
130 * Delete all sessions associated with a particular slot's session list.
132 * Assumptions: slotp is a valid slot, no mutexes are held, and the
133 * sessions were successfully closed with the provider already.
135 void
136 pkcs11_sessionlist_delete(pkcs11_slot_t *slotp)
139 pkcs11_session_t *sessp, *sess_nextp;
141 sessp = slotp->sl_sess_list;
143 /* Delete all the sessions in this slot's session list */
144 while (sessp) {
145 sess_nextp = sessp->se_next;
147 pkcs11_session_delete(slotp, sessp);
149 sessp = sess_nextp;
152 slotp->sl_sess_list = NULL;