Android下开机自启动C程序【转】
来源:互联网 发布:java 遍历ftp文件夹 编辑:程序博客网 时间:2024/06/06 14:06
来自:http://blog.csdn.net/hanbo622/article/details/49909415
本文简单介绍下在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);
- int err = property_set("ctl.start", PPPD_SERVICE_NAME);
- if (err < 0) {
- ALOGW("Can not restart server!");
- }
- if(count>COUNT_MAX)
- {
- system("reboot");
- count=0;
- }
- ALOGD("restart pppd_gprs...\n");
- }
-
- 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);
- 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 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;
- }
0 0