// Read a file into a std::string.  Return false on error.
MyString
MultiLogFiles::readFile(char const *filename,std::string& buf)
{
    char chunk[4000];
	MyString rtnVal;

	int fd = safe_open_wrapper_follow(filename, O_RDONLY);
	if (fd < 0) {
		rtnVal.formatstr("error opening submit file %s: %s",
				filename, strerror(errno) );
		dprintf(D_ALWAYS, "%s\n", rtnVal.Value() );
		return rtnVal;
	}

    while(1) {
        size_t n = read(fd,chunk,sizeof(chunk)-1);
        if(n>0) {
            chunk[n] = '\0';
            buf += chunk;
        }
        else if(n==0) {
            break;
        }
        else {
            rtnVal.formatstr("failed to read submit file %s: %s",
					filename, strerror(errno) );
			dprintf(D_ALWAYS, "%s\n", rtnVal.Value() );
			close(fd);
			return rtnVal;
        }
    }

	close(fd);
    return rtnVal;
}
Exemple #2
0
QuillErrCode FILESQL::file_open()
{
    if (is_dummy) return QUILL_SUCCESS;
    
	if (!outfilename) {
		dprintf(D_ALWAYS,"No SQL log file specified\n");
		return QUILL_FAILURE;
	}

	outfiledes = safe_open_wrapper_follow(outfilename,fileflags,0644);

	if(outfiledes < 0)
	{
		dprintf(D_ALWAYS,"Error opening SQL log file %s : %s\n",outfilename,strerror(errno));
		is_open = false;
		return QUILL_FAILURE;
	}
	else
	{
		is_open = true;
		
		/* Create a lock object when opening the file.
		 * Note: We very purposefully pass the "outfilename" to the
		 * FileLock ctor here; doing so enables the FileLock object
		 * to use kernel mutexes instead of on-disk file locks.  Not
		 * only is this more efficient, but on Windows it is required
		 * because later we will try to read from the locked file.  If
		 * it is "really" locked -vs- using Condor's advisory locking
		 * via the kernel mutexes, these reads will fail on Win32.
		 */
		lock = new FileLock(outfiledes,NULL,outfilename); 
		return QUILL_SUCCESS;
	}
}
Exemple #3
0
int
LoadData (const char * file_name, void *& data, int & data_size) {
  priv_state priv = set_root_priv();
  
  int fd = safe_open_wrapper_follow(file_name, O_RDONLY);
  if (fd == -1) {
    fprintf (stderr, "Can't open %s\n", file_name);
    set_priv (priv);
    return FALSE;
  }
  
  char buff [MAX_CRED_DATA_SIZE+1];
  data_size = read (fd, buff, MAX_CRED_DATA_SIZE);

  close (fd);

  if (data_size <= 0) {
    set_priv (priv);
    return FALSE;
  }

  data = malloc (data_size);

  memcpy (data, buff, data_size);

  set_priv (priv);
  return TRUE;

}
Exemple #4
0
int handleHistoryDir(ReliSock *sock) {
	int  result = -1;
	filesize_t filesize;
	char *filename = 0;

	sock->decode();

	sock->code(result);
	while (result == 1) {
			int fd = -1;
			filename = NULL;
			sock->code(filename);
			fd = safe_open_wrapper_follow(filename, O_CREAT | O_WRONLY);
			if (fd < 0) {
				printf("Can't open local file %s for writing\n", filename);
				exit(1);
			}
			result = sock->get_file(&filesize,fd,0);
			close(fd);
			
			sock->code(result);
			free(filename);
	}
	return 0;
}
bool
BaseLinuxHibernator::writeSysFile ( const char *file, const char *str ) const
{
	// Write to the "/sys or /proc" file(s)
	dprintf( D_FULLDEBUG,
			 "LinuxHibernator: Writing '%s' to '%s'\n", str, file );
	priv_state p = set_root_priv( );
	int fd = safe_open_wrapper_follow( file, O_WRONLY );
	set_priv( p );
	if ( fd < 0 ) {
		dprintf( D_ALWAYS,
				 "LinuxHibernator: Error writing '%s' to '%s': %s\n",
				 str, file, strerror(errno) );
		return false;
	}
	int len = strlen(str);
	if ( write( fd, str, len ) != len ) {
		close( fd );
		dprintf( D_ALWAYS,
				 "LinuxHibernator: Error writing '%s' to '%s': %s\n",
				 str, file, strerror(errno) );
		return false;
	}
	close( fd );
	return true;
}
Exemple #6
0
bool
read_file (const char * filename, char *& data, int & size) {

	int fd = safe_open_wrapper_follow(filename, O_RDONLY);
	if (fd == -1) {
		return false;
	}

	struct stat my_stat;
	if (fstat (fd, &my_stat) != 0) {
		close (fd);
		return false;
	}

	size = (int)my_stat.st_size;
	data = (char*)malloc(size+1);
	data[size]='\0';
	if (!data) 
		return false;
 	
	if (!read (fd, data, size)) {
		free (data);
		return false;
	}

	close (fd);
	return true;
}
//---------------------------------------------------------------------------
bool fileExists(const MyString &strFile)
{
	int fd = safe_open_wrapper_follow(strFile.Value(), O_RDONLY);
	if (fd == -1)
		return false;
	close(fd);
	return true;
}
Exemple #8
0
// Read the history from a single file and print it out. 
static void readHistoryFromFile(char *JobHistoryFileName)
{
    int EndFlag   = 0;
    int ErrorFlag = 0;
    int EmptyFlag = 0;
    AttrList *ad = NULL;

    MyString buf;
    
    int fd = safe_open_wrapper_follow(JobHistoryFileName, O_RDONLY | O_LARGEFILE);
	if (fd < 0) {
        fprintf(stderr,"History file (%s) not found or empty.\n", JobHistoryFileName);
        exit(1);
    }
    
	FILE *LogFile = fdopen(fd, "r");
	if (!LogFile) {
        fprintf(stderr,"History file (%s) not found or empty.\n", JobHistoryFileName);
        exit(1);
    }


    while(!EndFlag) {

        if( !( ad=new AttrList(LogFile,"***", EndFlag, ErrorFlag, EmptyFlag) ) ){
            fprintf( stderr, "Error:  Out of memory\n" );
            exit( 1 );
        } 
        if( ErrorFlag ) {
            printf( "\t*** Warning: Bad history file; skipping malformed ad(s)\n" );
            ErrorFlag=0;
            if(ad) {
                delete ad;
                ad = NULL;
            }
            continue;
        } 
        if( EmptyFlag ) {
            EmptyFlag=0;
            if(ad) {
                delete ad;
                ad = NULL;
            }
            continue;
        }

		insertHistoryJob(ad);

        if(ad) {
            delete ad;
            ad = NULL;
        }
    }
    fclose(LogFile);
    return;
}
BackwardFileReader::BackwardFileReader(std::string filename, int open_flags)
	: error(0), file(NULL), cbFile(0), cbPos(0) 
{
#ifdef WIN32
	open_flags |= O_BINARY;
#endif
	int fd = safe_open_wrapper_follow(filename.c_str(), open_flags);
	if (fd < 0)
		error = errno;
	else if ( ! OpenFile(fd, "rb"))
		close(fd);
}
Exemple #10
0
/*
  Open the named pipe in the given mode, and get the file descriptor to
  be "target_fd".
*/
void
open_named_pipe( const char *name, int mode, int target_fd )
{
	int		fd;

	if( (fd=safe_open_wrapper_follow(name,mode)) < 0 ) {
		EXCEPT( "Can't open named pipe %s", name );
	}

	if( fd != target_fd ) {
		ASSERT(dup2(fd, target_fd) >= 0);
		(void)close(fd);
	}
}
Exemple #11
0
void
FileLock::SetFdFpFile( int fd, FILE *fp, const char *file )
{
	// if I'm -1, NULL, NULL, that's ok, however, if file != NULL, then
	// either the fd or the fp must also be valid.
	if ((file == NULL && (fd >= 0 || fp != NULL)))
	{
		EXCEPT("FileLock::SetFdFpFile(). You must supply a valid file argument "
			"with a valid fd or fp_arg");
	}
#ifndef WIN32	
	if (m_delete == 1) {
		char *nPath = CreateHashName(file);	
		SetPath(nPath);	
		delete []nPath;
		close(m_fd);	
		m_fd = safe_open_wrapper_follow( m_path, O_RDWR | O_CREAT, 0644 );
		if (m_fd < 0) {
			dprintf(D_FULLDEBUG, "Lock File %s cannot be created.\n", m_path); 
			return;
		}
		updateLockTimestamp(); 
		return;
	}
#endif
	m_fd = fd;
	m_fp = fp;

	// Make sure we record our existence in the static list properly depending
	// on what the user is setting the variables to...
	if (m_path == NULL && file != NULL) {
		// moving from a NULL object to a object needed to update the timestamp

		SetPath(file);
		// This will use the new lock file in m_path
		updateLockTimestamp();

	} else if (m_path != NULL && file == NULL) {
		// moving from an updatable timestamped object to a NULL object

		SetPath(NULL);

	} else if (m_path != NULL && file != NULL) {
		// keeping the updatability of the object, but updating the path.
		
		SetPath(file);
		updateLockTimestamp();
	}
}
unsigned long
get_file_size(char *path)
{
	int		fd;
	unsigned long answer;

	fd = safe_open_wrapper_follow(path, O_RDONLY, 0);
	if (fd < 0) {
		return 0;
	}

	answer=lseek(fd,0,2);
	close(fd);
	return answer;
}
Exemple #13
0
int readFileIntoString(char* fileName, char* resultString, int size){
  // this is a retarded way to read the file, but it works.
  int fd = safe_open_wrapper_follow(fileName, O_RDONLY);
  if(fd < 0){
    cerr << "couldn't open" << fileName << endl;
    exit(1);
  }
  int nread = read(fd, resultString, size);
  if(nread < 0){
    cerr << "couldn't read" << fileName << endl;
    exit(1);
  }    
  close(fd);
  resultString[nread] = '\0';
  return 0;
}
Exemple #14
0
int write_factory_file(const char * filename, const void* data, size_t cb, mode_t access)
{
	int fd = safe_open_wrapper_follow(filename, O_WRONLY|_O_BINARY|O_CREAT|O_TRUNC|O_APPEND, access);
	if (fd == -1) {
		dprintf(D_ALWAYS, "ERROR: write_factory_file(%s): open() failed: %s (%d)\n", filename, strerror(errno), errno);
		return -1;
	}

	size_t cbwrote = write(fd, data, cb);
	if (cbwrote != cb) {
		dprintf(D_ALWAYS, "ERROR: write_factory_file(%s): write() failed: %s (%d)\n", filename, strerror(errno), errno);
		return -1;
	}

	close(fd);
	return 0;
}
MyString slurp_file(const char * filename) {
    int fd = safe_open_wrapper_follow(filename, O_RDONLY);
    if(fd == -1) {
        die("failed to open input");
    }
    MyString s;
    char buf[1024];
    while(true) {
        int bytes = read(fd, buf, sizeof(buf));
        for(int i = 0; i < bytes; ++i) {
            s += buf[i];
        }
        if(bytes != sizeof(buf)) {
            break;
        }
    }
    return s;
}
Exemple #16
0
int
StoreData (const char * file_name, const void * data, const int data_size) {

  if (!data) {
    return FALSE;
  }


  priv_state priv = set_root_priv();
  dprintf (D_FULLDEBUG, "in StoreData(), euid=%d\n", geteuid());

  int fd = safe_open_wrapper_follow(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0600 );
  if (fd == -1) {
    dprintf (D_ALWAYS, "Unable to store in %s\n", file_name);
    set_priv(priv);
    return FALSE;
  }

  // Change to user owning the cred (assume init_user_ids() has been called)
  if (fchmod (fd, S_IRUSR | S_IWUSR)) {
	  dprintf(D_ALWAYS, "Failed to fchmod %s to S_IRUSR | S_IWUSR: %s\n",
			  file_name, strerror(errno));
  }
  if (fchown (fd, get_user_uid(), get_user_gid())) {
	  dprintf(D_ALWAYS, "Failed to fchown %s to %d.%d: %s\n",
			  file_name, get_user_uid(), get_user_gid(), strerror(errno));
  }

  int written = write (fd, data, data_size);
  if (written < data_size) {
    dprintf (D_ALWAYS, "Can't write to %s: (%d) \n", file_name, errno);
    set_priv(priv);
	close(fd);
    return FALSE;
  }

  close (fd);

  set_priv(priv);
  return TRUE;
}
Exemple #17
0
bool Condor_MD_MAC::addMDFile(const char * filePathName)
{
#ifdef HAVE_EXT_OPENSSL
	int fd;

    fd = safe_open_wrapper_follow(filePathName, O_RDONLY | O_LARGEFILE, 0);
    if (fd < 0) {
        dprintf(D_ALWAYS,
                "addMDFile: can't open %s: %s\n",
                filePathName,
                strerror(errno));
        return false;
    }

	unsigned char *buffer;	

	buffer = (unsigned char *)calloc(1024*1024, 1);
	ASSERT(buffer != NULL);

	bool ok = true;
	ssize_t count = read(fd, buffer, 1024*1024); 
	while( count > 0) {
		MD5_Update(&(context_->md5_), buffer, count); 
		memset(buffer, 0, 1024*1024);
		count = read(fd, buffer, 1024*1024); 
	}
	if (count == -1) {
		dprintf(D_ALWAYS,
		        "addMDFile: error reading from %s: %s\n",
		        filePathName,
		        strerror(errno));
		ok = false;
	}

	close(fd);
	free(buffer);
	return ok;
#else
	return false;
#endif
}
bool
NamedPipeWriter::initialize(const char* addr)
{
	// open a write-only connection to the server
	//
	m_pipe = safe_open_wrapper_follow(addr, O_WRONLY | O_NONBLOCK);
	if (m_pipe == -1) {
		dprintf(D_ALWAYS,
		        "error opening %s: %s (%d)\n",
		        addr,
		        strerror(errno),
		        errno);
		return false;
	}

	// set it back into blocking mode
	//
	int flags = fcntl(m_pipe, F_GETFL);
	if (flags == -1) {
		dprintf(D_ALWAYS,
		        "fcntl error: %s (%d)\n",
		        strerror(errno),
		        errno);
		close(m_pipe);
		m_pipe = -1;
		return false;
	}
	if (fcntl(m_pipe, F_SETFL, flags & ~O_NONBLOCK) == -1) {
		dprintf(D_ALWAYS,
		        "fcntl error: %s (%d)\n",
		        strerror(errno),
		        errno);
		close(m_pipe);
		m_pipe = -1;
		return false;
	}

	m_initialized = true;
	return true;
}
Exemple #19
0
// writes a pool password file using the given password
// returns SUCCESS or FAILURE
//
int write_password_file(const char* path, const char* password)
{
		int fd = safe_open_wrapper_follow(path,
		                           O_WRONLY | O_CREAT | O_TRUNC,
		                           0600);
		if (fd == -1) {
			dprintf(D_ALWAYS,
			        "store_cred_service: open failed on %s: %s (%d)\n",
			        path,
			        strerror(errno),
					errno);
			return FAILURE;
		}
		FILE *fp = fdopen(fd, "w");
		if (fp == NULL) {
			dprintf(D_ALWAYS,
			        "store_cred_service: fdopen failed: %s (%d)\n",
			        strerror(errno),
			        errno);
			return FAILURE;
		}
		size_t password_len = strlen(password);
		char scrambled_password[MAX_PASSWORD_LENGTH + 1];
		memset(scrambled_password, 0, MAX_PASSWORD_LENGTH + 1);
		simple_scramble(scrambled_password, password, password_len);
		size_t sz = fwrite(scrambled_password, 1, MAX_PASSWORD_LENGTH + 1, fp);
		int save_errno = errno;
		fclose(fp);
		if (sz != MAX_PASSWORD_LENGTH + 1) {
			dprintf(D_ALWAYS,
			        "store_cred_service: "
			            "error writing to password file: %s (%d)\n",
					strerror(save_errno),
			        save_errno);
			return FAILURE;
		}
		return SUCCESS;
}
Exemple #20
0
int
ReadState(const Options				&opts,
          ReadUserLog::FileState	&state )
{

    // Create & initialize the state
    ReadUserLog::InitFileState( state );

    printf( "Reading state %s\n", opts.getFile() );
    int	fd = safe_open_wrapper_follow( opts.getFile(), O_RDONLY, 0 );
    if ( fd < 0 ) {
        fprintf( stderr, "Failed to read state file %s\n", opts.getFile() );
        return -1;
    }

    if ( read( fd, state.buf, state.size ) != state.size ) {
        fprintf( stderr, "Failed reading state file %s\n", opts.getFile() );
        close( fd );
        return -1;
    }
    close( fd );

    return 0;
}
Exemple #21
0
bool
VMGahpServer::startUp(Env *job_env, const char *workingdir, int nice_inc, FamilyInfo *family_info)
{
    //check if we already have spawned a vmgahp server
    if( m_vmgahp_pid > 0 ) {
        //vmgahp is already running
        return true;
    }

    if( !m_job_ad ) {
        start_err_msg = "No JobAd in VMGahpServer::startUp()";
        dprintf(D_ALWAYS,"%s\n", start_err_msg.Value());
        return false;
    }

    MyString JobName;
    if( m_vmgahp_server.IsEmpty() ) {
        start_err_msg = "No path for vmgahp in VMGahpServer::startUp()";
        dprintf(D_ALWAYS,"%s\n", start_err_msg.Value());
        return false;
    }

    JobName = m_vmgahp_server;

    // Create two pairs of pipes which we will use to
    int stdin_pipefds[2];
    int stdout_pipefds[2];
    int stderr_pipefds[2];

    if(!daemonCore->Create_Pipe(stdin_pipefds,
                                true, // read end registerable
                                false, // write end not registerable
                                false, // read end blocking
                                false // write end blocking
                               )) {
        start_err_msg = "unable to create pipe to stdin of VM gahp";
        dprintf(D_ALWAYS,"%s\n", start_err_msg.Value());
        return false;
    }
    if(!daemonCore->Create_Pipe(stdout_pipefds,
                                true,  //read end registerable
                                false, // write end not registerable
                                false, // read end blocking
                                false // write end blocking
                               )) {
        // blocking read
        start_err_msg = "unable to create pipe to stdout of VM gahp";
        dprintf(D_ALWAYS,"%s\n", start_err_msg.Value());
        return false;
    }
    if( m_include_gahp_log ) {
        if(!daemonCore->Create_Pipe(stderr_pipefds,
                                    true,  // read end registerable
                                    false, // write end not registerable
                                    true,  // read end non-blocking
                                    true  // write end non-blocking
                                   )) {
            // nonblocking read
            start_err_msg = "unable to create pipe to stderr of VM gahp";
            dprintf(D_ALWAYS,"%s\n", start_err_msg.Value());
            return false;
        }
    }

    int io_redirect[3];
    io_redirect[0] = stdin_pipefds[0];	//stdin gets read side of in pipe
    io_redirect[1] = stdout_pipefds[1];	//stdout gets write side of out pipe
    if( m_include_gahp_log ) {
        io_redirect[2] = stderr_pipefds[1];	//stderr gets write side of err pipe
    } else {
        int null_fd = safe_open_wrapper_follow(NULL_FILE, O_WRONLY | O_APPEND, 0666);
        if( null_fd < 0 ) {
            start_err_msg = "unable to open null file for stderr of VM gahp";
            dprintf(D_ALWAYS,"Failed to open '%s':%s (errno %d)\n",
                    NULL_FILE, strerror(errno), errno);
            return false;
        }
        io_redirect[2] = null_fd;
    }

    // Set Arguments
    ArgList vmgahp_args;

    vmgahp_args.SetArgV1SyntaxToCurrentPlatform();
    vmgahp_args.AppendArg(m_vmgahp_server.Value());

    // Add daemonCore options
    vmgahp_args.AppendArg("-f");
    if( m_include_gahp_log ) {
        vmgahp_args.AppendArg("-t");
    }
    vmgahp_args.AppendArg("-M");
    vmgahp_args.AppendArg(VMGAHP_STANDALONE_MODE);

    MyString args_string;
    vmgahp_args.GetArgsStringForDisplay(&args_string, 1);
    dprintf( D_ALWAYS, "About to exec %s %s\n", JobName.Value(),
             args_string.Value() );

#if !defined(WIN32)
    uid_t vmgahp_user_uid = (uid_t) -1;
    gid_t vmgahp_user_gid = (gid_t) -1;

    if( can_switch_ids() ) {
        // Condor runs as root
        vmgahp_user_uid = get_user_uid();
        vmgahp_user_gid = get_user_gid();
    }
    else if (Starter->condorPrivSepHelper() != NULL) {
        vmgahp_user_uid = Starter->condorPrivSepHelper()->get_uid();
        char* user_name;
        if (!pcache()->get_user_name(vmgahp_user_uid, user_name)) {
            EXCEPT("unable to get user name for UID %u", vmgahp_user_uid);
        }
        if (!pcache()->get_user_ids(user_name,
                                    vmgahp_user_uid,
                                    vmgahp_user_gid))
        {
            EXCEPT("unable to get GID for UID %u", vmgahp_user_uid);
        }
        free(user_name);
    }
    else {
        // vmgahp may have setuid-root (e.g. vmgahp for Xen)
        vmgahp_user_uid = get_condor_uid();
        vmgahp_user_gid = get_condor_gid();
    }

    // Setup vmgahp user uid/gid
    if( vmgahp_user_uid > 0 ) {
        if( vmgahp_user_gid <= 0 ) {
            vmgahp_user_gid = vmgahp_user_uid;
        }

        MyString tmp_str;
        tmp_str.sprintf("%d", (int)vmgahp_user_uid);
        job_env->SetEnv("VMGAHP_USER_UID", tmp_str.Value());
        tmp_str.sprintf("%d", (int)vmgahp_user_gid);
        job_env->SetEnv("VMGAHP_USER_GID", tmp_str.Value());
    }
#endif

    job_env->SetEnv("VMGAHP_VMTYPE", m_vm_type.Value());
    job_env->SetEnv("VMGAHP_WORKING_DIR", workingdir);

    // Grab the full environment back out of the Env object
    if(IsFulldebug(D_FULLDEBUG)) {
        MyString env_str;
        job_env->getDelimitedStringForDisplay(&env_str);
        dprintf(D_FULLDEBUG, "Env = %s\n", env_str.Value());
    }

    priv_state vmgahp_priv = PRIV_ROOT;
#if defined(WIN32)
    // TODO..
    // Currently vmgahp for VMware VM universe can't run as user on Windows.
    // It seems like a bug of VMware. VMware command line tool such as "vmrun"
    // requires Administrator privilege.
    // -jaeyoung 06/15/07
    if( strcasecmp(m_vm_type.Value(), CONDOR_VM_UNIVERSE_VMWARE ) == MATCH ) {
        vmgahp_priv = PRIV_UNKNOWN;
    }
#endif

    m_vmgahp_pid = daemonCore->Create_Process(
                       JobName.Value(), //Name of executable
                       vmgahp_args,	//Args
                       vmgahp_priv, 	//Priv state
                       1, 		//id for our registered reaper
                       FALSE, 		//do not want a command port
                       job_env, 	//env
                       workingdir,	//cwd
                       family_info,		//family_info
                       NULL, 		//network sockets to inherit
                       io_redirect,	//redirect stdin/out/err
                       NULL,
                       nice_inc
                   );

    //NOTE: Create_Process() saves the errno for us if it is an
    //"interesting" error.
    char const *create_process_error = NULL;
    if(m_vmgahp_pid == FALSE && errno) create_process_error = strerror(errno);

    // Now that the VMGAHP server is running, close the sides of
    // the pipes we gave away to the server, and stash the ones
    // we want to keep in an object data member.
    daemonCore->Close_Pipe(io_redirect[0]);
    daemonCore->Close_Pipe(io_redirect[1]);
    if( m_include_gahp_log ) {
        daemonCore->Close_Pipe(io_redirect[2]);
    } else {
        close(io_redirect[2]);
    }

    if ( m_vmgahp_pid == FALSE ) {
        m_vmgahp_pid = -1;
        start_err_msg = "Failed to start vm-gahp server";
        dprintf(D_ALWAYS, "%s (%s)\n", start_err_msg.Value(),
                m_vmgahp_server.Value());
        if(create_process_error) {
            MyString err_msg = "Failed to execute '";
            err_msg += m_vmgahp_server.Value(),
                       err_msg += "'";
            if(!args_string.IsEmpty()) {
                err_msg += " with arguments ";
                err_msg += args_string.Value();
            }
            err_msg += ": ";
            err_msg += create_process_error;
            dprintf(D_ALWAYS, "Failed to start vmgahp server (%s)\n",
                    err_msg.Value());
        }
        return false;
    }

    dprintf(D_ALWAYS, "VMGAHP server pid=%d\n", m_vmgahp_pid);

    m_vmgahp_writefd = stdin_pipefds[1];
    m_vmgahp_readfd = stdout_pipefds[0];
    if( m_include_gahp_log ) {
        m_vmgahp_errorfd = stderr_pipefds[0];
    }

    // Now initialization is done
    m_is_initialized = true;

    // print initial stderr messages from vmgahp
    printSystemErrorMsg();

    // Read the initial greeting from the vm-gahp, which is the version
    if( command_version() == false ) {
        start_err_msg = "Internal vmgahp server error";
        dprintf(D_ALWAYS,"Failed to read vmgahp server version\n");
        printSystemErrorMsg();
        cleanup();
        return false;
    }

    dprintf(D_FULLDEBUG,"VMGAHP server version: %s\n", m_vmgahp_version.Value());

    // Now see what commands this server supports.
    if( command_commands() == false ) {
        start_err_msg = "Internal vmgahp server error";
        dprintf(D_ALWAYS,"Failed to read supported commands from vmgahp server\n");
        printSystemErrorMsg();
        cleanup();
        return false;
    }

    // Now see what virtual machine types this server supports
    if( command_support_vms() == false ) {
        start_err_msg = "Internal vmgahp server error";
        dprintf(D_ALWAYS,"Failed to read supported vm types from vmgahp server\n");
        printSystemErrorMsg();
        cleanup();
        return false;
    }

    int result = -1;
    if( m_include_gahp_log ) {
        result = daemonCore->Register_Pipe(m_vmgahp_errorfd,
                                           "m_vmgahp_errorfd",
                                           static_cast<PipeHandlercpp>(&VMGahpServer::err_pipe_ready),
                                           "VMGahpServer::err_pipe_ready",this);

        if( result == -1 ) {
            dprintf(D_ALWAYS,"Failed to register vmgahp stderr pipe\n");
            if(m_stderr_tid != -1) {
                daemonCore->Cancel_Timer(m_stderr_tid);
                m_stderr_tid = -1;
            }
            m_stderr_tid = daemonCore->Register_Timer(2,
                           2, (TimerHandlercpp)&VMGahpServer::err_pipe_ready,
                           "VMGahpServer::err_pipe_ready",this);
            if( m_stderr_tid == -1 ) {
                start_err_msg = "Internal vmgahp server error";
                dprintf(D_ALWAYS,"Failed to register stderr timer\n");
                printSystemErrorMsg();
                cleanup();
                return false;
            }
        }
    }

    // try to turn on vmgahp async notification mode
    if  ( !command_async_mode_on() ) {
        // not supported, set a poll interval
        m_is_async_mode = false;
        setPollInterval(m_pollInterval);
    } else {
        // command worked... register the pipe and stop polling
        result = daemonCore->Register_Pipe(m_vmgahp_readfd,
                                           "m_vmgahp_readfd",
                                           static_cast<PipeHandlercpp>(&VMGahpServer::pipe_ready),
                                           "VMGahpServer::pipe_ready",this);
        if( result == -1 ) {
            // failed to register the pipe for some reason; fall
            // back on polling (yuck).
            dprintf(D_ALWAYS,"Failed to register vmgahp Read pipe\n");
            m_is_async_mode = false;
            setPollInterval(m_pollInterval);
        } else {
            // pipe is registered.  stop polling.
            setPollInterval(0);
            m_is_async_mode = true;
        }
    }

    return true;
}
Exemple #22
0
void
GangliaD::initAndReconfig()
{
	std::string libname;
	std::string gmetric_path;
	param(libname,"GANGLIA_LIB");
	param(gmetric_path,"GANGLIA_GMETRIC");
	if( libname.empty() && gmetric_path.empty()) {
		std::string libpath;
		char const *libpath_param = "GANGLIA_LIB_PATH";
		if( __WORDSIZE == 64 ) {
			libpath_param = "GANGLIA_LIB64_PATH";
		}
		param(libpath,libpath_param);

		dprintf(D_FULLDEBUG,"Searching for libganglia in %s=%s\n",libpath_param,libpath.c_str());
		locateSharedLib(libpath,"libganglia",libname);

		gmetric_path = "gmetric";

		if( libname.empty() && gmetric_path.empty()) {
			EXCEPT("libganglia was not found via %s, and GANGLIA_LIB is not configured, "
				   "and GANGLIA_GMETRIC is not configured.  "
				   "Ensure that libganglia is installed in a location included in %s "
				   "or configure GANGLIA_LIB and/or GANGLIA_GMETRIC.",
				   libpath_param, libpath_param);
		}
	}
	m_ganglia_noop = false;
	if( libname == "NOOP" ) {
		dprintf(D_ALWAYS,"GANGLIA_LIB=NOOP, so we will go through the motions, but not actually interact with ganglia\n");
		m_ganglia_noop = true;
	}

	if( !m_ganglia_noop ) {
		bool gmetric_initialized = false;
		bool libganglia_initialized = false;
		if( !gmetric_path.empty() ) {
			dprintf(D_ALWAYS,"Testing %s\n",gmetric_path.c_str());
			if( ganglia_init_gmetric(gmetric_path.c_str()) ) {
				gmetric_initialized = true;
			}
		}
		if( !libname.empty() ) {
			if( libname == m_ganglia_libname ) {
				libganglia_initialized = true;
				dprintf(D_ALWAYS,"Already loaded libganglia %s\n",libname.c_str());
				// I have observed instabilities when reloading the library, so
				// it is best to not do that unless it is really necessary.
			}
			else {
				dprintf(D_ALWAYS,"Loading libganglia %s\n",libname.c_str());
				ganglia_config_destroy(&m_ganglia_context,&m_ganglia_config,&m_ganglia_channels);
				if( ganglia_load_library(libname.c_str()) ) {
					libganglia_initialized = true;
					m_ganglia_libname = libname;
				}
				else if( gmetric_initialized ) {
					dprintf(D_ALWAYS,"WARNING: failed to load %s, so gmetric (which is slower) will be used instead.\n",libname.c_str());
				}
			}
		}

		if( libganglia_initialized ) {
			dprintf(D_ALWAYS,"Will use libganglia to interact with ganglia.\n");
		}
		else if( gmetric_initialized ) {
			dprintf(D_ALWAYS,"Will use gmetric to interact with ganglia.\n");
		}
		else {
			EXCEPT("Neither gmetric nor libganglia were successfully initialized.  Aborting");
		}

		std::string ganglia_conf_location;
		param(ganglia_conf_location, "GANGLIA_CONFIG", "/etc/ganglia/gmond.conf");
		int fd;
		if ((fd = safe_open_wrapper_follow(ganglia_conf_location.c_str(), O_RDONLY)) < 0)
		{
			EXCEPT("Cannot open Ganglia configuration file GANGLIA_CONFIG=%s.", ganglia_conf_location.c_str());
			return;
		}
		close(fd);

		if( !ganglia_reconfig(ganglia_conf_location.c_str(),&m_ganglia_context,&m_ganglia_config,&m_ganglia_channels) ) {
			EXCEPT("Failed to configure ganglia library.");
		}
	}

	param(m_gstat_command, "GANGLIA_GSTAT_COMMAND");
    split_args(m_gstat_command.c_str(), &m_gstat_argv);

    m_send_data_for_all_hosts = param_boolean("GANGLIA_SEND_DATA_FOR_ALL_HOSTS", false);

	StatsD::initAndReconfig("GANGLIAD");

	// the interval we tell ganglia is the max time between updates
	m_tmax = m_stats_pub_interval*2;
	// the interval we tell ganglia is the lifetime of the metric
	if( m_tmax*3 < 86400 ) {
		m_dmax = 86400;
	}
	else {
		m_dmax = m_tmax*3;
	}
}
Exemple #23
0
// Read the history from a single file and print it out. 
static void readHistoryFromFileOld(const char *JobHistoryFileName, const char* constraint, ExprTree *constraintExpr)
{
    int EndFlag   = 0;
    int ErrorFlag = 0;
    int EmptyFlag = 0;
    ClassAd *ad = NULL;

    long offset = 0;
    bool BOF = false; // Beginning Of File
    MyString buf;

	int flags = 0;
	if( !backwards ) {
			// Currently, the file position manipulations used in -backwards
			// do not work with files > 2GB on platforms with 32-bit file
			// offsets.
		flags = O_LARGEFILE;
	}
	int LogFd = safe_open_wrapper_follow(JobHistoryFileName,flags,0);
	if (LogFd < 0) {
		fprintf(stderr,"Error opening history file %s: %s\n", JobHistoryFileName,strerror(errno));
#ifdef EFBIG
		if( (errno == EFBIG) && backwards ) {
			fprintf(stderr,"The -backwards option does not support files this large.\n");
		}
#endif
		exit(1);
	}

	FILE *LogFile = fdopen(LogFd,"r");
	if (!LogFile) {
		fprintf(stderr,"Error opening history file %s: %s\n", JobHistoryFileName,strerror(errno));
		exit(1);
	}

	// In case of rotated history files, check if we have already reached the number of 
	// matches specified by the user before reading the next file
	if (specifiedMatch != 0) { 
        if (matchCount == specifiedMatch) { // Already found n number of matches, cleanup  
            fclose(LogFile);
            return;
        }
	}

	if (backwards) {
        offset = findLastDelimiter(LogFile, JobHistoryFileName);	
    }


	if(longformat && use_xml) {
		std::string out;
		AddClassAdXMLFileHeader(out);
		printf("%s\n", out.c_str());
	}

    while(!EndFlag) {

        if (backwards) { // Read history file backwards
            if (BOF) { // If reached beginning of file
                break;
            }
            
            offset = findPrevDelimiter(LogFile, JobHistoryFileName, offset);
            if (offset == -1) { // Unable to match constraint
                break;
            } else if (offset != 0) {
                fseek(LogFile, offset, SEEK_SET);
                buf.readLine(LogFile); // Read one line to skip delimiter and adjust to actual offset of ad
            } else { // Offset set to 0
                BOF = true;
                fseek(LogFile, offset, SEEK_SET);
            }
        }
      
        if( !( ad=new ClassAd(LogFile,"***", EndFlag, ErrorFlag, EmptyFlag) ) ){
            fprintf( stderr, "Error:  Out of memory\n" );
            exit( 1 );
        } 
        if( ErrorFlag ) {
            printf( "\t*** Warning: Bad history file; skipping malformed ad(s)\n" );
            ErrorFlag=0;
            if(ad) {
                delete ad;
                ad = NULL;
            }
            continue;
        } 
        if( EmptyFlag ) {
            EmptyFlag=0;
            if(ad) {
                delete ad;
                ad = NULL;
            }
            continue;
        }
        if (!constraint || constraint[0]=='\0' || EvalBool(ad, constraintExpr)) {
            if (longformat) { 
				if( use_xml ) {
					fPrintAdAsXML(stdout, *ad);
				}
				else {
					fPrintAd(stdout, *ad);
				}
				printf("\n"); 
            } else {
                if (customFormat) {
                    mask.display(stdout, ad);
                } else {
                    displayJobShort(ad);
                }
            }

            matchCount++; // if control reached here, match has occured

            if (specifiedMatch != 0) { // User specified a match number
                if (matchCount == specifiedMatch) { // Found n number of matches, cleanup  
                    if (ad) {
                        delete ad;
                        ad = NULL;
                    }
                    
                    fclose(LogFile);
                    return;
                }
            }
		}
		
        if(ad) {
            delete ad;
            ad = NULL;
        }
    }
	if(longformat && use_xml) {
		std::string out;
		AddClassAdXMLFileFooter(out);
		printf("%s\n", out.c_str());
	}
    fclose(LogFile);
    return;
}
Exemple #24
0
bool
FileLock::obtain( LOCK_TYPE t )
{
	int counter = 0; 
#if !defined(WIN32)
	start: 
#endif	
// lock_file uses lseeks in order to lock the first 4 bytes of the file on NT
// It DOES properly reset the lseek version of the file position, but that is
// not the same (in some very inconsistent and weird ways) as the fseek one,
// so if the user has given us a FILE *, we need to make sure we don't ruin
// their current position.  The lesson here is don't use fseeks and lseeks
// interchangeably...
	int		status = -1;
	int saved_errno = -1;

	if ( m_use_kernel_mutex == -1 ) {
		m_use_kernel_mutex = param_boolean_int("FILE_LOCK_VIA_MUTEX", TRUE);
	}

		// If we have the path, we can try to lock via a mutex.  
	if ( m_path && m_use_kernel_mutex ) {
		status = lockViaMutex(t);
	}

		// We cannot lock via a mutex, or we tried and failed.
		// Try via filesystem lock.
	if ( status < 0) {
		long lPosBeforeLock = 0;
		if (m_fp) // if the user has a FILE * as well as an fd
		{
			// save their FILE*-based current position
			lPosBeforeLock = ftell(m_fp); 
		}
		
			// We're seeing sporadic test suite failures where a daemon
			// takes more than 10 seconds to write to the user log.
			// This will help narrow down where the delay is coming from.
		time_t before = time(NULL);
		status = lock_file( m_fd, t, m_blocking );
		saved_errno = errno;
		time_t after = time(NULL);
		if ( (after - before) > 5 ) {
			dprintf( D_FULLDEBUG,
					 "FileLock::obtain(%d): lock_file() took %ld seconds\n",
					 t, (after-before) );
		}
		
		if (m_fp)
		{
			// restore their FILE*-position
			fseek(m_fp, lPosBeforeLock, SEEK_SET); 	
		}

#ifndef WIN32		
			// if we deal with our own fd and are not unlocking
		if (m_delete == 1 && t != UN_LOCK){
			struct stat si; 
			fstat(m_fd, &si);
				// no more hard links ... it was deleted while we were waiting
				// in that case we need to reopen and restart
			if ( si.st_nlink < 1 ){
				release();
				close(m_fd);
				bool initResult;
				if (m_orig_path != NULL && strcmp(m_path, m_orig_path) != 0)
					initResult = initLockFile(false);
				else 
					initResult = initLockFile(true);
				if (!initResult) {
					dprintf(D_FULLDEBUG, "Lock file (%s) cannot be reopened \n", m_path);
					if (m_orig_path) {
						dprintf(D_FULLDEBUG, "Opening and locking the actual log file (%s) since lock file cannot be accessed! \n", m_orig_path);
						m_fd = safe_open_wrapper_follow(m_orig_path, O_CREAT | O_RDWR , 0644);
					} 
				}
				
				if (m_fd < 0) {
					dprintf(D_FULLDEBUG, "Opening the log file %s to lock failed. \n", m_path);
				}
				++counter;
					// let's retry at most 5 times
				if (counter < 6) {
					goto start;
				}
				else 
					status = -1;
			}		
		}
#endif		
	}

	if( status == 0 ) {
		m_state = t;
	}
	if ( status != 0 ) {
		dprintf( D_ALWAYS, "FileLock::obtain(%d) failed - errno %d (%s)\n",
	                t, saved_errno, strerror(saved_errno) );
	}
	else {
		UtcTime	now( true );
		dprintf( D_FULLDEBUG,
				 "FileLock::obtain(%d) - @%.6f lock on %s now %s\n",
				 t, now.combined(), m_path, getStateString(t) );
	}
	return status == 0;
}
Exemple #25
0
static int 
gc_image(const std::string & image) {

  std::list<std::string> images;
  std::string imageFilename;
  
  int cache_size = param_integer("DOCKER_IMAGE_CACHE_SIZE", 20);
  cache_size--;
  if (cache_size < 0) cache_size = 0;

  if( ! param( imageFilename, "LOG" ) ) {
    dprintf(D_ALWAYS, "LOG not defined in param table, giving up\n");
    ASSERT(false);
  }

  
  TemporaryPrivSentry sentry(PRIV_ROOT);
  imageFilename += "/.startd_docker_images";

  int lockfd = safe_open_wrapper_follow(imageFilename.c_str(), O_WRONLY|O_CREAT, 0666);

  if (lockfd < 0) {
    dprintf(D_ALWAYS, "Can't open %s for locking: %s\n", imageFilename.c_str(), strerror(errno));
    ASSERT(false);
  }
  FileLock lock(lockfd, NULL, imageFilename.c_str());
  lock.obtain(WRITE_LOCK); // blocking

  FILE *f = safe_fopen_wrapper_follow(imageFilename.c_str(), "r");

  if (f) {
    char existingImage[1024];
    while ( fgets(existingImage, 1024, f)) {

      if (strlen(existingImage) > 1) {
	existingImage[strlen(existingImage) - 1] = '\0'; // remove newline
      }
      std::string tmp(existingImage);
      //
      // If we're reusing an image, we'll shuffle it to the end
      if (tmp != image) {
	images.push_back(tmp);
      }
    }
    fclose(f);
  }

  dprintf(D_ALWAYS, "Found %lu entries in docker image cache.\n", images.size());

   std::list<std::string>::iterator iter;
   int remove_count = (int)images.size() - cache_size;
   if (remove_count < 0) remove_count = 0;

   for (iter = images.begin(); iter != images.end(); iter++) {
    if (remove_count <= 0) break;
    std::string toRemove = *iter;

    CondorError err;
    int result = DockerAPI::rmi(toRemove, err);

    if (result == 0) {
      images.erase(iter);
      remove_count--;
    }
  }

  images.push_back(image); // our current image is the most recent one

  f = safe_fopen_wrapper_follow(imageFilename.c_str(), "w");
  if (f) {
    std::list<std::string>::iterator it;
    for (it = images.begin(); it != images.end(); it++) {
      fputs((*it).c_str(), f);
      fputs("\n", f);
    }
    fclose(f);
  } else {
    dprintf(D_ALWAYS, "Can't write to docker images file: %s\n", imageFilename.c_str());
    ASSERT(false);
  }

  lock.release();
  close(lockfd);

  return 0;
}
Exemple #26
0
int RefreshProxyThruMyProxy(Proxy * proxy)
{
	char * proxy_filename = proxy->proxy_filename;
	MyProxyEntry * myProxyEntry = NULL;
	MyString args_string;
	int pid;

	// Starting from the most recent myproxy entry
	// Find an entry with a password
	int found = FALSE;
	proxy->myproxy_entries.Rewind();
	while (proxy->myproxy_entries.Next (myProxyEntry)) {
		if (myProxyEntry->myproxy_password ||
			GetMyProxyPasswordFromSchedD (myProxyEntry->cluster_id,
										  myProxyEntry->proc_id,
										  &(myProxyEntry->myproxy_password))) {
			found=TRUE;

			//. Now move it to the front of the list
			proxy->myproxy_entries.DeleteCurrent();
			proxy->myproxy_entries.Prepend(myProxyEntry);
			break;
		}
	}

	if (!found) {
		// We're screwed - can't get MyProxy passwords for any entry
		return FALSE;
	}

	// Make sure we're not called more often than necessary and if
	time_t now=time(NULL);
	if ((myProxyEntry->get_delegation_pid != FALSE) ||
		(now - myProxyEntry->last_invoked_time < 30)) {

		dprintf (D_ALWAYS,
			 "proxy %s too soon or myproxy-get-delegation already started\n",
				 proxy_filename);
		return FALSE;
	}
	myProxyEntry->last_invoked_time=now;


	// If you don't have a myproxy password, ask SchedD for it
	if (!myProxyEntry->myproxy_password) {
		// Will there ever be a case when there is no MyProxy password needed at all?
		return FALSE;
	}

	// Initialize reaper, if needed
	if (myproxyGetDelegationReaperId == 0 ) {
		myproxyGetDelegationReaperId = daemonCore->Register_Reaper(
					   "GetDelegationReaper",
					   (ReaperHandler) &MyProxyGetDelegationReaper,
					   "GetDelegation Reaper");
 	}

	// Set up environnment for myproxy-get-delegation
	Env myEnv;
	std::string buff;

	if (myProxyEntry->myproxy_server_dn) {
		formatstr( buff, "MYPROXY_SERVER_DN=%s",
				 myProxyEntry->myproxy_server_dn);
		myEnv.SetEnv(buff.c_str());
		dprintf (D_FULLDEBUG, "%s\n", buff.c_str());
	}


	formatstr(buff, "X509_USER_PROXY=%s", proxy_filename);
	myEnv.SetEnv (buff.c_str());
	dprintf (D_FULLDEBUG, "%s\n", buff.c_str());


	// Print password (this will end up in stdin for myproxy-get-delegation)
	if (pipe (myProxyEntry->get_delegation_password_pipe)) {
		dprintf(D_ALWAYS,
				"Failed to pipe(2) in RefreshProxyThruMyProxy "
				"for writing password, aborting\n");
		return FALSE;
	}
	int written = write (myProxyEntry->get_delegation_password_pipe[1],
		   myProxyEntry->myproxy_password,
		   strlen (myProxyEntry->myproxy_password));
	if (written < (int) strlen (myProxyEntry->myproxy_password)) {
		dprintf(D_ALWAYS, "Failed to write to pipe in RefreshProxyThruMyProxy %d\n", errno);
		return FALSE;
    }
	written = write (myProxyEntry->get_delegation_password_pipe[1], "\n", 1);
	if (written < 1) {
		dprintf(D_ALWAYS, "Failed to write to pipe in RefreshProxyThruMyProxy %d\n", errno);
		return FALSE;
	}


	// Figure out user name;
	char * username = my_username(0);	


	// Figure out myproxy host and port
	char * myproxy_host = getHostFromAddr (myProxyEntry->myproxy_host);
	int myproxy_port = getPortFromAddr (myProxyEntry->myproxy_host);

	// args
	ArgList args;
	args.AppendArg(proxy_filename);
	args.AppendArg("-v");
	args.AppendArg("-o");
	args.AppendArg(proxy_filename);
	args.AppendArg("-s");
	args.AppendArg(myproxy_host);
	args.AppendArg("-d");
	args.AppendArg("-t");
	args.AppendArg(myProxyEntry->new_proxy_lifetime);
	args.AppendArg("-S");
	args.AppendArg("-l");
	args.AppendArg(username);


	// Optional port argument
	if (myproxy_port) {
		args.AppendArg("-p");
		args.AppendArg(myproxy_port);
	}

	// Optional credential name argument
	if (myProxyEntry->myproxy_credential_name) {
		args.AppendArg("-k");
		args.AppendArg(myProxyEntry->myproxy_credential_name);
	}

	free (username);
	free (myproxy_host);

	// Create temporary file to store myproxy-get-delegation's stderr
	myProxyEntry->get_delegation_err_filename = create_temp_file();
	if(!myProxyEntry->get_delegation_err_filename) {
		dprintf( D_ALWAYS, "Failed to create temp file");
	} else {
		MSC_SUPPRESS_WARNING_FIXME(6031) // warning: return value of 'chmod' ignored.
		chmod (myProxyEntry->get_delegation_err_filename, 0600);
		myProxyEntry->get_delegation_err_fd = safe_open_wrapper_follow(myProxyEntry->get_delegation_err_filename,O_RDWR);
		if (myProxyEntry->get_delegation_err_fd == -1) {
			dprintf (D_ALWAYS, "Error opening file %s\n",
					 myProxyEntry->get_delegation_err_filename);
		}
	}


	int arrIO[3];
	arrIO[0]=myProxyEntry->get_delegation_password_pipe[0]; //stdin
	arrIO[1]=myProxyEntry->get_delegation_err_fd;
	arrIO[2]=myProxyEntry->get_delegation_err_fd; // stderr

	char * myproxy_get_delegation_pgm = param ("MYPROXY_GET_DELEGATION");
	if (!myproxy_get_delegation_pgm) {
		dprintf (D_ALWAYS, "MYPROXY_GET_DELEGATION not defined in config file\n");
		goto error_exit;
	}


	args.GetArgsStringForDisplay(&args_string);
	dprintf (D_ALWAYS, "Calling %s %s\n", myproxy_get_delegation_pgm, args_string.Value());

	pid = daemonCore->Create_Process (
					myproxy_get_delegation_pgm,
					args,
					PRIV_USER_FINAL,
					myproxyGetDelegationReaperId,
					FALSE,
					&myEnv,
					NULL,	// cwd
					NULL,  // process family info
					NULL,  // socket inherit
					arrIO); // in/out/err streams

	free (myproxy_get_delegation_pgm);

	if (pid == FALSE) {
		dprintf (D_ALWAYS, "Failed to run myproxy-get-delegation\n");
		goto error_exit;
	}

	myProxyEntry->get_delegation_pid = pid;

	return TRUE;

 error_exit:
	myProxyEntry->get_delegation_pid=FALSE;

	if (myProxyEntry->get_delegation_err_fd >= 0) {
		close (myProxyEntry->get_delegation_err_fd);
		myProxyEntry->get_delegation_err_fd=-1;
	}

	if (myProxyEntry->get_delegation_err_filename) {
		MSC_SUPPRESS_WARNING_FIXME(6031) // warning: return value of 'unlink' ignored.
		unlink (myProxyEntry->get_delegation_err_filename);// Remove the tempora
		free (myProxyEntry->get_delegation_err_filename);
		myProxyEntry->get_delegation_err_filename=NULL;
	}

	if (myProxyEntry->get_delegation_password_pipe[0] >= 0) {
		close (myProxyEntry->get_delegation_password_pipe[0]);
		myProxyEntry->get_delegation_password_pipe[0]=-1;
	}
	if (myProxyEntry->get_delegation_password_pipe[1] >= 0 ) {
		close (myProxyEntry->get_delegation_password_pipe[1]);
		myProxyEntry->get_delegation_password_pipe[1]=-1;
	}

	return FALSE;
}
Exemple #27
0
/*
  Open a standard file (0, 1, or 2), given its fd number.
*/
void
open_std_file( int fd )
{
	char	*logical_name = NULL;
	char	*physical_name = NULL;
	char	*file_name;
	int	flags;
	int	success;
	int	real_fd;

	/* First, try the new set of remote lookups */

	success = REMOTE_CONDOR_get_std_file_info( fd, logical_name );
	if(success>=0) {
		success = REMOTE_CONDOR_get_file_info_new(logical_name, physical_name);
	}

	/* If either of those fail, fall back to the old way */

	if(success<0) {
		success = REMOTE_CONDOR_std_file_info( fd, logical_name, &real_fd );
		if(success<0) {
			EXCEPT("Couldn't get standard file info!");
		}
		physical_name = (char *)malloc(strlen(logical_name)+7);
		sprintf(physical_name,"local:%s",logical_name);
	}

	if(fd==0) {
		flags = O_RDONLY;
	} else {
		flags = O_CREAT|O_WRONLY|O_TRUNC;
	}

	/* The starter doesn't have the whole elaborate url mechanism. */
	/* So, just strip off the pathname from the end */

	file_name = strrchr(physical_name,':');
	if(file_name) {
		file_name++;
	} else {
		file_name = physical_name;
	}

	/* Check to see if appending is forced */

	if(strstr(physical_name,"append:")) {
		flags = flags | O_APPEND;
		flags = flags & ~O_TRUNC;
	}

	/* Now, really open the file. */

	real_fd = safe_open_wrapper_follow(file_name,flags,0);
	if(real_fd<0) {
		// Some things, like /dev/null, can't be truncated, so
		// try again w/o O_TRUNC. Jim, Todd and Derek 5/26/99
		flags = flags & ~O_TRUNC;
		real_fd = safe_open_wrapper_follow(file_name,flags,0);
	}

	if(real_fd<0) {
		MyString err;
		err.formatstr("Can't open \"%s\": %s", file_name,strerror(errno));
		dprintf(D_ALWAYS,"%s\n",err.Value());
		REMOTE_CONDOR_report_error(const_cast<char *>(err.Value()));
		exit( 4 );
	} else {
		if( real_fd != fd ) {
			dup2( real_fd, fd );
		}
	}
	free( logical_name );
	free( physical_name );
}
Exemple #28
0
ClassAdLog::ClassAdLog(const char *filename,int max_historical_logs_arg) : table(CLASSAD_LOG_HASHTABLE_SIZE, hashFunction)
{
	log_filename_buf = filename;
	active_transaction = NULL;
	m_nondurable_level = 0;

	this->max_historical_logs = max_historical_logs_arg;
	historical_sequence_number = 1;
	m_original_log_birthdate = time(NULL);

	int log_fd = safe_open_wrapper_follow(logFilename(), O_RDWR | O_CREAT | O_LARGEFILE, 0600);
	if (log_fd < 0) {
		EXCEPT("failed to open log %s, errno = %d", logFilename(), errno);
	}

	log_fp = fdopen(log_fd, "r+");
	if (log_fp == NULL) {
		EXCEPT("failed to fdopen log %s, errno = %d", logFilename(), errno);
	}


	// Read all of the log records
	LogRecord		*log_rec;
	unsigned long count = 0;
	bool is_clean = true; // was cleanly closed (until we find out otherwise)
	bool requires_successful_cleaning = false;
	long long next_log_entry_pos = 0;
    long long curr_log_entry_pos = 0;
	while ((log_rec = ReadLogEntry(log_fp, 1+count, InstantiateLogEntry)) != 0) {
        curr_log_entry_pos = next_log_entry_pos;
		next_log_entry_pos = ftell(log_fp);
		count++;
		switch (log_rec->get_op_type()) {
        case CondorLogOp_Error:
            // this is defensive, ought to be caught in InstantiateLogEntry()
            EXCEPT("ERROR: transaction record %lu was bad (byte offset %lld)\n", count, curr_log_entry_pos);
            break;
		case CondorLogOp_BeginTransaction:
			// this file contains transactions, so it must not
			// have been cleanly shut down
			is_clean = false;
			if (active_transaction) {
				dprintf(D_ALWAYS, "Warning: Encountered nested transactions in %s, "
						"log may be bogus...", filename);
			} else {
				active_transaction = new Transaction();
			}
			delete log_rec;
			break;
		case CondorLogOp_EndTransaction:
			if (!active_transaction) {
				dprintf(D_ALWAYS, "Warning: Encountered unmatched end transaction in %s, "
						"log may be bogus...", filename);
			} else {
				active_transaction->Commit(NULL, (void *)&table); // commit in memory only
				delete active_transaction;
				active_transaction = NULL;
			}
			delete log_rec;
			break;
		case CondorLogOp_LogHistoricalSequenceNumber:
			if(count != 1) {
				dprintf(D_ALWAYS, "Warning: Encountered historical sequence number after first log entry (entry number = %ld)\n",count);
			}
			historical_sequence_number = ((LogHistoricalSequenceNumber *)log_rec)->get_historical_sequence_number();
			m_original_log_birthdate = ((LogHistoricalSequenceNumber *)log_rec)->get_timestamp();
			delete log_rec;
			break;
		default:
			if (active_transaction) {
				active_transaction->AppendLog(log_rec);
			} else {
				log_rec->Play((void *)&table);
				delete log_rec;
			}
		}
	}
	long long final_log_entry_pos = ftell(log_fp);
	if( next_log_entry_pos != final_log_entry_pos ) {
		// The log file has a broken line at the end so we _must_
		// _not_ write anything more into this log.
		// (Alternately, we could try to clear out the broken entry
		// and continue writing into this file, but since we are about to
		// rotate the log anyway, we may as well just require the rotation
		// to be successful.  In the case where rotation fails, we will
		// probably soon fail to write to the log file anyway somewhere else.)
		dprintf(D_ALWAYS,"Detected unterminated log entry in ClassAd Log %s."
				" Forcing rotation.\n", logFilename());
		requires_successful_cleaning = true;
	}
	if (active_transaction) {	// abort incomplete transaction
		delete active_transaction;
		active_transaction = NULL;

		if( !requires_successful_cleaning ) {
			// For similar reasons as with broken log entries above,
			// we need to force rotation.
			dprintf(D_ALWAYS,"Detected unterminated transaction in ClassAd Log"
					"%s. Forcing rotation.\n", logFilename());
			requires_successful_cleaning = true;
		}
	}
	if(!count) {
		log_rec = new LogHistoricalSequenceNumber( historical_sequence_number, m_original_log_birthdate );
		if (log_rec->Write(log_fp) < 0) {
			EXCEPT("write to %s failed, errno = %d", logFilename(), errno);
		}
	}
	if( !is_clean || requires_successful_cleaning ) {
		if( !TruncLog() && requires_successful_cleaning ) {
			EXCEPT("Failed to rotate ClassAd log %s.\n", logFilename());
		}
	}
}
Exemple #29
0
bool
ClassAdLog::TruncLog()
{
	MyString	tmp_log_filename;
	int new_log_fd;
	FILE *new_log_fp;

	dprintf(D_ALWAYS,"About to rotate ClassAd log %s\n",logFilename());

	if(!SaveHistoricalLogs()) {
		dprintf(D_ALWAYS,"Skipping log rotation, because saving of historical log failed for %s.\n",logFilename());
		return false;
	}

	tmp_log_filename.sprintf( "%s.tmp", logFilename());
	new_log_fd = safe_open_wrapper_follow(tmp_log_filename.Value(), O_RDWR | O_CREAT | O_LARGEFILE, 0600);
	if (new_log_fd < 0) {
		dprintf(D_ALWAYS, "failed to rotate log: safe_open_wrapper(%s) returns %d\n",
				tmp_log_filename.Value(), new_log_fd);
		return false;
	}

	new_log_fp = fdopen(new_log_fd, "r+");
	if (new_log_fp == NULL) {
		dprintf(D_ALWAYS, "failed to rotate log: fdopen(%s) returns NULL\n",
				tmp_log_filename.Value());
		return false;
	}


	// Now it is time to move courageously into the future.
	historical_sequence_number++;

	LogState(new_log_fp);
	fclose(log_fp);
	log_fp = NULL;
	fclose(new_log_fp);	// avoid sharing violation on move
	if (rotate_file(tmp_log_filename.Value(), logFilename()) < 0) {
		dprintf(D_ALWAYS, "failed to rotate job queue log!\n");

		// Beat a hasty retreat into the past.
		historical_sequence_number--;

		int log_fd = safe_open_wrapper_follow(logFilename(), O_RDWR | O_APPEND | O_LARGEFILE, 0600);
		if (log_fd < 0) {
			EXCEPT("failed to reopen log %s, errno = %d after failing to rotate log.",logFilename(),errno);
		}

		log_fp = fdopen(log_fd, "a+");
		if (log_fp == NULL) {
			EXCEPT("failed to refdopen log %s, errno = %d after failing to rotate log.",logFilename(),errno);
		}

		return false;
	}
	int log_fd = safe_open_wrapper_follow(logFilename(), O_RDWR | O_APPEND | O_LARGEFILE, 0600);
	if (log_fd < 0) {
		EXCEPT( "failed to open log in append mode: "
			"safe_open_wrapper(%s) returns %d\n", logFilename(), log_fd);
	}
	log_fp = fdopen(log_fd, "a+");
	if (log_fp == NULL) {
		close(log_fd);
		EXCEPT("failed to fdopen log in append mode: "
			"fdopen(%s) returns %d\n", logFilename(), log_fd);
	}

	return true;
}
Exemple #30
0
int MyProxyGetDelegationReaper(Service *, int exitPid, int exitStatus)
{
	// Find the right MyProxyEntry
	Proxy *proxy=NULL;
	MyProxyEntry *matched_entry=NULL;
	int found = FALSE;

	// Iterate through each proxy
	ProxiesByFilename.startIterations();
	while ( ProxiesByFilename.iterate( proxy ) != 0 ) {
		// Iterate through all myproxy entries for the proxy
		proxy->myproxy_entries.Rewind();
		while (proxy->myproxy_entries.Next(matched_entry)) {
			if (matched_entry->get_delegation_pid == exitPid) {
				found = TRUE;
				break;
			}
		}
		if (found) {
			break;
		}
	}

 	if (!found) {
		dprintf (D_ALWAYS, "WEIRD! MyProxyManager::GetDelegationReaper unable to find entry for pid %d", exitPid);
		return FALSE;
	}

	if (exitStatus == 0) {
		dprintf (D_ALWAYS, "myproxy-get-delegation for proxy %s exited successfully\n", proxy->proxy_filename);
		close (matched_entry->get_delegation_err_fd);
	} else {
		// This myproxyEntry is no good, move it to the back of the list
		MyProxyEntry * myProxyEntry = NULL;
		proxy->myproxy_entries.Rewind();
		if (proxy->myproxy_entries.Next (myProxyEntry)) {
			proxy->myproxy_entries.DeleteCurrent();
			proxy->myproxy_entries.Append (myProxyEntry);
		}

		// In the case of an error, append the stderr stream of myproxy-get-delegation to log
		close (matched_entry->get_delegation_err_fd);

		char buff[500];
		buff[0]='\0';
		std::string output;
		int fd = safe_open_wrapper_follow(matched_entry->get_delegation_err_filename, O_RDONLY);
		if (fd != -1) {
			int bytes_read;
			do {
				bytes_read = read( fd, buff, 499 );
				if ( bytes_read > 0 ) {
					buff[bytes_read] = '\0';
					output += buff;
				} else if ( bytes_read < 0 ) {
					dprintf( D_ALWAYS, "WEIRD! Cannot read err file %s, "
							 "errno=%d (%s)\n",
							 matched_entry->get_delegation_err_filename,
							 errno, strerror( errno ) );
				}
			} while ( bytes_read > 0 );
			close (fd);
		} else {
			dprintf( D_ALWAYS, "WEIRD! Cannot open err file %s, "
					 "errno=%d (%s)\n",
					 matched_entry->get_delegation_err_filename,
					 errno, strerror( errno ) );
		}

		dprintf (D_ALWAYS, "myproxy-get-delegation for proxy %s, for job (%d.%d) exited with code %d, output (top):\n%s\n",
			proxy->proxy_filename,
			matched_entry->cluster_id,
			matched_entry->proc_id,
			WEXITSTATUS(exitStatus),
			output.c_str());

	}


	// Clean up
	close (matched_entry->get_delegation_password_pipe[0]);
	close (matched_entry->get_delegation_password_pipe[1]);
	matched_entry->get_delegation_password_pipe[0]=-1;
	matched_entry->get_delegation_password_pipe[1]=-1;

	matched_entry->get_delegation_err_fd=-1;
	matched_entry->get_delegation_pid=FALSE;
	MSC_SUPPRESS_WARNING_FIXME(6031) // warning: return value of 'unlink' ignored.
	unlink (matched_entry->get_delegation_err_filename);// Remove the temporary file
	free (matched_entry->get_delegation_err_filename);
	matched_entry->get_delegation_err_filename=NULL;

   return TRUE;
}