* kprop.c: Use NPROTOTYPE for declarations.
authorMark Eichin <eichin@mit.edu>
Fri, 18 Nov 1994 00:13:39 +0000 (00:13 +0000)
committerMark Eichin <eichin@mit.edu>
Fri, 18 Nov 1994 00:13:39 +0000 (00:13 +0000)
(close_database): New function. Cleans up locks properly.
(main): call it.
(open_database): Use krb5_lock_file instead of POSIX_FILE_LOCKS.
* kpropd.c (doit): Use krb5_lock_file.
(changes from jtkohl@mit.edu.)

from [txn 0497]

git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@4673 dc483132-0cff-0310-8789-dd5450dbe970

src/slave/ChangeLog
src/slave/kprop.c
src/slave/kpropd.c

index 0e16cd2674ec3c8ad4d15f6a349ee82dde8fff70..ab93b48827bca1a5cd6608d836037f426ca56c9b 100644 (file)
@@ -1,3 +1,12 @@
+Thu Nov 17 18:31:18 1994  Mark Eichin  (eichin@cygnus.com)
+
+       * kprop.c: Use NPROTOTYPE for declarations.
+       (close_database): New function. Cleans up locks properly.
+       (main): call it.
+       (open_database): Use krb5_lock_file instead of POSIX_FILE_LOCKS.
+       * kpropd.c (doit): Use krb5_lock_file.
+       (changes from jtkohl@mit.edu.)
+
 Mon Oct  3 19:13:46 1994  Theodore Y. Ts'o  (tytso@dcl)
 
        * Makefile.in: Use $(srcdir) to find manual page for make install.
index 622bb6d5e11f303cf7361475d85ffa4220811d42..939c476a5e9ac527fc573d9978ba787a134f4f96 100644 (file)
 
 
 #include <errno.h>
-#ifdef POSIX_FILE_LOCKS
-#include <fcntl.h>
-#endif
-
 #include <stdio.h>
 #include <ctype.h>
 #include <sys/file.h>
@@ -49,6 +45,7 @@
 #include <krb5/kdb_dbm.h>
 #include <krb5/ext-proto.h>
 #include <krb5/los-proto.h>
+#include <krb5/libos.h>
 #include <com_err.h>
 
 #ifdef NEED_SYS_FCNTL_H
@@ -77,13 +74,14 @@ krb5_address        receiver_addr;
 
 void   PRS();
 void   get_tickets();
-static void usage();
-krb5_error_code open_connection();
-void   kerberos_authenticate();
-int    open_database();
-void   xmit_database();
-void   send_error();
-void   update_last_prop_file();
+static void usage NPROTOTYPE((void));
+krb5_error_code open_connection NPROTOTYPE((char *, int *, char *));
+void   kerberos_authenticate NPROTOTYPE((int, krb5_principal));
+int    open_database NPROTOTYPE((char *, int *));
+void   close_database NPROTOTYPE((int));
+void   xmit_database NPROTOTYPE((int, int, int));
+void   send_error NPROTOTYPE((int, int, int));
+void   update_last_prop_file NPROTOTYPE((char *, char *));
 
 static void usage()
 {
@@ -123,6 +121,7 @@ main(argc, argv)
        xmit_database(fd, database_fd, database_size);
        update_last_prop_file(slave_host, file);
        printf("Database propagation to %s: SUCCEEDED\n", slave_host);
+       close_database(database_fd);
        exit(0);
 }
 
@@ -395,6 +394,8 @@ void kerberos_authenticate(fd, me)
        krb5_free_ap_rep_enc_part(rep_result);
 }
 
+FILE * dbfp;
+char * dbpathname;
 /*
  * Open the Kerberos database dump file.  Takes care of locking it
  * and making sure that the .ok file is more recent that the database
@@ -409,41 +410,33 @@ open_database(data_fn, size)
        int     *size;
 {
        int             fd;
+       int             err;
        struct stat     stbuf, stbuf_ok;
        char            *data_ok_fn;
        static char ok[] = ".dump_ok";
-#ifdef POSIX_FILE_LOCKS
-       struct flock lock_arg;
-#endif
 
-       if ((fd = open(data_fn, O_RDONLY)) < 0) {
+       dbpathname = strdup(data_fn);
+       if (!dbpathname) {
+           com_err(progname, ENOMEM, "allocating database file name '%s'",
+                   data_fn);
+           exit(1);
+       }
+       if ((dbfp = fopen(dbpathname, "r")) == 0) {
                com_err(progname, errno, "while trying to open %s",
-                       data_fn);
+                       dbpathname);
                exit(1);
        }
-       
-#ifdef POSIX_FILE_LOCKS
-       lock_arg.l_whence = 0;
-       lock_arg.l_start = 0;
-       lock_arg.l_len = 0;
-       if (fcntl(fd, F_SETLK, &lock_arg) == -1) {
-               if (errno == EACCES || errno == EAGAIN)
-                       com_err(progname, 0, "database locked");
-               else
-                       com_err(progname, errno, "while trying to flock %s",
-                               data_fn);
-               exit(1);
-       }
-#else
-       if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
-               if (errno == EWOULDBLOCK || errno == EAGAIN)
-                       com_err(progname, 0, "database locked");
-               else
-                       com_err(progname, errno, "while trying to flock %s",
-                               data_fn);
-               exit(1);
-       }
-#endif
+
+       err = krb5_lock_file(dbfp, dbpathname,
+                            KRB5_LOCKMODE_SHARED|KRB5_LOCKMODE_DONTBLOCK);
+       if (err == EAGAIN || err == EWOULDBLOCK || errno == EACCES) {
+           com_err(progname, 0, "database locked");
+           exit(1);
+       } else if (err) {
+           com_err(progname, err, "while trying to lock '%s'", dbpathname);
+           exit(1);
+       }           
+       fd = fileno(dbfp);
        if (fstat(fd, &stbuf)) {
                com_err(progname, errno, "while trying to stat %s",
                        data_fn);
@@ -471,6 +464,23 @@ open_database(data_fn, size)
        return(fd);
 }
 
+void
+close_database(fd)
+    int fd;
+{
+    int err;
+    if (fd != fileno(dbfp)) {
+       com_err(progname, 0, "bad fd passed to close_database");
+       exit(1);
+    }
+    err = krb5_file_lock(dbfp, dbpathname, KRB5_LOCKMODE_UNLOCK);
+    if (err)
+       com_err(progname, err, "while unlocking database '%s'", dbpathname);
+    free(dbpathname);
+    (void) fclose(dbfp);
+    return;
+}
+  
 /*
  * Now we send over the database.  We use the following protocol:
  * Send over a KRB_SAFE message with the size.  Then we send over the
index 780db3492368d77db71dfac5487273744359eb35..d9b23285cf1d8fdc9aab3173972a82921db07191 100644 (file)
@@ -57,6 +57,7 @@
 #include <krb5/kdb_dbm.h>
 #include <krb5/ext-proto.h>
 #include <krb5/los-proto.h>
+#include <krb5/libos.h>
 #include <com_err.h>
 #include <errno.h>
 
@@ -189,7 +190,8 @@ void doit(fd)
        int on = 1, fromlen;
        struct hostent  *hp;
        krb5_error_code retval;
-       int     lock_fd;
+       FILE *lock_fp;
+       int omask;
 
        fromlen = sizeof (from);
        if (getpeername(fd, (struct sockaddr *) &from, &fromlen) < 0) {
@@ -238,37 +240,16 @@ void doit(fd)
                printf("My sequence number: %d\n", my_seq_num);
                printf("His sequence number: %d\n", his_seq_num);
        }
-       if ((lock_fd = (open(temp_file_name, O_WRONLY | O_CREAT, 0600))) < 0) {
-               com_err(progname, errno,
-                       "while opening database file, '%s'",
-                       temp_file_name);
-               exit(1);
-       }
-#ifdef POSIX_FILE_LOCKS
-       {
-               int lock_cmd = F_SETLK;
-               struct flock lock_arg;
-
-               lock_arg.l_type = F_WRLCK;
-               lock_arg.l_whence = 0;
-               lock_arg.l_start = 0;
-               lock_arg.l_len = 0;
-               
-               if (fcntl(lock_fd, lock_cmd, &lock_arg) == -1) {
-                       /* see POSIX/IEEE 1003.1-1988, 6.5.2.4 */
-                       if (errno == EACCES || errno == EAGAIN)
-                               errno = EAGAIN;
-                       com_err(progname, errno, "while trying to lock '%s'",
-                               temp_file_name);
-               }
+       omask = umask(077);
+       lock_fp = fopen(temp_file_name, "a");
+       (void) umask(omask);
+       retval = krb5_lock_file(lock_fp, temp_file_name,
+                               KRB5_LOCKMODE_EXCLUSIVE|KRB5_LOCKMODE_DONTBLOCK);
+       if (retval) {
+           com_err(progname, retval, "while trying to lock '%s'",
+                   temp_file_name);
+           exit(1);
        }
-#else
-       if (flock(lock_fd, LOCK_EX | LOCK_NB)) {
-               com_err(progname, errno, "while trying to lock '%s'",
-                       temp_file_name);
-               exit(1);
-       }
-#endif
        if ((database_fd = open(temp_file_name,
                                O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) {
                com_err(progname, errno,
@@ -288,7 +269,12 @@ void doit(fd)
                exit(1);
        }
        load_database(kdb5_edit, file);
-       close(lock_fd);
+       retval = krb5_lock_file(lock_fp, temp_file_name, KRB5_LOCKMODE_UNLOCK);
+       if (retval) {
+           com_err(progname, retval, "while unlocking '%s'", temp_file_name);
+           exit(1);
+       }
+       (void) fclose(lock_fp);
        exit(0);
 }