]> git.pld-linux.org Git - projects/setup.git/blobdiff - joinpasswd.c
- 2.4.9
[projects/setup.git] / joinpasswd.c
index 520fd303b3c8a9521236676872bf422ac4634fbf..75fb9ebcd7bede96398b32378beefb61a1c5e5c1 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * $Id$
  * 
- * Copyright (c) 2001 Michal Moskal <malekith@pld.org.pl>.
+ * Copyright (c) 2001 Michal Moskal <malekith@pld-linux.org>.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -45,7 +45,7 @@
  * 20000 users in system database on Pentium class machine. After static link
  * against uClibc it is under 2k on x86. Stdio hasn't been used intentionally.
  * 
- * Written for PLD GNU/Linux (http://www.pld.org.pl) setup package.
+ * Written for PLD Linux (http://www.pld-linux.org/) setup package.
  *
  * Compilation against uClibc:
  * UCROOT=/usr/lib/bootdisk/usr
@@ -53,7 +53,7 @@
  *             $UCROOT/lib/libc.a -lgcc -o joinpasswd
  * strip -R .comment -R .note joinpasswd
  *
- * The idea of this program comes from Lukasz Dobrek <dobrek@pld.org.pl>.
+ * The idea of this program comes from Lukasz Dobrek <dobrek@pld-linux.org>.
  * 
  */
 
 #include <fcntl.h>
 #include <stdlib.h>
 #include <string.h>
+#include <signal.h>
 
 #define FILE1 "/etc/passwd"
 #define FILE2 "/etc/shadow"
 #define FILE3 "/etc/group"
 #define FILE4 "/etc/gshadow"
 
+/* #define OLD_LOCK */
+
+#define LOCK_FILE "/etc/.pwd.lock"
+
 #define SUFFIX ".rpmnew"
 /* maybe "-" or sth? */
 #define BACKUP ".old"
@@ -142,8 +147,17 @@ void itoa(char *buf, long i)
        *buf = 0;
 }
 
+#ifndef OLD_LOCK
+int lock_fd = -1;
+void noop(int x)
+{
+       (void)x;
+}
+#endif
+
 int try_lock(const char *name)
 {
+#ifdef OLD_LOCK
        char file[strlen(name) + 32], lock[strlen(name) + 32];
        char buf[32];
        int fd;
@@ -184,15 +198,44 @@ int try_lock(const char *name)
 oops:
        unlink(file);
        return -1;
+#else
+       struct flock fl;
+       
+       if (lock_fd != -1)
+               return -1;
+       lock_fd = open(LOCK_FILE, O_RDWR|O_CREAT, 0600);
+       if (lock_fd == -1)
+               return -1;
+       signal(SIGALRM, noop);
+       alarm(15);
+       memset(&fl, 0, sizeof(fl));
+       fl.l_type = F_WRLCK;
+       fl.l_whence = SEEK_SET;
+       if (fcntl(lock_fd, F_SETLKW, &fl) != 0) {
+               alarm(0);
+               close(lock_fd);
+               lock_fd = -1;
+               return -1;
+       }
+       alarm(0);
+       
+       return 0;
+#endif
 }
 
 void unlock(const char *name)
 {
+#ifdef OLD_LOCK
        char lock[strlen(name) + 32];
 
        strcpy(lock, name);
        strcat(lock, ".lock");
        unlink(lock);
+#else
+       if (lock_fd != -1)
+               close(lock_fd);
+       lock_fd = -1;
+#endif
 }
 
 void lock(const char *name)
@@ -231,7 +274,7 @@ int join(const char *old_name, const char *new_name, const char *backup_name)
        close(fd);
        
 #ifndef SILENT
-       eputs("marging contest of `");
+       eputs("merging content of `");
        eputs(old_name);
        eputs("' with `");
        eputs(new_name);
This page took 0.058224 seconds and 4 git commands to generate.