本文简单介绍下在Android开机自启动C程序方法,在C程序由于某种原因退出时,并能重新启动,在不使用rild的功能的条件下,如果使用rild功能,则更改一下相应的服务即可,或者自己添加一个简单的服务。
1.把Android源码hardware/ril/rild/rild.c 替换成如下:
#define LOG_TAG "RILD"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <utils/Log.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cutils/properties.h> //property_set()
#define PPPD_SERVICE_NAME "pppd_gprs"
#define FIFO_NAME "/system/bin/Sh_fifo"
#define INDEX_INIT 25 //多少秒数接收不到C程序的响应后重启C程序
#define COUNT_MAX 5 //多少次重启C程序不成功后重启系统
static int Index=INDEX_INIT;
static int count=0;
void *read_thread(void *arg)
{
int fd;
fd=open(FIFO_NAME,O_RDONLY);//打开命名管道
if(fd<0)
{
ALOGW("open %s error!",FIFO_NAME);
}
while(1)
{
int recv=0;
read(fd,&recv,sizeof(recv));
if(recv!=0)
{
Index=recv;
count=0;
}
}
return NULL;
}
void *check_thread(void *arg)
{
while(1)
{
Index--;
if(Index<=0)
{
Index=INDEX_INIT;
count++;
property_set("ctl.stop", PPPD_SERVICE_NAME);//异常停止pppd_gprs
int err = property_set("ctl.start", PPPD_SERVICE_NAME);//设置pppd_gprs属性为启动
if (err < 0) {
ALOGW("Can not restart server!");
}
if(count>COUNT_MAX)
{
system("reboot");
count=0;
}
ALOGD("restart pppd_gprs...\n");
}
// ALOGD("---count:%d Index:%d----\n",count,Index);
sleep(1);
}
return NULL;
}
int main(int argc, char *argv[])
{
pthread_t read_tid;
pthread_t check_tid;
int ret=mkfifo(FIFO_NAME,S_IRUSR|S_IWUSR);//创建命名管道
if(ret!=0)
{
ALOGW("mkfifo %s error!",FIFO_NAME);
}
sleep(15);
int err = property_set("ctl.start", PPPD_SERVICE_NAME);//设置pppd_gprs属性为启动
if (err < 0) {
ALOGW("Can not start server!");
}
else
{
ALOGD("server started...");
}
pthread_create(&read_tid,NULL,read_thread,NULL); //创建读取数据线程
pthread_create(&check_tid,NULL,check_thread,NULL); //创建检测线程
while(1){
sleep(0x00ffffff);
}
return 0;
}
2.把Android源码hardware/ril/rild/Android.mk 替换成如下:
# Copyright 2006 The Android Open Source Project
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= rild.c
LOCAL_SHARED_LIBRARIES := \
libcutils \
libdl
LOCAL_LDLIBS += -lpthread
LOCAL_MODULE:= rild
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)
3.在out/target/product/ut4412/root/init.rc中添加如下代码
on boot (在这条语句下面)
#hslong by add 2015.11.18
chmod 777 /system/etc/hsl_sh
chmod 777 /system/bin/server
...
service ril-daemon /system/bin/rild
class main
# socket rild stream 660 root radio
# socket rild-debug stream 660 radio system
user root
group radio cache inet misc audio sdcard_rw qcom_oncrpc qcom_diag log (在这条语句下面)
#hslong by add 2015.11.18
service pppd_gprs /system/bin/c_test
class main
user root
group radio cache inet misc
disabled
oneshot
4.把编译好的程序放到/system/bin 下面
测试代码如下:
Android.mk
# Copyright 2006 The Android Open Source Project
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS+=-lpthread
LOCAL_LDFLAGS := -ldl
LOCAL_SRC_FILES :=c_test.c
LOCAL_SHARED_LIBRARIES := \
libcutils \
libdl
LOCAL_MODULE:= c_test
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)
c_test.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>
/************************************************************
*函数名称: int main(int argc, char *argv[])
*函数功能: 主函数
*函数参数: 无
*函数返回: 无
***********************************************************/
int main(int argc, char *argv[])
{
int fifo_fd; //声明与自启动代码通信命名管道描述符
pthread_t tid; //服务线程描述符
if((fifo_fd=open(FIFO_NAME,O_WRONLY))<0)
{
perror("open fifo error!\n");
exit(-1);
}
pthread_create(&tid,NULL,thread,NULL); //创建服务线程
pthread_detach(tid); //服务线程分离
while(1){
int data=20;
write(fifo_fd,&data,sizeof(data));
sleep(10);
}
return 0;
}