0

私のプログラムは基本的に、コマンドライン引数を使用して実行可能ファイルを実行します。子プロセスがフォークされ、子プロセスの出力がファイル「filename」に取り込まれます。

問題は、ファイルが作成され、データが書き込まれるが、rootユーザーのみが開くことができることです。プログラムを呼び出したユーザーがファイルを読み取れるようにするにはどうすればよいですか。

コードは次のとおりです。

    #include<stdio.h>
    #include<string.h>      //strcpy() used
    #include<malloc.h>      //malloc() used
    #include<unistd.h>      //fork() used
    #include<stdlib.h>      //exit() function used
    #include<sys/wait.h>    //waitpid() used
    #include<fcntl.h>

    int main(int argc, char **argv)
    {
char *command;
char input[256];
char **args=NULL;
char *arg;
int count=0;
char *binary;
pid_t pid;
int fdw;

printf("Enter the name of the executable(with full path)");
fgets(input,256,stdin);

command = malloc(strlen(input));
strncpy(command,input,strlen(input)-1);

binary=strtok(command," ");
args=malloc(sizeof(char*));

args[0]=malloc(strlen(binary)+1);
strcpy(args[0],binary);

while ((arg=strtok(NULL," "))!=NULL)
{
    if ( count%10 == 0) args=realloc(args,sizeof(char*)*10);
    count++;
    args[count]=malloc(strlen(arg));
    strcpy(args[count],arg);
}
args[++count]=NULL;

if ((fdw=open("filename",O_WRONLY|O_EXCL|O_CREAT|0700)) == -1)
    perror("Error making file");
close(1);
dup(fdw);

if ((pid = fork()) == -1)
{
    perror("Error forking...\n");
    exit(1);
}
if (pid == 0)   execvp(args[0],&args[0]);
else
{
    int status;
    waitpid(-1, &status, 0);
}
return 0;
}
4

1 に答える 1

3

開くためにマンページを読み直してください。ファイルモード引数を正しく渡していないため、プロセスでフラグが台無しになっています。

于 2010-02-02T20:08:51.693 に答える