afs: Introduce afs_FreeFirstToken
[openafs.git] / src / afs / afs_tokens.c
index 8bc5020..7b5f3cf 100644 (file)
@@ -43,7 +43,8 @@
  *     accessed using the appropriate element of the union.
  */
 union tokenUnion *
-afs_FindToken(struct tokenJar *tokens, rx_securityIndex type) {
+afs_FindToken(struct tokenJar *tokens, rx_securityIndex type)
+{
     while (tokens != NULL) {
        if (tokens->type == type) {
            return &tokens->content;
@@ -54,39 +55,43 @@ afs_FindToken(struct tokenJar *tokens, rx_securityIndex type) {
 }
 
 /*!
- * Free a single token
- *
- * This will free the given token. No attempt is made to unlink
- * the token from its container, and it is an error to attempt to
- * free a token which is still linked.
+ * Unlink and free a single token
  *
- * This performs a secure free, setting all token information to 0
- * before returning allocated data blocks to the kernel.
+ * This will unlink the first token in the given tokenJar, and free that token.
+ * This attempts to perform a secure free, setting all token information to 0
+ * before returning allocated data blocks to the kernel.  (Optimizing compilers
+ * may eliminate such a "dead store", though.)
  *
  * Intended primarily for internal use.
  *
- * @param[in] token
- *     The token to free
+ * @param[inout] tokenPtr
+ *     The token to unlink and free
  */
+static void
+afs_FreeFirstToken(struct tokenJar **tokenPtr)
+{
+    struct tokenJar *token = *tokenPtr;
+    if (token == NULL) {
+       return;
+    }
 
-void
-afs_FreeOneToken(struct tokenJar *token) {
-    if (token->next != NULL)
-       osi_Panic("Freeing linked token");
+    /* Unlink the token. */
+    *tokenPtr = token->next;
+    token->next = NULL;
 
     switch (token->type) {
       case RX_SECIDX_KAD:
        if (token->content.rxkad.ticket != NULL) {
-               memset(token->content.rxkad.ticket, 0, token->content.rxkad.ticketLen);
-               afs_osi_Free(token->content.rxkad.ticket,
-                            token->content.rxkad.ticketLen);
+           memset(token->content.rxkad.ticket, 0, token->content.rxkad.ticketLen);
+           afs_osi_Free(token->content.rxkad.ticket,
+                        token->content.rxkad.ticketLen);
        }
        break;
       default:
        break;
     }
-    memset(token, 0, sizeof(struct tokenJar));
-    afs_osi_Free(token, sizeof(struct tokenJar));
+    memset(token, 0, sizeof(*token));
+    afs_osi_Free(token, sizeof(*token));
 }
 
 /*!
@@ -99,16 +104,10 @@ afs_FreeOneToken(struct tokenJar *token) {
  *     A pointer to the address of the tokenjar to free.
  */
 void
-afs_FreeTokens(struct tokenJar **tokenPtr) {
-    struct tokenJar *next, *tokens;
-
-    tokens = *tokenPtr;
-    *tokenPtr = NULL;
-    while(tokens != NULL) {
-       next = tokens->next;
-       tokens->next = NULL; /* Unlink from chain */
-       afs_FreeOneToken(tokens);
-       tokens = next;
+afs_FreeTokens(struct tokenJar **tokenPtr)
+{
+    while (*tokenPtr != NULL) {
+       afs_FreeFirstToken(tokenPtr);
     }
 }
 
@@ -130,10 +129,12 @@ afs_FreeTokens(struct tokenJar **tokenPtr) {
  *     which may then be used to populate the token.
  */
 union tokenUnion *
-afs_AddToken(struct tokenJar **tokens, rx_securityIndex type) {
+afs_AddToken(struct tokenJar **tokens, rx_securityIndex type)
+{
     struct tokenJar *newToken;
 
-    newToken = afs_osi_Alloc(sizeof(struct tokenJar));
+    newToken = afs_osi_Alloc(sizeof(*newToken));
+    osi_Assert(newToken != NULL);
     memset(newToken, 0, sizeof(*newToken));
 
     newToken->type = type;
@@ -155,8 +156,9 @@ afs_AddToken(struct tokenJar **tokens, rx_securityIndex type) {
  * @returns
  *     True if the token has expired, false otherwise
  */
-int
-afs_IsTokenExpired(struct tokenJar *token, afs_int32 now) {
+static int
+afs_IsTokenExpired(struct tokenJar *token, afs_int32 now)
+{
     switch (token->type) {
       case RX_SECIDX_KAD:
        if (token->content.rxkad.clearToken.EndTimestamp < now - NOTOKTIMEOUT)
@@ -165,7 +167,7 @@ afs_IsTokenExpired(struct tokenJar *token, afs_int32 now) {
       default:
        return 0;
     }
-    return 0; /* not reached, but keep gcc happy */
+    return 0;
 }
 
 /*!
@@ -182,8 +184,9 @@ afs_IsTokenExpired(struct tokenJar *token, afs_int32 now) {
  * @returns
  *     True if the token is usable, false otherwise
  */
-int
-afs_IsTokenUsable(struct tokenJar *token, afs_int32 now) {
+static int
+afs_IsTokenUsable(struct tokenJar *token, afs_int32 now)
+{
 
     if (afs_IsTokenExpired(token, now))
        return 0;
@@ -211,15 +214,11 @@ afs_IsTokenUsable(struct tokenJar *token, afs_int32 now) {
  */
 
 void
-afs_DiscardExpiredTokens(struct tokenJar **tokenPtr, afs_int32 now) {
-    struct tokenJar *next;
-
+afs_DiscardExpiredTokens(struct tokenJar **tokenPtr, afs_int32 now)
+{
     while (*tokenPtr != NULL) {
        if (afs_IsTokenExpired(*tokenPtr, now)) {
-           next = (*tokenPtr)->next;
-           (*tokenPtr)->next = NULL;
-           afs_FreeOneToken(*tokenPtr);
-           *tokenPtr = next;
+           afs_FreeFirstToken(tokenPtr);
        } else {
            tokenPtr = &(*tokenPtr)->next;
         }
@@ -238,7 +237,8 @@ afs_DiscardExpiredTokens(struct tokenJar **tokenPtr, afs_int32 now) {
  *     True if the jar contains usable tokens, otherwise false
  */
 int
-afs_HasUsableTokens(struct tokenJar *token, afs_int32 now) {
+afs_HasUsableTokens(struct tokenJar *token, afs_int32 now)
+{
     while (token != NULL) {
         if (afs_IsTokenUsable(token, now))
            return 1;
@@ -260,7 +260,8 @@ afs_HasUsableTokens(struct tokenJar *token, afs_int32 now) {
  *
  */
 int
-afs_HasValidTokens(struct tokenJar *token, afs_int32 now) {
+afs_HasValidTokens(struct tokenJar *token, afs_int32 now)
+{
     while (token != NULL) {
         if (!afs_IsTokenExpired(token, now))
            return 1;
@@ -283,7 +284,8 @@ afs_HasValidTokens(struct tokenJar *token, afs_int32 now) {
  *     The number of valid tokens in the jar
  */
 static int
-countValidTokens(struct tokenJar *token, time_t now) {
+countValidTokens(struct tokenJar *token, time_t now)
+{
     int count = 0;
 
     while (token != NULL) {
@@ -308,7 +310,8 @@ countValidTokens(struct tokenJar *token, time_t now) {
  */
 void
 afs_AddRxkadToken(struct tokenJar **tokens, char *ticket, int ticketLen,
-                 struct ClearToken *clearToken) {
+                 struct ClearToken *clearToken)
+{
     union tokenUnion *tokenU;
     struct rxkadToken *rxkad;
 
@@ -316,6 +319,7 @@ afs_AddRxkadToken(struct tokenJar **tokens, char *ticket, int ticketLen,
     rxkad = &tokenU->rxkad;
 
     rxkad->ticket = afs_osi_Alloc(ticketLen);
+    osi_Assert(rxkad->ticket != NULL);
     rxkad->ticketLen = ticketLen;
     memcpy(rxkad->ticket, ticket, ticketLen);
     rxkad->clearToken = *clearToken;
@@ -323,7 +327,8 @@ afs_AddRxkadToken(struct tokenJar **tokens, char *ticket, int ticketLen,
 
 static int
 afs_AddRxkadTokenFromPioctl(struct tokenJar **tokens,
-                           struct ktc_tokenUnion *pioctlToken) {
+                           struct ktc_tokenUnion *pioctlToken)
+{
     struct ClearToken clear;
 
     clear.AuthHandle = pioctlToken->ktc_tokenUnion_u.at_kad.rk_kvno;
@@ -344,7 +349,8 @@ afs_AddRxkadTokenFromPioctl(struct tokenJar **tokens,
 
 static int
 rxkad_extractTokenForPioctl(struct tokenJar *token,
-                              struct ktc_tokenUnion *pioctlToken) {
+                              struct ktc_tokenUnion *pioctlToken)
+{
 
     struct token_rxkad *rxkadPioctl;
     struct rxkadToken *rxkadInternal;
@@ -383,7 +389,8 @@ rxkad_extractTokenForPioctl(struct tokenJar *token,
  */
 int
 afs_AddTokenFromPioctl(struct tokenJar **tokens,
-                      struct ktc_tokenUnion *pioctlToken) {
+                      struct ktc_tokenUnion *pioctlToken)
+{
 
     switch (pioctlToken->at_type) {
       case RX_SECIDX_KAD:
@@ -395,14 +402,15 @@ afs_AddTokenFromPioctl(struct tokenJar **tokens,
 
 static int
 extractPioctlToken(struct tokenJar *token,
-                  struct token_opaque *opaque) {
+                  struct token_opaque *opaque)
+{
     XDR xdrs;
     struct ktc_tokenUnion *pioctlToken;
     int code;
 
     memset(opaque, 0, sizeof(token_opaque));
 
-    pioctlToken = osi_Alloc(sizeof(struct ktc_tokenUnion));
+    pioctlToken = osi_Alloc(sizeof(*pioctlToken));
     if (pioctlToken == NULL)
        return ENOMEM;
 
@@ -446,12 +454,13 @@ extractPioctlToken(struct tokenJar *token,
     }
     xdr_destroy(&xdrs);
 
-out:
+ out:
     xdr_free((xdrproc_t) xdr_ktc_tokenUnion, &pioctlToken);
-    osi_Free(pioctlToken, sizeof(struct ktc_tokenUnion));
+    osi_Free(pioctlToken, sizeof(*pioctlToken));
 
     if (code != 0) {
-       osi_Free(opaque->token_opaque_val, opaque->token_opaque_len);
+       if (opaque->token_opaque_val != NULL)
+           osi_Free(opaque->token_opaque_val, opaque->token_opaque_len);
        opaque->token_opaque_val = NULL;
        opaque->token_opaque_len = 0;
     }
@@ -470,7 +479,7 @@ afs_ExtractTokensForPioctl(struct tokenJar *token,
 
     tokenSet->tokens.tokens_len = numTokens;
     tokenSet->tokens.tokens_val
-       = xdr_alloc(sizeof(struct token_opaque) * numTokens);
+       = xdr_alloc(sizeof(tokenSet->tokens.tokens_val[0]) * numTokens);
 
     if (tokenSet->tokens.tokens_val == NULL)
        return ENOMEM;
@@ -484,7 +493,7 @@ afs_ExtractTokensForPioctl(struct tokenJar *token,
        pos++;
     }
 
-out:
+ out:
     if (code)
        xdr_free((xdrproc_t) xdr_ktc_setTokenData, tokenSet);