2016-06-15 21 views
0

OpenWrtディストリビューションを組み込みOSとして使用する デバイスでlinuxコマンドを実行するのに、system関数を使用します。cシステム()関数がセグメンテーションフォルトを引き起こします

intシステム(const char *コマンド);

get_file_info()が2回呼び出されたときに、私のプログラムは

int check_file_dir(char *name) 
{ 
    int i = 0; 
    char command[128]; 
    sprintf(command, "ls /etc/config/%s &> /dev/null", name); 
    printf("====> command =%s \n", command); 
    i = system(command); 
    return i; 
} 

void get_file_info() 
{ 
char name[128]; 
struct dirent *d_file; 
struct stat attr; 
char path[128]; 
char s_now[sizeof "AAAA-MM-JJTHH:MM:SS.000Z"]; 

    if ((dir = opendir ("/etc/config/")) != NULL) 
    { 
     while ((d_file = readdir (dir)) != NULL) 
     { 
      if(d_file->d_name[0] == '.') 
       continue; 
      sprintf(path, "/etc/config/%s", d_file->d_name); 
      stat(path, &attr); 
      strftime(s_now, sizeof s_now, "%Y-%m-%dT%H:%M:%S.000Z", localtime(&attr.st_mtime)); 
     } 
    } 
    closedir (dir); 
    int j; 
    for (j = 0; j< FILE_NUMBER; j++) 
    { 
     sprintf(name, "/etc/config/file%d", j); 
     if(check_file_dir(name) !=0) 
      printf("file doesn't exist \n"); 
    } 
} 


void main() 
{ 
get_file_info(); 
get_file_info(); 
} 

以下のような問題がsystem機能による原因であります!

システムの重大な障害を回避するための予防措置はありますか?

+0

おそらく、バッファサイズが不十分です。 'char command []'にさらにバイトを与えるようにしてください。 –

+0

私は既にそれを512にbufのサイズを増やしますが、同じ問題です –

+0

http://stackoverflow.com/questions/19913446/why-should-the-system-function-be-avoided-in-c-and- c –

答えて

0

このリンクによると、私はあなたの問題を再現することはできませんのでごコードはコンパイルされません。これは、変更の注釈を持つあなたのプログラムの作業バージョンです(とtestdir/etc/configを置き換える):

#include <limits.h> /* for PATH_MAX and LINE_MAX */ 
#include <stdio.h> /* header include was missing */ 
#include <stdlib.h> /* header include was missing */ 
#include <time.h> /* header include was missing */ 

#include <dirent.h> /* header include was missing */ 
#include <sys/stat.h> /* header include was missing */ 

int check_file_dir(char *name) 
{ 
    int i = 0; 
    char command[LINE_MAX]; /* standard max command line length */ 
    /* path was wrong below */ 
    sprintf(command, "ls %s &> /dev/null", name); 
    printf("====> command =%s \n", command); 
    i = system(command); 
    return i; 
} 

void get_file_info() 
{ 
    char name[PATH_MAX]; /* standard max path length */ 
    struct dirent *d_file; 
    struct stat attr; 
    char path[PATH_MAX]; /* standard max path length */ 
    char s_now[sizeof "AAAA-MM-JJTHH:MM:SS.000Z"]; 
    DIR *dir;   /* declaration was missing */ 
    int FILE_NUMBER = 0; /* declaration was missing */ 

    if ((dir = opendir("testdir")) == NULL) 
     return; /* no point to go on */ 

    while ((d_file = readdir(dir)) != NULL) { 
     if (d_file->d_name[0] == '.') 
      continue; 
     sprintf(path, "testdir/%s", d_file->d_name); 
     stat(path, &attr); 
     strftime(s_now, sizeof s_now, "%Y-%m-%dT%H:%M:%S.000Z", 
       localtime(&attr.st_mtime)); /* not used? */ 
     ++FILE_NUMBER; /* assuming this is what you meant to do */ 
    } 
    closedir(dir); /* was called even if !opendir() */ 

    int j; 
    for (j = 0; j < FILE_NUMBER; j++) { 
     sprintf(name, "testdir/file%d", j); 
     if (check_file_dir(name) != 0) 
      printf("file doesn't exist \n"); 
    } 
} 

int main(void) /* int and (void) */ 
{ 
    get_file_info(); 
    get_file_info(); 
    return 0; /* was missing */ 
} 
関連する問題