From: John Kohl Date: Wed, 3 Apr 1991 09:54:31 +0000 (+0000) Subject: add locking code X-Git-Tag: krb5-1.0-alpha5~120 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=ffda07b8b428b85f9513df2dc1a9b6857647cbce;p=krb5.git add locking code git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@1981 dc483132-0cff-0310-8789-dd5450dbe970 --- diff --git a/src/lib/krb5/ccache/stdio/scc_maybe.c b/src/lib/krb5/ccache/stdio/scc_maybe.c index 39b60c9e3..8b6b3088c 100644 --- a/src/lib/krb5/ccache/stdio/scc_maybe.c +++ b/src/lib/krb5/ccache/stdio/scc_maybe.c @@ -18,6 +18,8 @@ static char rcsid_scc_maybe_c[] = #include "scc.h" #include /* XXX ip only? */ +#include +#include 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; }