Init instance of Android

Source: Internet
Author: User

This period of time is fine, mainly is the supervisor too rubbish, does not have the reasonable arrangement subordinate, causes me to be very busy, just, I use this time can study under the Android.

Today I looked at the Android init this part, I myself want to be able to write a daemon in the Andorid after the start of the run, so that a more profound understanding of the Android init knowledge, but the book is not impressed.

Summed up below, roughly divided into two steps to complete:

1) write the daemon related code, write android.mk, compile Android code, generate the daemon executable file.

2) Modify the content of init.rc and invoke the daemon in init.rc.

The first step:

The daemon is ready to write a 60s record of the process of writing files, this code is mainly copied from other places on the Internet, because this is mainly to explain how to invoke the executable file in init, the code is not very concerned about.

/*

* Init_daemon.cpp

*/

#include <unistd.h>
#include <signal.h>
#include <sys/param.h>
#include <sys/ Types.h>
#include <sys/stat.h>
#include <stdlib.h>

void Init_daemon (void)
{
   int pid;
   int i;
   if (Pid=fork ())
& nbsp      exit (0);        //is the parent process, ending the parent process
   else if (pid< 0)
       exit (1);        //fork failed, exit
   //is the first child process, background continues execution
   setsid (),         & nbsp The first child process becomes the new session leader and process leader
   //and detaches from the control terminal
   if (Pid=fork ())
       exit (0 );        //is the first child process, ending the first child process
   else if (pid< 0)
       exit (1); &N Bsp      //fork failed, exit
   //is the second child process, continue
   //second child process is no longer a session leader
   for (i=0; i< nofile;++i)  //Close Open file descriptor
       close (i);

ChDir ("/"); Change the working directory to/
Umask (0); Resetting file Creation Masks
Return
}


/*

* Mark_log.cpp

*/

#include <stdio.h>
#include <time.h>

void Init_daemon (void);//Daemon initialization function

int main (int nargs,char *args)
{
FILE *FP;
time_t T;
Init_daemon ();//Initialize to Daemon

while (1)//report running status to Test.log every minute
{
Sleep (60);//sleeping for one minute
if ((Fp=fopen ("/test.log", "a")) >=0) {
T=time (0);
fprintf (FP, "Im here at%sn", Asctime (LocalTime (&t)));
Fclose (FP);
}
}
return 0;
}


/*

* ANDROID.MK

*/

Local_path:= $ (call My-dir)
Include $ (clear_vars)

local_src_files:= \
Init_deamon.cpp \
Mark_log.cpp

local_module:= Marklog

Include $ (build_executable)

The above three files are placed under the Marklog folder under the Framework folder (Marklog is the new folder).


Step Two: Modify Init.rc

Added at the end of the init.rc

Service Marklog/system/bin/marklog
Class Core
Disabled
OneShot

Indicates that Marklog is a service, and then starts the service in the appropriate place, which I added in the Post-fs-data section

"Start Marklog"

Of course, there are many ways to modify init.rc, not just above.


Then compile Android, generate kernel and rootfs, re-burn kernel and rootfs,android up Ps-ef can see the marklog process, and every minute will be recorded in Test.log information.




This article is from "Meat House" blog, please make sure to keep this source http://4895268.blog.51cto.com/4885268/1410903

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.