Saving file with sudo without reopening

It happens many time that you open file in vim, make major changes and when you try to save, you realize that you needed to open it as a root.

Well, you do not need to reopen that file and you can save it as sudo with below command:

:w !sudo tee %

Hit enter after typing that in, you will be prompted for a password if required and then you will then get the following message.

Press ENTER or type command to continue

Press ENTER and assuming you actually made some changes, you will then get the following message.

Press ENTER or type command to continue
W12: Warning: File "/etc/vim/vimrc" has changed and the buffer was changed in Vim as well
See ":help W12" for more info.
[O]K, (L)oad File:

Press L and quit the file.

That’s all!

Store and Restore Sessions in VIM

Below commands once added in vimrc creates a session with F2 and restore using F3:

map <F2> :mksession! ~/vim_session <cr> " Quick write session with F2
map <F3> :source ~/vim_session <cr> " And load session with F3

You can make any number of sessions using “mksession” command and restore using “source” command from vim editor also.

Ask for confirmation before running bash command

Sometimes we need to confirm or give warning before running any command.

In my case, I need to check JAVA version before building android source.
Android L needs to be built with Java 1.7 and Android KK needs to be built with Java 1.6. So it is required to change JAVA version while switching between Android K and L.

If we forget to change JAVA version, it tries to build source with improper version consuming lots of time (as java version changes, it tries to rebuild all the things) and results in failure at some point.

To avoid this, I updated bash script so that before build command it checks for JAVA version and asks user if it is proper or not. Upon confirmation from user it proceeds further.

Below is script I wrote and included in bashrc using “source” command at end of bashrc.


#!/bin/bash

function jdkmsg {
echo -e "\e[38;5;82m"
echo "########################################################################"
echo -e "\tHope You are using correct JDK version\n"
echo -e "JAVA_HOME = $JAVA_HOME"
java -version
javac -version
echo -e "You can change java and javac versions using below commands:"
echo -e "sudo update-alternatives --config java and"
echo -e "sudo update-alternatives --config javac"
echo "########################################################################"
echo -e "\e[39;5;82m"
}

debug_trap () {
	if [[ "$BASH_COMMAND" == *"build.sh"* ]]
	then
		jdkmsg
		echo "Allow?"
		select choice in yes no
		do
			if [ "$choice" = "yes" ]
				then break
			elif [ "$choice" = "no" ]
			then return 1
			fi
		done
	fi
}

shopt -s extdebug
trap debug_trap DEBUG

Git send email

My git send-email config:

git config sendemail.smtpuser 
git config sendemail.smtpserver smtp.googlemail.com
git config sendemail.smtpencryption tls
git config sendemail.smtpserverport 587
git config sendemail.suppresscc self
git send-email --to <to_list> --in-reply-to="<message_id_of_message1>" --thread *.patch

This will generate patch in below way:
– Message 1
.           – Patch 1
.                       – Patch 2

This is how I prepared cover page followed by two patches:

git format-patch -2 --subject-prefix="PATCH v2" --cover-letter --thread=shallow
  • Here –cover-later would create a cover page. We need to modify subject and content.
  • The –thread=shallow flag specifies that all patches will be In-Reply-To your cover letter. Hence all patches would be in reply to cover page by default.
  • –subject-prefix=”PATCH v2″ would use given prefix in patch file.

This is how I sent this patches:

git send-email –to <to_id> –cc <cc_id> <patches>

The patch follows below sequence

  • Cover Page
    • Patch 1
    • Patch 2

References:
https://burzalodowa.wordpress.com/2013/10/05/how-to-send-patches-with-git-send-email/
http://git-scm.com/docs/git-send-email
example patch

How to check Memory Leak in linux kernel

  • Compile kernel with
    CONFIG_DEBUG_KMEMLEAK=y
    #CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF=y
    CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE=4096
    
  • Mount debugfs (if it is not mounted)
    mount -t debugfs nodev /sys/kernel/debug/
    
  • Check memory leaks
    # echo scan > /sys/kernel/debug/kmemleak
    # cat /sys/kernel/debug/kmemleak
    

Example Trace:

unreferenced object 0xffff88021481d600 (size 16):
comm “swapper/0”, pid 1, jiffies 4294667622 (age 9531.310s)
hex dump (first 16 bytes):
70 6e 70 20 30 30 3a 30 31 00 38 81 ff ff ff ff pnp 00:01.8…..
backtrace:
[<ffffffff811619cf>] create_object+0xff/0x2a0
[<ffffffff8189ba63>] kmemleak_alloc+0x23/0x40
[<ffffffff8115bf37>] kmem_cache_alloc_trace+0xb7/0x160
[<ffffffff81395fe9>] reserve_range+0x59/0xf0
[<ffffffff81396102>] system_pnp_probe+0x82/0x90
[<ffffffff81392447>] pnp_device_probe+0x67/0xd0
[<ffffffff814c2a7f>] driver_probe_device+0x13f/0x2a0
[<ffffffff814c2ea5>] __device_attach_driver+0x85/0xa0
[<ffffffff814c0fa7>] bus_for_each_drv+0x57/0x90
[<ffffffff814c28d0>] __device_attach+0xb0/0xf0
[<ffffffff814c2ece>] device_initial_probe+0xe/0x10
[<ffffffff814c1f85>] bus_probe_device+0x95/0xa0
[<ffffffff814c00ce>] device_add+0x36e/0x570
[<ffffffff814c0469>] device_register+0x19/0x20
[<ffffffff813913d6>] __pnp_add_device+0x96/0xe0
[<ffffffff8139145b>] pnp_add_device+0x3b/0x100

Git useful commands

  • Git reset hard remote branch to specific commit
    # git reset --hard <commit-id>
    # git push -f origin <branch_name>
    
  • Add prefix in patch subject
    # git format-patch --subject-prefix=PREFIX][PATCH
    
      This will generate a patch with subject: [PREFIX] [PATCH]
  • Get list of new added files between two commits
    # git diff --name-only --diff-filter=A <old-commit-id> <new-commit-id>
  • Get list of modified files between two commits
    # git diff --name-only --diff-filter=M <old-commit-id> <new-commit-id>