We know that Linux is open source, its own powerful commands are open source, that is, we can obtain the source code of these commands and study it. So how exactly did you get the source code for the system command?
The source code of the command is a package unit, placed in a software package source, in order to download a command of the sources, it is necessary to the command of the software package to download the source. The source code of the command is in the relevant directory of the downloaded source code, usually the SRC directory, the corresponding main file name is CMD.C, where CMD is a specific command, such as the main program file for the LS command is LS.C.
Because the source code is not getting the same way on different Linux distributions, the following are examples of common Linux distributions such as Ubuntu and CentOS.
1. How to obtain system command source code in Ubuntu
Take the source code of the LS command under 32-bit Ubuntu14.04, for example, you can download the source code using the following method: 1) Use the which command to see the location of the LS command, as follows: Which LS # output:/bin/ls
2) Use the dpkg command to query the package that the LS command belongs to, as follows: Dpkg-s/bin/ls # output: coreutils:/bin/ls
3) Use the Apt-get command to download the source code of the package where the LS command is located, as follows: Apt-get source coreutils
Note: 1) When using dpkg, if an error occurs, you can use the sudo command to execute it as root. 2) Save the source code downloaded using Apt-get source to the current directory: In addition, when we know the order belongs to the package, you can also go to the appropriate official website to download.
2. How to obtain system command source code in CentOS
Take the source code of the LS command under 32-bit CentOS6.6, for example, you can download the source code by using the following method: 1) Configure source code source in CentOS, the source configuration file is:/etc/yum.repos.d/ Centos-base.repo, the root permission is required to modify the file.
Open the file to view the contents of the file, on my machine, the contents of the file are as follows:
# centos-base.repo## The mirror system uses the connecting IP address of the client and the# update status of each mirror To pick mirrors that is updated to and# geographically close to the client. You should use this for CentOS updates# unless is manually picking other mirrors.## If the mirrorlist= does For your, as a fall back you can try the # remarked out Baseurl= line instead.##[base]name=centos-$releasever-basemirrorl ist=http://mirrorlist.centos.org/?release= $releasever &arch= $basearch &repo=os#baseurl=http:// mirror.centos.org/centos/$releasever/os/$basearch/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/ rpm-gpg-key-centos-6#released updates [updates]name=centos-$releasever-updatesmirrorlist=http:// mirrorlist.centos.org/?release= $releasever &arch= $basearch &repo=updates#baseurl=http:// mirror.centos.org/centos/$releasever/updates/$basearch/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/ Rpm-gpg-key-centos-6#additional packages that could be useful[extras]name=centoS-$releasever-extrasmirrorlist=http://mirrorlist.centos.org/?release= $releasever &arch= $basearch &repo= extras#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/gpgcheck=1gpgkey=file:///etc/pki/ Rpm-gpg/rpm-gpg-key-centos-6#additional packages that extend functionality of existing packages[centosplus]name= centos-$releasever-plusmirrorlist=http://mirrorlist.centos.org/?release= $releasever &arch= $basearch & repo=centosplus#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/gpgcheck=1enabled= 0gpgkey=file:///etc/pki/rpm-gpg/rpm-gpg-key-centos-6#contrib-packages by CentOS users[contrib]name=centos-$ releasever-contribmirrorlist=http://mirrorlist.centos.org/?release= $releasever &arch= $basearch &repo= contrib#baseurl=http://mirror.centos.org/centos/$releasever/contrib/$basearch/gpgcheck=1enabled=0gpgkey=file:/ Etc/pki/rpm-gpg/rpm-gpg-key-centos-6
There are two variables in the file: Releasever and Basearch, respectively, for release version and branch, such as my machine, these two values are 6 and i386 respectively. Note that Releasever is not subdivided into sub-versions, i.e. not 6.6.
From the contents of this document can be known, the document has a total of 5 sections, that is, I have 5 of the software source of the machine, respectively, base, updates, extras, centosplus and contrib. So you only need to specify the source of the source code for the following 5 software sources. At the end of the file, add the following content:
[base-src]name=centos-$releasever-base srcbaseurl=http://vault.centos.org/6.6/os/source/gpgcheck=1gpgkey=file:/ etc/pki/rpm-gpg/rpm-gpg-key-centos-6[updates-src]name=centos-$releasever-updates src-centosbaseurl=http:// VAULT.CENTOS.ORG/6.6/UPDATES/SOURCE/GPGCHECK=1GPGKEY=FILE:///ETC/PKI/RPM-GPG/RPM-GPG-KEY-CENTOS-6[EXTRAS-SRC] name=centos-$releasever- Extras src-centosbaseurl=http://vault.centos.org/6.6/extras/source/gpgcheck= 1gpgkey=file:///etc/pki/rpm-gpg/rpm-gpg-key-centos-6[centosplus-src]name=centos-$releasever-plus src-centosbaseurl=http://vault.centos.org/6.6/centosplus/source/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/ rpm-gpg-key-centos-6[contrib-src]name=centos-$releasever-contrib src-centosbaseurl=http://vault.centos.org/6.6/ Contrib/soruce/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/rpm-gpg-key-centos-6
Because different versions are set differently, the settings can be summarized as follows: (1) The Gpgkey is the same as the gpgkey of the section already in the file (2) to confirm the existence and condition of the file, you can open the Web site http://vault.centos.org/ To see the specific situation, and then according to the specific situation to fill out (3) Set BaseURL need to set according to their own version of the system, such as my machine for CentOS-6.6, then in the settings, the relevant parameters are set to 6.6, that is http://vault.centos.org/ The next sub-path is 6.6.
The purpose of this file is to provide a source for the download of Yum and its related work, such as the use ofYumdownloader (see below) when downloading the source code, the following actions occur: (1) Download the $baseurl/repodata/repomd.xml file if the Repomd.xml file is newer than the system, update the corresponding repomd.xml (2) in the system to download $baseurl/spackages/ The corresponding source code in the RPM package.
2) Use the which command to view the location of the LS command, as follows: which ls# output:/bin/ls
3) Use the RPM command to query the package that the LS command belongs to, as follows: Rpm-qf/bin/ls # output: coreutils-8.4-37.el6.i686
4) Yumdownloader is a program that downloads rpm from the RPM source and can be usedYumdownloadercommand to install the source code, as follows:yumdownloader--source coreutils-8.4-37.el6.i686
Note: (1) ifYumdownloadercommand is not installed, you can install it using this command: Yum install yum-utils. (2) Download the file in the current directory, you can use the parameter--destdir to specify which directory to download.
5) UseYumdownloader downloaded to an installation package file rpm, you can use the following command to query the tar file in RPM:RPM-QPL coreutils-8.4-37.el6.src.rpm | grep tar# output: Coreutils-8.4.tar.xz
6) Use the Rpm2cpio command to convert the RPM package file to an cpio archive file, and then use the Cpio command to extract an archive from the Cpio archive file, as follows:Rpm2cpio coreutils-8.4-37.el6.src.rpm | Cpio-idv coreutils-8.4.tar.xz# Output:# COREUTILS-8.4.TAR.XZ# 9561 Blocks
The man of Cpio is interpreted as:cpio copies files into an archive-I: Equivalent to--extract, the meaning of extraction-D: Equivalent to--make-directories, create a load directory when needed-V: Lists the file's processing process
7) Extract the COREUTILS-8.4.TAR.XZ compressed package using the TAR command, as follows:tar-xvf./coreutils-8.4.tar.xz
at this point, the command's source code gets completed.
How to obtain command source code of Linux system