add locking code
authorJohn Kohl <jtkohl@mit.edu>
Wed, 3 Apr 1991 09:54:31 +0000 (09:54 +0000)
committerJohn Kohl <jtkohl@mit.edu>
Wed, 3 Apr 1991 09:54:31 +0000 (09:54 +0000)
git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@1981 dc483132-0cff-0310-8789-dd5450dbe970

src/lib/krb5/ccache/stdio/scc_maybe.c

index 39b60c9e36bbfd4d478d1f53eec6c6ceaf24a9e1..8b6b3088ccf40fa80875d1e54497606765c5ba68 100644 (file)
@@ -18,6 +18,8 @@ static char rcsid_scc_maybe_c[] =
 
 #include "scc.h"
 #include <netinet/in.h>                        /* XXX ip only? */
+#include <krb5/libos.h>
+#include <krb5/los-proto.h>
 
 krb5_error_code
 krb5_scc_close_file (id)
@@ -25,6 +27,7 @@ krb5_scc_close_file (id)
 {
      krb5_scc_data *data;
      int ret;
+     krb5_error_code retval;
 
      data = (krb5_scc_data *) id->data;
      if (data->file == (FILE *) NULL) {
@@ -43,12 +46,17 @@ krb5_scc_close_file (id)
      bzero (data->stdio_buffer, sizeof (data->stdio_buffer));
      if (ret == EOF) {
          int errsave = errno;
+         (void) krb5_unlock_file(data->file, data->filename);
          (void) fclose (data->file);
          data->file = 0;
          return krb5_scc_interpret (errsave);
      }
+     retval = krb5_unlock_file(data->file, data->filename);
      ret = fclose (data->file);
      data->file = 0;
+     if (retval)
+        return retval;
+     else
      return ret ? krb5_scc_interpret (errno) : 0;
 }
 
@@ -61,10 +69,12 @@ krb5_scc_open_file (id, mode)
      krb5_scc_data *data;
      FILE *f;
      char *open_flag;
+     krb5_error_code retval;
 
      data = (krb5_scc_data *) id->data;
      if (data->file) {
          /* Don't know what state it's in; shut down and start anew.  */
+         (void) krb5_unlock_file(data->file, data->filename);
          (void) fclose (data->file);
          data->file = 0;
      }
@@ -88,23 +98,41 @@ krb5_scc_open_file (id, mode)
 #if 0 /* alternative, not requiring sizeof stdio_buffer == BUFSIZ */
      setvbuf(f, data->stdio_buffer, _IOFBF, sizeof (data->stdio_buffer));
 #endif
-
+     switch (mode) {
+     case SCC_OPEN_RDONLY:
+        if (retval = krb5_lock_file(f, data->filename, KRB5_LOCKMODE_SHARED)) {
+            (void) fclose(f);
+            return retval;
+        }
+        break;
+     case SCC_OPEN_RDWR:
+     case SCC_OPEN_AND_ERASE:
+        if (retval = krb5_lock_file(f, data->filename,
+                                    KRB5_LOCKMODE_EXCLUSIVE)) {
+            (void) fclose(f);
+            return retval;
+        }
+        break;
+     }
      if (mode == SCC_OPEN_AND_ERASE) {
         /* write the version number */
         int errsave;
 
         if (!fwrite((char *)&scc_fvno, sizeof(scc_fvno), 1, f)) {
             errsave = errno;
+            (void) krb5_unlock_file(f, data->filename);
             (void) fclose(f);
             return krb5_scc_interpret(errsave);
         }
      } else {
         /* verify a valid version number is there */
         if (!fread((char *)&scc_fvno, sizeof(scc_fvno), 1, f)) {
+            (void) krb5_unlock_file(f, data->filename);
             (void) fclose(f);
             return KRB5_CCACHE_BADVNO;
         }
         if (scc_fvno != htons(KRB5_SCC_FVNO)) {
+            (void) krb5_unlock_file(f, data->filename);
             (void) fclose(f);
             return KRB5_CCACHE_BADVNO;
         }