因测试需要加入开机次数记录,所以记录一下7816开关机是怎么做的
原理很简单,开机时判断一个记录文件是否存在,如果存在,运行一段代码,将记录开机次数文件的值读出来+1
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int main(void)
{
int on_off_fd, count = 0, retval;
on_off_fd = open("/on-off.file", O_RDWR );
if (on_off_fd < 0)
{
perror("on-off.file");
return 0;
}
retval = read(on_off_fd, &count, 4);
count++;
lseek(on_off_fd, SEEK_SET, 0);
retval = write(on_off_fd, &count, 4);
if (retval < 0)
{
perror("write of-off count error");
}
lseek(on_off_fd, SEEK_SET, 0);
retval = read(on_off_fd, &count, 4);
if(retval > 0)
{
printf(" on-off count : %d \n", count);
}
close(on_off_fd);
system("sync");
return 0;
}
# 位置为 etc/init.d/S90aplex
# 将上面那个文件编译出来 名字为 on-off,在开机的时候作如下判断并执行:
if [ -e /on-off.file ]; then
on-off
fi
# 如果要开启关闭开机次数记录,可运行如下脚本
#!/bin/sh
if [ $1 = "start" ]; then
touch /on-off.file
sync
elif [ $1 = "end" ];then
rm /on-off.file -rf
sync
else
echo please input "start" or "end" ;
fi