Showing posts with label linux-unix. Show all posts
Showing posts with label linux-unix. Show all posts

Thursday, March 23, 2017

Android Internals : just what application developers needs to know

It is always good (not a must) to know the internals of a software platform, to be an efficient developer. After having exposed to the internals, many facts will make more sense to during the development, further you will get the clear picture of how each components connect and interact with each other.

Its been a while since the inception of the Android Mobile platform. Since then, there has been higher demand for android developers, not to mention how it made easy for existing Java developers to migrate. Statistics show, most mobile and embedded devices will default to Android soon or later.Learning this platform would be worth the time spend. 

This article, will explain only necessary internal details just enough for user-space developer. Once complete we will have a clear picture of entire android stack starting from the source tree organisation, architecture component interaction, start up sequence, to how an application is launched.

1. Architecture

  

Fig 1 Conceptual Architecture 
1.1 Kernel 

A the bottom most level, the host operating system, is the Linux kernel.
Theoretically it could be any host kernel, practically, however it is a difficult task to achieve. Though, Linux kernel is being used, there were many kernel specific patches those were added to support various user level android policies and drivers. 

Kernel patches (Details are skipped)
  • Wake Locks
  • Binder
  • Klogger                            
  • Anonymous Share Memory
  • Alarm Timers
  • Low Memory Killer
  • Network Security policy   
Other than the patches above, one has to keep in mind, the device manufactures do add custom patches to support their hardware, what is mentioned above are core kernel patches for a android system to work. 

Source Tree : kernel

1.2 Android Runtime 

Consist of the Dalvik virtual machine, which is just like JVM on tbe desktop, that executes the android applications we run, Java runtime, that comes from the Apache Harmony project, system daemons, the init binary  and various supporting binaries. Dalvik virtual machine executes dex files instead of class files. dex files are generated by post processing class files with dx utility.   

Oracle Java    = Java Language    + class file + JVM    + JRE libs
Android Java = Java Language    + dex file   + DVM  + Apache Harmony Project 

Most of the java.* , org.w3c.* junit.* org.json.* in the android API comes from here, including the sql jdbc driver

Source Tree: libcore ,  dalvik , system 

1.3 Libraries 

This is where, the 3rd party native libraries (C/C++) are hosted, they act as a support layer for low level access from the java libraries, it consist of google developed C library called Bionic, it is stripped down library from BSD standard C library. Bionic isn't compliance with POSIX, so native (non java) application and library porting from POSIX system is a hurdle. Though only few libraries have been mentioned in the diagram, it consist of more, starting from libusb to webRTC, kindly see the source tree. 

The Application frame work access these libraries via JNI , one such example to site would be sqlite, we will see a source relation, check the links
   

So, in nutshell, these libraries are accessed by the application frame work via JNI.  

Source Tree:  external

1.4 Application Framework 

Ah, finally we are at the top, the android.* APIs you see in the Android API reference sprouts from here, this is what we mostly use while developing an android applications. Framework don't just consist of developer apis, but, various services and interfaces to communicate with them. Services are not directly accessible via framework , however, via kernel supported mechanism called Binder. In short andorid.*  APIs connected to service / system server via binder.  

Source Tree: framework

1.5 Applications 

There is were we have all the fun, we use the SDKs to access these framework APIs to develop the applications. Most of the application developers play here. There are other species of developers who hack the frameworks , android services and even the kernel subsystem to build custom roms, one such community is the CyanaogenMOD, we should say they are the first class android developers and us to be economy developers. ;-). Most android devices have set of default applications like dialers, mms, browsers you can find its source tree over here.

2. Android Hardware Abstraction Layer 

Kernel does provide HAL , however Google engineers felt it wasn't enough, one reason could be GPL license that the kernel has. So, in order to make device manufacturers implement non GPLed drivers in the user space, this may be a favorable choice. 

Libhardware provides interface for various devices and sensors, the device manufacturers can develop drivers in closed source environment, complying to these interfaces. Few of the interfaces are listed here. 
  • Audio (libaudio.so)                
  • Graphics (gralloc.so, copybit.so, libhgl.so)                
  • Camera (libcamera.so)          
  • GPS (libgps.so)                   
  • Sensors (libsensors.so)       
  • Radio Interface (libril-vendor-version.so)  
All that the manufacturer has to do is to put their library into /system/libs/hw/ 

Case Study : If you like to know how RIL is used for receiving calls, sending sms, receving sms etc take a look into this answer.

3. Android File System 

Android though is Linux based doesn't strictly abide to the FHS , instead has a custom hierarchy

/system                    Immutable directory coming from the original build.
                                It contains native binaries and libraries, framework jar files, 
                                configuration files, standard apps, etc.
/system/app             All the pre-installed apps
/system/bin              Binaries installed on the system (toolbox, vold, surfaceflinger)
/system/etc               Configuration files
/system/fonts            Fonts installed on the system
/system/framework  Jar fi les for the framework
/system/lib               Shared objects for the system libraries
/system/modules      Kernel modules
/system/xbin            External binaries
/data                        changing content of the system are put: 
                                apps, data added by the user, data generated by all the apps at runtime, etc. 

/etc                         Symbolic link to /system/etc
/dev                       Mount point for tmpfs filesystem
/proc                      Mount point for procfs filesystem which provide access to kernel data structures 
/sbin                       Contains various daemons
/root                       As usual the root home directory
/mnt                       Mount point for external and internal filesystems 
                              and some security related filessytem

/sys                        Mount point for sysfs filesystem which has information on 
                              kernel's device objects
/sdcard                   Symbolic link to /mnt/sdcard
/cache                    Used by application to save data temporarily like Google Play Store download etc 
/d                           Symbolic link to /sys/kernel/debug  
/acct                      Mount point for the acct cgroup (control group), 
                              which provides for user accounting.
/vendor                  Symbolic link to /system/vendor 

4. Source Code Organisation Overview

bionic/            is where Android's standard C library is stored
bootable/        contains code samples regarding the boot of an Android device. In this folder, 
                       you will find the protocol used by all Android bootloaders and a recovery image
build/              holds the core components of the build system
cts/                 The Compatibility Test Suite
dalvik/            contains the source code of the Dalvik virtual machine
development/ holds the development tools, debug applications, API samples, etc
device/            contains the device-speci c components
external/         is one of the largest folders in the source code, it contains all the external 
                       projects used in the Android code
frameworks/   holds the source code of the various parts of the framework
hardware/       contains all the hardware abstraction layers
libcore/           is the Java core library
ndk/                is the place where you will nd the Native Development Kit, which allows to build native
                       applications for Android
packages/        contains the standard Android applications
prebuilt/          holds all the prebuilt binaries, most notably the toolchains
sdk/                is where you will nd the Software Development Kit
system/           contains all the basic pieces of the Android system: init, shell, the volume manager, etc.

5. System Startup

Fig 2. start up sequence 

Start up sequence till init is quite straight forward, this is similar usual x86 boot process, finally calling Init , however Android system differs starting from the init sequence. The init itself was written from scratch and not same as the one seen on Linux distribution.

Once the kernel start /init process, it reads the file name /init.rc and /init.(machine-name).rc , these rc script, first of all sets the environment variables,  mounts various file system to its respective mount points, starts various daemons like rild (radio interface layer daemon), adb , vold, netd etc. Init become mother of all the process created subsequently. The next core job of init is to start up the Zygote, which is the mother of all the android applications. 

Lets take a deeper view into Zygote start up,


Init runs the /system/bin/app_process and name it zygote. 
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server

The entry above from ./init.rc that start the app_process to startup the zygote.

As seen in the source line 188 of app_main.cpp



188    if (zygote) {
189        runtime.start("com.android.internal.os.ZygoteInit",
190                startSystemServer ? "start-system-server" : "");
191    }


It start up the java implementation of Zygote , and 'start-system-server' flag is passed as the parameter. This is exactly what we had passed in the entry from the init.rc. The line 189 does many other tasks under the hood, the runtine.start() at line 839 start up up the Dalvik virtual machine, see line 443 of AndroidRuntime.cpp for more information.Ahead of zygote invocation, Dalvik runtime was setup and Dalvik virtual machine was initialized. ZygoteInit::main() get called at line 888 of AndroidRuntime.cpp , which start up the System Server this can be seen at line 544 of ZygoteInit.java, finally it runs runSelectLoopMode() and waits for future request to fork() new zygote instances which specialize into the android application.The System server is started in a two sequence process , first a libary is called to initialize the native services, which can be seen at line 1064 of SystemServer.java , then java based core services are run by ServerThread::run() which is seen at like 1067 of SystemServer.java. Check the source to find out the sequence of the services started, core services line the PowerManager, ActivityManager, Sensor Service etc and other non critical services are started this way. The main being the AcitivityManager, which handles the our Andorid Application.

 

501            /* Request to fork the system server process */
502            pid = Zygote.forkSystemServer(
503                    parsedArgs.uid, parsedArgs.gid,
504                    parsedArgs.gids,
505                    parsedArgs.debugFlags,
506                    null,
507                    parsedArgs.permittedCapabilities,
508                    parsedArgs.effectiveCapabilities);
509        } catch (IllegalArgumentException ex) {
510            throw new RuntimeException(ex);
511        }
512

544            if (argv[1].equals("start-system-server")) {
545                startSystemServer();
546            } else if (!argv[1].equals("")) {
547                throw new RuntimeException(argv[0] + USAGE_STRING);
548            }
Code Snippet Showing How System Server is started

Conclusion



Ah finally we have reached the end,  though this was quick introduction into the android internals, it is my humble opinion that , information above would be sufficient enough to get you started with griping the Android APIs. This introduction will hopefully help you understand the relations and component interaction, which would make android application programming more exiting. Where go from here ? Now you can start reading Application Fundamentals from the android main resource, trust me, many things will make more sense now.

Reference

Friday, November 13, 2015

UNIX (tm) Version History



Well this about history of UNIX,main reason to write this is because of confusion about various versions and how they came to be.What ever given below are not my keystrokes rather from distributed sources like Wikipedia and others. What make this blog different , well i have compiled only necessary information for understanding version confusion.

---
Unix (officially trademarked as UNIX, sometimes also written as Unix with small caps) is a computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Douglas McIlroy, and Joe Ossanna.As of 2007, the owner of the trademark is The Open Group.Only systems fully compliant with and certified to the Single UNIX Specification are qualified to use the trademark; others are called "Unix system-like" or "Unix-like".Commercial start ups, the most notable of which are Solaris, HP-UX, AIX, and Mac OS X.


AT&T made Unix available to universities and commercial firms, as well as the United States government under licenses. The licenses included all source code including the machine-dependent parts of the kernel, which were written in PDP-11 assembly code. Copies of the annotated Unix kernel sources circulated widely in the late 1970s in the form of a much-copied book by John Lions of the University of New South Wales, the Lions' Commentary on UNIX 6th Edition, with Source Code, which led to considerable use of Unix as an educational example.

Version 5 and especially Version 6 led to a plethora of different Unix versions. Version 7 Unix, the last version of Research Unix to be released widely, was released in 1979. Versions 8, 9 and 10 were developed through the 1980s but were only released to a few universities.AT&T licensed UNIX System III, based largely on Version 7, for commercial use, the first version launching in 1982. This also included support for the VAX. AT&T continued to issue licenses for older Unix versions.To end the confusion between all its differing internal versions, AT&T combined them into UNIX System V Release 1.

Since the newer commercial UNIX licensing terms were not as favorable for academic use as the older versions of Unix, the Berkeley researchers continued to develop BSD Unix as an alternative to UNIX System III and V.the most important aspect of the BSD development effort was the addition of TCP/IP network code to the mainstream Unix kernel.

Other companies began to offer commercial versions of the UNIX System for their own mini-computers and workstations. Most of these new Unix flavors were developed from the System V base under a license from AT&T; however, others were based on BSD instead. One of the leading developers of BSD, Bill Joy, went on toco-found Sun Microsystems in 1982 and created SunOS for their workstation computers.


In 1991, a group of BSD developers (Donn Seeley, Mike Karels, Bill Jolitz, and Trent Hein) left the University of California to found Berkeley Software Design, Inc (BSDI). BSDI produced a fully functional commercial version of BSD Unix for the inexpensive and ubiquitous Intel platform, which started a wave of interest in the use of inexpensive hardware for production computing. Shortly after it was founded, Bill Jolitz left BSDI to pursue distribution of 386BSD, the free software ancestor of FreeBSD, OpenBSD, and NetBSD.A free derivative of BSD Unix, 386BSD, was also released in 1992 and led to the NetBSD and FreeBSD projects.

With the 1994 settlement of a lawsuit that UNIX Systems Laboratories brought against the University of California and Berkeley Software Design Inc. (USL v. BSDi), it was clarified that Berkeley had the right to distribute BSD Unix ¿ for free, if it so desired. Since then, BSD Unix has been developed in several different directions, including OpenBSD and DragonFly BSD.

Shortly after UNIX System V Release 4 was produced, AT&T sold all its rights to UNIX to Novell. (Dennis Ritchie likened this to the Biblical story of Esau selling his birthright for the proverbial "mess of pottage".[7]) Novell developed its own version, UnixWare, merging its NetWare with UNIX System V Release 4. Novell tried to use this to battle against Windows NT, but their core markets suffered considerably.

In 1993, Novell decided to transfer the UNIX trademark and certification rights to the X/Open Consortium. In 1996, X/Open merged with OSF, creating the Open Group. Various standards by the Open Group now define what is and what is not a "UNIX" operating system, notably the post-1998 Single UNIX Specification.

Beginning in the late 1980s, an open operating system standardization effort now known as POSIX provided a common baseline for all operating systems; IEEE based POSIX around the common structure of the major competing variants of the Unix system, publishing the first POSIX standard in 1988. In the early 1990s a separate but very similar effort was started by an industry consortium, the Common Open Software Environment (COSE) initiative, which eventually became the Single UNIX Specification administered by The Open Group. Starting in 1998 the Open Group and IEEE started the Austin Group, to provide a common definition of POSIX and the Single UNIX Specification.

In 1983, Richard Stallman announced the GNU project, an ambitious effort to create a free software Unix-like system; "free" in that everyone who received a copy would be free to use, study, modify, and redistribute it. The GNU project's own kernel development project, GNU Hurd, had not produced a working kernel, but in 1992 Linus Torvalds released the Linux kernel as free software under the GNU General Public License. In addition to their use in the Linux operating system, many GNU packages ¿ such as the GNU Compiler Collection (and the rest of the GNU toolchain), the GNU C library and the GNU core utilities ¿ have gone on to play central roles in other free Unix systems as well.

Linux distributions, comprising Linux and large collections of compatible software have become popular both with individual users and in business. Popular distributions include Red Hat Enterprise Linux, Fedora, SUSE Linux Enterprise, openSUSE, Debian GNU/Linux, Ubuntu, Mandriva Linux, Slackware Linux and Gentoo.

Saturday, August 30, 2014

SSH Login without password


you@localhost$ ssh-keygen -t rsa -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/home/username/.ssh/id_rsa): /home/username/.ssh/id_rsa.my_jee_server
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/username/.ssh/id_rsa.my_jee_server
Your public key has been saved in /home/username/.ssh/id_rsa.pub.my_jee_server.pub
Copy keys to server:
$ ssh-copy-id -i /home/username/.ssh/id_rsa.my_jee_server.pub you@server
you@server's password: 
Try login
you@localhost$ ssh you@server
you@server:~$ 
Try copy 
you@locahost$ scp ~/you_servlet.java you@server:/home/username

Friday, July 12, 2013

How to know if Linux swap is working


After installation of Ubuntu 12.04 LTS, its seems like my swap was never, used, most of the time it remained inactive in my system monitor. In order to ensure, if swap is working, one trick is to write a C code that fills up the available RAM and ensure, if swap is being used.

The code below, allocates 10 MB per allocation, do monitor the swap.[1]

 #include <stdlib.h>  
 #include <stdio.h>  
 #include <string.h>  
 int main(int argc, char** argv) {  
   int max = -1, mb = 0;  
   char* buffer;  
   if(argc > 1)   
     max = atoi(argv[1]);  
   else {  
      printf("\nmeater <size in MB> eg: meater 2048\n");  
       exit(0);       
   }  
   printf("\n[INFO] attempting to allocate %d MB @ 10 MB\n", max);  
   while((buffer=malloc(10*1024*1024)) != NULL && mb != max/10) {  
     memset(buffer, 0, 10*1024*1024);  
     mb++;  
     printf("[INFO] allocated %d MB\n", mb*10);  
     sleep(1);  
   }  
   printf("[INFO] meater done eating, yummy..!\n");  
   return 0;  
 }  

Tuesday, November 13, 2012

Let's get Solarized


Ethan Schoonover
Solarized is a sixteen color palette (eight monotones, eight accent colors) designed for use with terminal and gui applications. It has several unique properties. I designed this colorscheme with both precise CIELAB lightness relationships and a refined set of hues based on fixed color wheel relationships. It has been tested extensively in real world use on color calibrated displays (as well as uncalibrated/intentionally miscalibrated displays) and in a variety of lighting conditions.

Now let start Solarizing


Friday, June 3, 2011

Slackware and manual laptop screen off


Sometime you may have large compilation (kernel?) / download, yea and you are bored to do anything else on your laptop, if so you may have puzzled about how to manually switchoff the laptop screen. heh, if you haven't i have ;-).

We make use of 'xset' and 'dpms' to handle this, i have assigned a keyboard shortcut for my KDE.I have also anticipated about the security so i use 'xlock' too.

xlock &  xset dpms 1200 1800 2100 ; sleep 2 ; xset dpms force off; 

Assign the above command sequence to a keyboard short cut, your DE will have such a feature, KDE and xfce do.

Tuesday, May 31, 2011

Slackware 13.37 root and xapplications


In slackware, we have few applications like pkgtool, sbopkg etc that has to run as root, however always using the console to punching in sudo is difficult, one way to over come is to execute them with a desktop link, however since the apps are executed in a auxiliary terminal emulator like xterm/konsole running in non-privileged user's xsession we have an issue, root can't connect to the xserver unless we allow. This might be the error you might see
sudo /usr/bin/firefox
No protocol specified
No protocol specified
Error: cannot open display: :0
Do the following to authorise unrestricted remote connection to x

xhost + 
Consider the security implications of running xhost + read 'man 1 xhost'
Now you can enjoy running
sudo /usr/bin/konsole -e /usr/sbin/sbokpg

Sunday, May 29, 2011

Slackware 13.37 and BroadComm [14e4:4727] wireless Marriage



I have a Broadcom Wireless card, which was obvious enough not to work just like that.
root@noor:/home/althaf# lspci -vnn | grep 14e4
12:00.0 Network controller [0280]: Broadcom Corporation BCM4313
02.11b/g/n Wireless LAN Controller [14e4:4727] (rev 01)

Follow these procedures to make it work

Kernel configuration for (2.6.37 slackware 13.37 default)

Make sure you have enabled
Location: -> Device Drivers
-> Staging drivers
  -> Exclude Staging drivers from being built
      -> Broadcom IEEE802.11n WLAN drivers
          -> Broadcom IEEE802.11n driver style

Download the firmware


I was luck enough to have this card, well the broadcom guys had the mercy to open up their driver code, any way as usual it required firmware to be loaded.

[Firmware link]

We just need *bcm43xx-0.fw* and *bcm43xx_hdr-0.fw*, when downloaded make sure you have the same file name as given, the drivers have hard coded the path and firmware name in them, so it should be the same to work.

Copy them to /lib/firmware/brcm

Reboot

I highly recommend rebooting the system, though theoretically we could load the firmware on the go, however it didn't work for me.Once rebooted you will find wlan0 or similar interface.

References

[Brcm80211]

Wednesday, May 4, 2011

Slackware 13.37 hibernation

Slackware doesn't provide a out of the box configuration to hibernate even though stock kernel is capable.

Few modifications to your lilo configuration could enable hibernation.

To do so edit your /etc/lilo.conf with the following, assuming that your are using the default configuration from slackware.
image = /boot/vmlinuz
root = /dev/sda1
label = Slackware(Res)  
#for resuming /dev/sda6 is the my swap
append = "resume=/dev/sda6" 
read-only
After saving the file re-run lilo to apply the changes.

Saturday, April 11, 2009

Slackware - simplicity is fun

Well as far my exposure to various Linux distros is concerned, Slackware resonated with my frequency. I felt intimidate connection between me a the philosophy of Slackware. 
“ Slack, which generally stands for the sense of freedom, independence, and original thinking that comes when you achieve your personal goals ”.
So the meaning Slackware is obvious , till now revealed that its no easy for a newbie. Though my intention of using is to learn Linux from its raw form.It does provide an environment for learning Linux. Learning Linux means not the kernel alone but the utilities as well.  

Choose Slackware
  •  to learn Linux from scratch.
  •  to tweak each and every settings.
  •  to be a fan of simplicity.
  •  if you have killer instinct to solve issues.
Slackware isn't a difficult to conquer once you have understood the UNIX philosophy. It is the best one I have come across for the purpose of learning Linux .Of course it is still powerful for other purposes. 

For support /join IRC freenode ##slackware channel. 
  

Thursday, April 9, 2009

Spliting Files .. the GNU way

Well here is typical scenario you have two 1GB pen drive and 2GB file to transport.

Q. What would to do?

Split is utility that comes with GNU coreutils .It allows the user to split a large file into small manageable size of MB KB or what ever apt to the situation.


For survival sake we make use if only -b and parameters associated with it.Well here it goes...

My file is of size 1.4 GB i need to split it to a chunk of 700MB

$ split -b 700m myfile myfile-split

Q. What would be the name of second half ?

Well the programmers (guess who wrote it ) are clever ,  myfile-split is the base file name that i gave you can choose any name , when the chunks are made it would create myfile-splitaa,myfile-splitab etc

Q. How to join ?

On UNIX
$ cat myfile-split* > myfile

On Windows
copy /b myfile-split* myfile

Wednesday, April 8, 2009

Encrypting your files

GnuPG GNU privacy guard is pretty good tool for a securing data. It has many powerful features, however this tip is on the stand alone encryption on a particular file with a passcode. This can come in handy if you have to encrypt your private documents. GnuPG can be download from http://www.gnupg.org/download/index.en.html

On Terminal a simple demo explanation follows

To encrypt the file wget-log





To decrypt my wget-log file



Explanation

One of the feature of gpg is to encrypt and decrypt files, from the example above we have seen an option -c which means to encrypt with a symmetric cipher using a passphrase. The default symmetric cipher used is CAST5, but any algorithm may be chosen with the --cipher-algo option. CAST5 is a block cipher algorithm,  there are lots to know about the internals.

The issue of the gpg with the parameter -c and filename it asks you for a secret key or passphrase which when entered is not echoed on the screen. Enter it twice for cross checking, once successfully done wget-log file would be encrypted to wget-log.gpg .


In order to decrypt the file just call gpg with the file name to decrypt and its done only if you give the correct passphrase.

There are other way to do the same thing using gpg ,those are options -e and -d .. well its better to keep things simple for time being .. : )

Next time, if you want to share a private email / document / picture / what not with your spouse or other family members, i hope you got the right tool.