pndevmapperd: add f-keys
[pandora-libraries.git] / lib / pnd_utility.c
index a002bbb..b011cdd 100644 (file)
@@ -9,7 +9,9 @@
 #include <sys/types.h> /* ditto */
 #include <pwd.h> /* ditto */
 #include <sys/stat.h> /* for fstat */
+#include <errno.h> /* for stat->ENOENT */
 #include <dirent.h> /* for opendir */
+#include <fcntl.h> /* for creat */
 
 #include "pnd_pxml.h"
 #include "pnd_container.h"
@@ -167,6 +169,9 @@ void pnd_exec_no_wait_1 ( char *fullpath, char *arg1 ) {
     execl ( fullpath, fullpath, (char*) NULL );
   }
 
+  // error invoking something, and we're the child process, so just die before all hell breaks lose with us thinking we're the (second!) parent on return!
+  exit ( -1 );
+
   // getting here is an error
   //printf ( "Error attempting to run %s\n", fullpath );
 
@@ -358,3 +363,71 @@ unsigned char pnd_filecopy ( char *sourcepath, char *targetpath ) {
 
   return ( 1 );
 }
+
+unsigned char pnd_lock ( char *lockname ) {
+
+  if ( pnd_is_locked ( lockname ) ) {
+    return ( 0 ); // already locked
+  }
+
+  char fullpath [ PATH_MAX ];
+  int fd;
+
+  snprintf ( fullpath, PATH_MAX, "%s/%s", PND_LOCK_PATH, lockname );
+
+  if ( ( fd = creat ( fullpath, 0444 ) < 0 ) ) {
+    return ( 0 ); // error, yeah, I know, no way to know why it failed..
+  }
+
+  close ( fd );
+
+  return ( 1 );
+}
+
+time_t pnd_is_locked ( char *lockname ) {
+  char fullpath [ PATH_MAX ];
+  int rv;
+  snprintf ( fullpath, PATH_MAX, "%s/%s", PND_LOCK_PATH, lockname );
+
+  struct stat statbuf;
+  rv = stat ( fullpath, &statbuf );
+
+  if ( rv == ENOENT ) {
+    return ( 0 ); // file not existk, so no lock
+  } else if ( rv < 0 ) {
+    return ( 0 ); // assume unlocked for error, so app can continue?
+  }
+
+  return ( statbuf.st_mtime );
+}
+
+void pnd_unlock ( char *lockname ) {
+  char fullpath [ PATH_MAX ];
+  snprintf ( fullpath, PATH_MAX, "%s/%s", PND_LOCK_PATH, lockname );
+
+  unlink ( fullpath );
+
+  return;
+}
+
+unsigned char pnd_wait_for_unlock ( char *lockname, unsigned short int max, unsigned int usec_delta ) {
+
+  // check right off the top
+  if ( ! pnd_is_locked ( lockname ) ) {
+    return ( 1 ); // all clear!
+  }
+
+  unsigned short int count = 0;
+  while ( count < max ) {
+
+    if ( ! pnd_is_locked ( lockname ) ) {
+      return ( 1 ); // all clear!
+    }
+
+    usleep ( usec_delta );
+
+    count++;
+  }
+
+  return ( 0 );
+}