2012-06-06 52 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

你说得对。现在按照这些说明进行操作。感谢你的回答。 –