2012-06-06 145 views
4

我可以編譯提供給我的這個程序,但我必須進一步開發。我有一些關於它的問題:如何運行此程序?

#include <sys/types.h> 
#include <signal.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <errno.h> 

#define TIMEOUT (20) 

int main(int argc, char *argv[]) 
{ 
    pid_t pid; 
    if(argc > 1 && strncmp(argv[1], "-help", strlen(argv[1])) == 0) 
    { 
     fprintf(stderr, "Usage: RunSafe Prog [CommandLineArgs]\n\nRunSafe takes as arguments:\nthe program to be run (Prog) and its command line arguments (CommandLineArgs) (if any)\n\nRunSafe will execute Prog with its command line arguments and\nterminate it and any remaining childprocesses after %d seconds\n", TIMEOUT); 
     exit(0); 
    } 

    if((pid = fork()) == 0)  /* Fork off child */ 
    { 
     execvp(argv[1], argv+1); 
     fprintf(stderr,"RunSafe failed to execute: %s\n",argv[1]); 
     perror("Reason"); 
     kill(getppid(),SIGKILL); /* kill waiting parent */ 
     exit(errno);    /* execvp failed, no child - exit immediately */ 
    } 
    else if(pid != -1) 
    { 
     sleep(TIMEOUT); 
     if(kill(0,0) == 0)   /* are there processes left? */ 
    { 
     fprintf(stderr,"\nRunSafe: Attempting to kill remaining (child) processes\n"); 
     kill(0, SIGKILL);  /* send SIGKILL to all child processes */ 
    } 
    } 
    else 
    { 
     fprintf(stderr,"RunSafe failed to fork off child process\n"); 
     perror("Reason"); 
     } 

} 

當我編譯它時,我的警告是什麼意思?

$ gcc -o RunSafe RunSafe.c -lm 
RunSafe.c: In function ‘main’: 
RunSafe.c:30:44: warning: incompatible implicit declaration of built-in function ‘strlen’ [enabled by default] 

爲什麼我不能執行該文件?

$ file RunSafe 
RunSafe: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x0a128c8d71e16bfde4dbc316bdc329e4860a195f, not stripped 
[email protected]:/media/Lexar$ sudo chmod 777 RunSafe 
[email protected]:/media/Lexar$ ./RunSafe 
bash: ./RunSafe: Permission denied 
[email protected]:/media/Lexar$ sudo ./RunSafe 
sudo: ./RunSafe: command not found 

回答

2

首先,您需要#include <string.h>以擺脫該警告。

其次,操作系統可能會阻止您在/media/Lexar文件系統上執行程序,而不管它們的權限位是多少。如果您輸入mount,則可能會看到/media/Lexarnoexec選項。

+0

添加到@ Greg的響應我相信如果'noexec'選項不是mount選項,'/ media/Lexar'可能是一種不同類型的文件系統,可能是FAT或其他。 – g13n

+0

僅僅通過FAT通常不足以阻止執行。 –

+1

那麼,如果操作系統在FAT上,操作系統將不會運行該程序。儘管chmod將會成功,但可執行位將不會被設置。因此shell會抱怨拒絕許可。 – g13n

1

警告:內建函數「strlen的」 [默認啓用]

不兼容的隱式聲明您需要包括#include<string.h>因爲strlen()在其聲明。

嘗試在文件系統中的某個其他位置運行exe,而不是掛載的分區,因爲錯誤表示出於某種原因,您對該掛載的分區沒有權限。

+0

你說得對。現在按照這些說明進行操作。感謝你的回答。 –