123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- #include <features.h>
- #include <fcntl.h>
- #include <signal.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/file.h>
- #include <paths.h>
- #include <shadow.h>
- #define TIMEOUT 15
- static int lock_fd = -1;
- #include <bits/uClibc_mutex.h>
- __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
- static void noop_handler (int __sig);
- int
- lckpwdf (void)
- {
- sigset_t saved_set;
- struct sigaction saved_act;
- sigset_t new_set;
- struct sigaction new_act;
- struct flock fl;
- int result;
- int rv = -1;
- if (lock_fd != -1)
-
- return -1;
-
- __UCLIBC_MUTEX_LOCK(mylock);
- lock_fd = open (_PATH_PASSWD, O_WRONLY | O_CLOEXEC);
- if (lock_fd == -1) {
- goto DONE;
- }
- #ifndef __ASSUME_O_CLOEXEC
-
- fcntl (lock_fd, F_SETFD, FD_CLOEXEC);
- #endif
-
- memset (&new_act, '\0', sizeof (new_act));
- new_act.sa_handler = noop_handler;
- __sigfillset (&new_act.sa_mask);
-
- sigaction (SIGALRM, &new_act, &saved_act);
-
- __sigemptyset (&new_set);
- __sigaddset (&new_set, SIGALRM);
- sigprocmask (SIG_UNBLOCK, &new_set, &saved_set);
-
- alarm (TIMEOUT);
-
- memset (&fl, '\0', sizeof (fl));
- if (F_WRLCK)
- fl.l_type = F_WRLCK;
- if (SEEK_SET)
- fl.l_whence = SEEK_SET;
- result = fcntl (lock_fd, F_SETLKW, &fl);
-
- alarm (0);
- sigprocmask (SIG_SETMASK, &saved_set, NULL);
- sigaction (SIGALRM, &saved_act, NULL);
- if (result < 0) {
- close(lock_fd);
- lock_fd = -1;
- goto DONE;
- }
- rv = 0;
- DONE:
- __UCLIBC_MUTEX_UNLOCK(mylock);
- return rv;
- }
- int
- ulckpwdf (void)
- {
- int result;
- if (lock_fd == -1)
-
- result = -1;
- else
- {
-
- __UCLIBC_MUTEX_LOCK(mylock);
- result = close (lock_fd);
-
- lock_fd = -1;
-
- __UCLIBC_MUTEX_UNLOCK(mylock);
- }
- return result;
- }
- static void
- noop_handler (int sig attribute_unused) {
-
- }
|