Esempio n. 1
0
int
System(const char *cmdstr, char *buf, int buflen)
{
	int status;
	int rval = -128;

#ifdef USE_SYS_POPEN
	int cmdfd;
	int nbytes;

	if ( (cmdfd = sys_popen(cmdstr, 1)) >= 0) {
		if (buf) {
			if ((nbytes = read(cmdfd, buf, buflen-1)) >=0) {
				*(buf+nbytes) = '\0';	//	terminate the string
				NETDEBUG(MEXECD, NETLOG_DEBUG4, ("Cmd Output: %s", buf));
			}
			else {
				NETERROR(MEXECD, ("read: %s, for output of - %s\n", strerror(errno),
					cmdstr));
				status = 1;
			}
		}
		status = sys_pclose(cmdfd);
	}
	else {
		NETERROR(MEXECD, ("sys_popen: failed for cmd %s, error: %s\n", cmdstr,
			strerror(errno)));
		status = 1;
	}
#else
	status = system(cmdstr);	
#endif

	if (WIFEXITED(status)) {
		NETDEBUG(MEXECD, NETLOG_DEBUG3, ("normal termination, exit status = %d\n",
			WEXITSTATUS(status)));
		rval = (WEXITSTATUS(status)) - ( ((WEXITSTATUS(status)) & 0x80) ? 0x100 : 0 );
	}
	else if (WIFSIGNALED(status)) {
		NETDEBUG(MEXECD, NETLOG_DEBUG3, ("abnormal termination, signal number = %d%s\n", 
			WTERMSIG(status), WCOREDUMP(status) ? " (core file generated)" : ""));
		rval = -128;		
	}
	else if (WIFSTOPPED(status)) {
		NETDEBUG(MEXECD, NETLOG_DEBUG3, ("child stopped. signal number = %d\n", 
			WSTOPSIG(status)));
		rval = -128;
	}

	return(rval);
}
Esempio n. 2
0
char *file_pload(char *syscmd, size_t *size)
{
    int fd, n;
    char *p;
    pstring buf;
    size_t total;

    fd = sys_popen(syscmd);
    if (fd == -1) {
        return NULL;
    }

    p = NULL;
    total = 0;

    while ((n = read(fd, buf, sizeof(buf))) > 0) {
        p = (char *)SMB_REALLOC(p, total + n + 1);
        if (!p) {
            DEBUG(0,("file_pload: failed to expand buffer!\n"));
            close(fd);
            return NULL;
        }
        memcpy(p+total, buf, n);
        total += n;
    }

    if (p) {
        p[total] = 0;
    }

    /* FIXME: Perhaps ought to check that the command completed
     * successfully (returned 0); if not the data may be
     * truncated. */
    sys_pclose(fd);

    if (size) {
        *size = total;
    }

    return p;
}
Esempio n. 3
0
static SMB_BIG_UINT disk_free(char *path, BOOL small_query, 
                              SMB_BIG_UINT *bsize,SMB_BIG_UINT *dfree,SMB_BIG_UINT *dsize)
{
	int dfree_retval;
	SMB_BIG_UINT dfree_q = 0;
	SMB_BIG_UINT bsize_q = 0;
	SMB_BIG_UINT dsize_q = 0;
	char *dfree_command;

	(*dfree) = (*dsize) = 0;
	(*bsize) = 512;


	/*
	 * If external disk calculation specified, use it.
	 */

	dfree_command = lp_dfree_command();
	if (dfree_command && *dfree_command) {
		pstring line;
		char *p;
		FILE *pp;

		slprintf (line, sizeof(pstring) - 1, "%s %s", dfree_command, path);
		DEBUG (3, ("disk_free: Running command %s\n", line));

		pp = sys_popen(line, "r", False);
		if (pp) {
			fgets(line, sizeof(pstring), pp);
			line[sizeof(pstring)-1] = '\0';
			if (strlen(line) > 0)
				line[strlen(line)-1] = '\0';

			DEBUG (3, ("Read input from dfree, \"%s\"\n", line));

			*dsize = (SMB_BIG_UINT)strtoul(line, &p, 10);
			while (p && *p & isspace(*p))
				p++;
			if (p && *p)
				*dfree = (SMB_BIG_UINT)strtoul(p, &p, 10);
			while (p && *p & isspace(*p))
				p++;
			if (p && *p)
				*bsize = (SMB_BIG_UINT)strtoul(p, NULL, 10);
			else
				*bsize = 1024;
			sys_pclose (pp);
			DEBUG (3, ("Parsed output of dfree, dsize=%u, dfree=%u, bsize=%u\n",
				(unsigned int)*dsize, (unsigned int)*dfree, (unsigned int)*bsize));

			if (!*dsize)
				*dsize = 2048;
			if (!*dfree)
				*dfree = 1024;
		} else {
			DEBUG (0, ("disk_free: sys_popen() failed for command %s. Error was : %s\n",
				line, strerror(errno) ));
			fsusage(path, dfree, dsize);
		}
	} else
		fsusage(path, dfree, dsize);

#if 0
	if (disk_quotas(path, &bsize_q, &dfree_q, &dsize_q)) {
		(*bsize) = bsize_q;
		(*dfree) = MIN(*dfree,dfree_q);
		(*dsize) = MIN(*dsize,dsize_q);
	}
#endif
	/* FIXME : Any reason for this assumption ? */
	if (*bsize < 256) {
		DEBUG(5,("disk_free:Warning: bsize == %d < 256 . Changing to assumed correct bsize = 512\n",(int)*bsize));
		*bsize = 512;
	}

	if ((*dsize)<1) {
		static int done;
		if (!done) {
			DEBUG(0,("WARNING: dfree is broken on this system\n"));
			done=1;
		}
		/* min dsize = 2GB */
		*dsize = 2000*1024*1024/(*bsize);
		*dfree = MAX(1,*dfree);
	}

	disk_norm(small_query,bsize,dfree,dsize);

	if ((*bsize) < 1024) {
		dfree_retval = (*dfree)/(1024/(*bsize));
	} else {
		dfree_retval = ((*bsize)/1024)*(*dfree);
	}

	return(dfree_retval);
}
Esempio n. 4
0
//
//	Function :
//		restart_ispd()
//
//  Arguments       :
//		None
//
//
//	Description :
//		This function checks on ispd configuration and starts
//		the ispd on the local host if it is configured in the
//		server.cfg file.
//
//	Return Values:
//		None
//
void
restart_ispd( void )
{
	char			command[128];
	int				cmdfd;
	pthread_attr_t	thread_attr;
	int32_t			status;

	// Is an ispd configuration defined in the server.cfg ?

	if ( iserver == NULL )
		return;

	if ( iserver->ispd.location.type == CONFIG_LOCATION_NONE)
		return;		// No, simply return - do nothing

	//
	// code to prevent multiple copies of ispd on one host.
	// Uses advisory locking scheme to insure locking
	// ala Stevens - Advanced Unix Programming - Page 372
	//
	// In this case we want to insure we dont call nullproc
	// on a server that already has an ispd running.
	//

	{
		char	pidfile[MAXPATHLEN];
		char	pid_buf[10];
		int		fd_val;
		int		ispd_fd;

		// Make sure ispd is not already running

		sprintf( pidfile, "%s/%s", PIDS_DIRECTORY, ISPD_PID_FILE );

		if ( ( ispd_fd = open( pidfile, O_WRONLY | O_CREAT, 0644 )) < 0 )
		{
			NETERROR(	MDEF,
						("INIT : Error opening pid file, %s\n",
						pidfile ));
		}

		// Is another ispd running?

		if ( write_lock( ispd_fd, 0, SEEK_SET, 0 ) < 0 )
		{
			// Yes, it has the lock

			if ( errno == EACCES || errno == EAGAIN )
			{
				// Send it a HUP to make it restart!!

				sprintf(	command,
							"/bin/pkill -HUP ispd" );

				// Issue command to restart ispd

				NETERROR(	MDEF,
							("INIT : restart_ispd() - restarting ispd\n" ));

				if ( ( cmdfd = sys_popen( command, 0 ) ) >= 0 )
				{
					sys_pclose( cmdfd );
				}
				return;
			}
			else
			{
				NETERROR(	MDEF,
							("INIT : write_lock() error on file, %s\n",
							pidfile ));
				return;
			}
		}

		close( ispd_fd );
	}

	// If the ispd is disabled non of the stuff below needs to be done

	if ( ispd_type == ISPD_TYPE_DISABLED )
		return;		// No, simply return - do nothing

	//
	// Startup a thread as an RPC server for the ISPD_PROG program number
	// and GIS_VERS version number. The client of the service is the
	// local ispd daemon. The service will be called when ever the status
	// of the box changes. A callback routine gis_callback() local to this
	// c file is registered to handle data passed to it. It is defined below.
	//

	pthread_attr_init( &thread_attr );
	pthread_attr_setdetachstate( &thread_attr, PTHREAD_CREATE_DETACHED );
	pthread_attr_setscope( &thread_attr, PTHREAD_SCOPE_SYSTEM );

	if ( (status = pthread_create(	&gis_rpcsvc_thread,
							&thread_attr,
							gis_rpcsvc_init,
							(void*) gis_callback ) ) != 0 )
	{
		NETERROR(	MDEF,
					("start_ispd() : error failed to start "
						"gis_rpcsvc_init() - status %d\n",
					status ));
	}

	// Build command to ping nullproc of local rpc service ispd
	// to bring up local ispd

	ping_local_ispd();

	return;
}