ARPDAS_QNX6 1.0
rundir.c
Go to the documentation of this file.
00001 #include <grp.h>
00002 #include <pwd.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 #include <unistd.h>
00006 #include <sys/stat.h>
00007 #include <errno.h>
00008 #include "rundir.h"
00009 #include "nortlib.h"
00010 #include "nl_assert.h"
00011 
00012 void mkfltdir(const char *dir, uid_t flt_uid, gid_t flt_gid) {
00013   struct stat buf;
00014   if ( stat( dir, &buf) == -1 ) {
00015     if (errno == ENOENT) {
00016       if ( mkdir( dir, 02775 ) == -1)
00017         nl_error(3,"Error creating directory %s: %s", dir, strerror(errno));
00018     } else {
00019       nl_error(3,"Error on stat(%s): %s", dir, strerror(errno));
00020     }
00021   } else if (! S_ISDIR(buf.st_mode)) {
00022     // check to make sure it's a directory
00023     nl_error(3, "%s is not a directory", dir);
00024   } else {
00025     if (chmod(dir, 02775) == -1)
00026       nl_error(3, "Error on chmod(%s): %s", dir, strerror(errno));
00027   }
00028   if ( chown( dir, flt_uid, flt_gid) == -1)
00029     nl_error(3,"Error chowning directory %s: %s", dir, strerror(errno));
00030 }
00031 
00032 static const char *get_runexpdir() {
00033   static char *red = 0;
00034   const char *Exp;
00035   int nb, nb2;
00036 
00037   if (red) return red;
00038   Exp = getenv("Experiment");
00039   if (Exp == NULL) Exp = "none";
00040   nb = snprintf(NULL, 0, "%s/%s", RUNDIR, Exp);
00041   red = new_memory(nb+1);
00042   nb2 = snprintf(red, nb+1, "%s/%s", RUNDIR, Exp);
00043   nl_assert(nb2 == nb);
00044   return red;
00045 }
00046 
00047 void delete_rundir(void) {
00048   const char *runexpdir;
00049   struct stat buf;
00050 
00051   runexpdir = get_runexpdir();
00052   if (stat(runexpdir, &buf) != -1 || errno != ENOENT) {
00053     char rmcmd[80];
00054     int nb = snprintf(rmcmd, 80, "/bin/rm -rf %s", runexpdir);
00055     nl_assert(nb < 80);
00056     int rv = system(rmcmd);
00057     if (rv == -1) {
00058       nl_error(3, "system(%s) failed: %s", rmcmd, strerror(errno));
00059     } else if ( WEXITSTATUS(rv) ) {
00060       nl_error(3, "system(%s) returned non-zero status: %d", WEXITSTATUS(rv));
00061     }
00062     if (stat(runexpdir, &buf) != -1 || errno != ENOENT) {
00063       nl_error(3, "Failed to delete runexpdir %s", runexpdir);
00064     }
00065   } // else the directory does not exist
00066 }
00067 
00068 void setup_rundir(void) {
00069   struct group *flt_grp;
00070   struct passwd* flt_user;
00071   uid_t flt_uid;
00072   gid_t flt_gid;
00073   const char *runexpdir;
00074 
00075   flt_user = getpwnam("flight");
00076   if (flt_user == NULL) nl_error(3, "No flight user" );
00077   flt_grp = getgrnam("flight");
00078   if (flt_grp == NULL) nl_error(3, "No flight group" );
00079   flt_uid = flt_user->pw_uid;
00080   flt_gid = flt_grp->gr_gid;
00081 
00082   mkfltdir(RUNDIR, flt_uid, flt_gid);
00083   delete_rundir();
00084   atexit(&delete_rundir);
00085   runexpdir = get_runexpdir();
00086   mkfltdir(runexpdir, flt_uid, flt_gid);
00087 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines