غير قادر على استخدام "execve ()" بنجاح
-
25-09-2019 - |
سؤال
الهدف من البرنامج هو تغطية عملية الطفل الجديدة وتنفيذ عملية تحتوي أيضًا على وسيط سطر الأوامر. إذا أدخلت /bin/ls --help
, ، أحصل على الخطأ:
shadyabhi@shadyabhi-desktop:~/lab/200801076_lab3$ ./a.out
Enter the name of the executable(with full path)/bin/ls --help
Starting the executable as a new child process...
Binary file to be executed: /bin/ls
/bin/ls: unrecognized option '--help
'
Try `/bin/ls --help' for more information.
Status returned by Child process: 2
shadyabhi@shadyabhi-desktop:~/lab/200801076_lab3$
ماذا ستكون الحجة الصحيحة execve()
?
#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
int main(int argc, char **argv)
{
char command[256];
char **args=NULL;
char *arg;
int count=0;
char *binary;
pid_t pid;
printf("Enter the name of the executable(with full path)");
fgets(command,256,stdin);
binary=strtok(command," ");
args=malloc(sizeof(char*)*10);
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 ((pid = fork()) == -1)
{
perror("Error forking...\n");
exit(1);
}
if (pid == 0)
{
printf("Starting the executable as a new child process...\n");
printf("Binary file to be executed: %s\n",binary);
execve(args[0],args,NULL);
}
else
{
int status;
waitpid(-1, &status, 0);
printf("Status returned by Child process: %d\n",WEXITSTATUS(status));
}
return 0;
}
المحلول
يجب أن يكون الإدخال الأول في صفيف ARGS هو اسم البرنامج مرة أخرى. استدعاء الكود الخاص بك /bin/ls
مع --help
كاسم العملية.
نصائح أخرى
يرجى التحقق للتأكد args
لا تتعرض للاشمئزاز من قبل realloc
يتصل. انظر هنا على ذلك بخصوص Realloc
تعديل:أيضا الحلقة تبدو مضحكة .... لقد اتصلت strtok
مثله:
binary=strtok(command," ");
تغيير بنية الحلقة للاستخدام binary
بدلا من ذلك كما هو موضح ...
char *tmpPtr; while (binary != NULL){ if ( count%10 == 0) tmpPtr=realloc(args,sizeof(char)*10); if (tmpPtr != NULL) args = tmpPtr; count++; args[count-1]=malloc(strlen(binary)+1); strcpy(args[count-1],binary); binary = strtok(command, " "); }
واستخدام binary
لنسخ السلسلة ....
أتمنى أن يساعد هذا ، مع أطيب التحيات ، توم.
برنامجك لديه بعض الأخطاء الواضحة. على سبيل المثال ، أعلن char **args=NULL;
وثم args=realloc(args,sizeof(char)*10);
(منذ ل char**
, ، يجب ان تكون تخصيص-ing ل char*
, ، لا؟..).
حيث sizeof(char*)
عادة 4 بينما sizeof(char)
عادةً ما يكون 1 ، ينتهي بك الأمر إلى بعض مشاكل إدارة الذاكرة الخطيرة الموجودة هناك (يمكنك تخصيص أقل مما تستخدمه ، وينتهي بك الأمر إلى الكتابة حيث لا ينبغي عليك). من هناك ، ينهار كل الجحيم ولا يمكنك أن تتوقع أن يكون سلوك البرنامج منطقيًا.
أقترح أن تدير برنامجك من خلال استخدام مثل فالغريند لمعرفة تسرب الذاكرة وتصحيح البرنامج بشكل مناسب. ربما الخاص بك execve
سوف تختفي المشاكل بمجرد تصحيح مشاكل الذاكرة.