Re: [PATCH 1/5] Prepare run_ext_program() for getting stdout to the caller

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Sorry for the delay,

On Wed, 01 Sep 2010 17:45:51 -0700
Chandra Seetharaman <sekharan@xxxxxxxxxx> wrote:

>  int run_ext_program(const char *cmd,
> -		    void (*callback)(void *data, int result), void *data)
> +		    void (*callback)(void *data, int result), void *data,
> +		    char *output, int op_len)

We might want to run the program asynchronously? We don't need to
support the feature now but adding the flags argument for it might be
nice?


>  {
>  	pid_t pid;
> -	int fds[2], ret;
> -	struct ext_prog_info *ex;
> -	ssize_t ignored;
> -
> -	ex = zalloc(sizeof(*ex));
> -	if (!ex)
> -		return -ENOMEM;
> +	int fds[2], ret, i;
> +	char *pos, arg[256];
> +	char *argv[sizeof(arg) / 2];
> +
> +	i = 0;
> +	pos = arg;
> +	str_spacecpy(&pos, cmd);
> +	if (strchr(cmd, ' ')) {
> +		while (pos != '\0')
> +			argv[i++] = strsep(&pos, " ");
> +	} else
> +		argv[i++] = arg;
> +	argv[i] =  NULL;
>  
>  	ret = pipe(fds);
> -	if (ret < 0) {
> -		free(ex);
> +	if (ret < 0)
>  		return ret;
> -	}
>  
>  	eprintf("%d %d\n", fds[0], fds[1]);
>  
> -	ex->callback = callback;
> -	ex->data = data;
> -
> -	tgt_event_add(fds[0], EPOLLIN, run_ext_callback, ex);
> -
>  	pid = fork();
>  	if (pid < 0)
>  		return pid;
>  
>  	if (!pid) {
> -		ret = system(cmd);
> -		ignored = write(fds[1], &ret, sizeof(ret));
> -		return 0;
> +		close(1);
> +		dup(fds[1]);
> +		close(fds[0]);
> +		ret = execv(argv[0], argv);
> +		exit(-1);
> +	} else {
> +		close(fds[1]);
> +		waitpid(pid, &i, 0);
> +		ret = read(fds[0], output, op_len);
> +		if (ret < 0)
> +			eprintf("failed to get the output from <%s>.", cmd);
> +
> +		if (callback)
> +			callback(data, WEXITSTATUS(i));
> +		close(fds[0]);
>  	}
>  
> -	close(fds[1]);
> -
>  	return 0;
>  }
>  
> Index: tgt-1.0.8/usr/tgtd.h
> ===================================================================
> --- tgt-1.0.8.orig/usr/tgtd.h
> +++ tgt-1.0.8/usr/tgtd.h
> @@ -335,5 +335,8 @@ struct event_data {
>  };
>  
>  int run_ext_program(const char *cmd,
> -		    void (*callback)(void *data, int result), void *data);
> +		    void (*callback)(void *data, int result), void *data,
> +		    char *output, int op_len);
> +#define call_program(cmd, output, len) \
> +	run_ext_program(cmd, NULL, NULL, output, len)
>  #endif

I think that you can simply rename the function since nobody uses
run_ext_program().
--
To unsubscribe from this list: send the line "unsubscribe stgt" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux SCSI]     [Linux RAID]     [Linux Clusters]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]

  Powered by Linux