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
機能による原因であります!
システムの重大な障害を回避するための予防措置はありますか?
おそらく、バッファサイズが不十分です。 'char command []'にさらにバイトを与えるようにしてください。 –
私は既にそれを512にbufのサイズを増やしますが、同じ問題です –
http://stackoverflow.com/questions/19913446/why-should-the-system-function-be-avoided-in-c-and- c –