Ake Koomsin

TIL There Is a Tool Named Diffstat

Right now I am working on my thesis which is “Specializing Operating System Kernels using a Scripting Language”. A part of my work is porting the LuaJIT compiler into the FreeBSD x86 kernel.

When I was writing my thesis, I was asked to show modification information in the thesis. They are number of lines added, numbers of lines removed, number of lines changed. I know that diff exists. However, I need a summary of information produced by it.

My Google kung fu led me to diffstat. It is very simple to use:

diff -u /path/to/luajit_kernel/src /path/to/luajit/src | diffstat -f0

The result is shown below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 ./lib_syscall.c                                       |only
 ./lib_util.c                                          |only
 /Users/peace/sources/LuaJIT-2.0.3/src/lauxlib.h       |   13     2 + 11 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lib_aux.c       |   23     6 + 17 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lib_base.c      |   59     20 +    39 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lib_debug.c     |   36     17 +    19 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lib_init.c      |   11     0 + 11 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lib_package.c   |   43     0 + 43 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lib_string.c    |   40     0 + 40 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_alloc.c      |   18     2 + 16 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_asm.c        |   18     0 + 18 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_asm_x86.h    |   46     0 + 46 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_bc.h         |  127     1 + 126 -   0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_ccallback.c  |    4     0 + 4 - 0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_def.h        |    2     0 + 2 - 0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_dispatch.c   |    2     0 + 2 - 0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_dispatch.h   |   19     1 + 18 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_err.c        |   12     0 + 12 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_errmsg.h     |    6     0 + 6 - 0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_ffrecord.c   |  154     0 + 154 -   0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_ir.c         |   25     0 + 25 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_ir.h         |  143     1 + 142 -   0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_ircall.h     |   37     0 + 37 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_lex.c        |    5     0 + 5 - 0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_lib.c        |    7     0 + 7 - 0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_lib.h        |    2     0 + 2 - 0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_load.c       |    6     0 + 6 - 0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_mcode.c      |   59     1 + 58 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_meta.c       |    4     0 + 4 - 0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_obj.h        |   38     3 + 35 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_opt_fold.c   | 1195     551 +   644 -   0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_opt_loop.c   |    2     0 + 2 - 0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_opt_narrow.c |   70     0 + 70 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_parse.c      |   99     3 + 96 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_record.c     |   42     0 + 42 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_snap.c       |    7     0 + 7 - 0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_state.c      |    2     0 + 2 - 0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_str.c        |    2     0 + 2 - 0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_strscan.c    |   91     1 + 90 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_tab.c        |   33     0 + 33 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_trace.c      |    2     1 + 1 - 0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_vm.h         |   33     0 + 33 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_vmevent.c    |    6     0 + 6 - 0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lj_vmmath.c     |   67     0 + 67 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lua.h           |    6     1 + 5 - 0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/luaconf.h       |   30     0 + 30 -    0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/lualib.h        |    6     0 + 6 - 0 !
 /Users/peace/sources/LuaJIT-2.0.3/src/vm_x86.dasc     |  452     34 +    418 -   0 !
 51 files changed, 645 insertions(+), 2459 deletions(-)

SSH to a Computer Inside an Internal Network

There are times that I want to access to my computer in my laboratory remotely. However, it is inside the lab’s internal network. Luckily, There is a gateway that I make a ssh connection to. The simplified scenario is shown below.

1
2
3
|---------|      |-------|      |-----------|
|my laptop|------|gateway|------|my computer|
|---------|      |-------|      |-----------|

To access my computer in the internal network, I can create a tunnel by running this command on my laptop:

1
ssh -L PORT:MY_COMPUTER_INTERNAL_IP:22 gateway -lUSER_TO_LOGIN_ON_THE_GATEWAY -N

This command will create a tunnel that will forward the local port PORT to my computer port 22 (SSH). -N is for not executing a remote command.

After that, I can simply ssh to my computer using the following command:

1
ssh -lUSER_TO_LOGIN_ON_MY_COMPUTER -pPORT localhost

And that’s all.

Remap CAPSLOCK to CTRL on FreeBSD

Assuming that you are using us.iso.kbd keymap, you can remap CAPSLOCK to CTRL by this simple following steps.

First, copy the us.iso.kbd

1
2
# cd /usr/share/syscons/keymaps/
# cp us.iso.kbd us.iso-swap.kbd

Next, modify the permission of us.iso-swap.kbd to be writable.

1
# chmod 644 us.iso-swap.kbd

Then, modify us.iso-swap.kbd. Look at the line with number 058 and change “clock” to “lctrl” like this.

1
2
3
...
058   lctrl  lctrl  lctrl  lctrl  lctrl  lctrl  lctrl  lctrl   O
...

After that, change the permission back to read-only

1
# chmod 444 us.iso-swap.kbd

Finally, add this text to /etc/rc.conf

1
keymap="us.iso-swap"

Reboot and now your CAPLOCKS will be CTRL.

Seeing All Man Pages Available in a Section on FreeBSD

When I am working with a kernel module in FreeBSD, there are times that I want to see all functions available in man 9. I know that apropos is out there but I sometimes don’t know the keyword.

Because of this, I decided to write a simple zsh script to list all available pages in a section, not only in section 9.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function ls_man() {
    if [[ $# -ne 1 ]]; then
        echo "Number of argument must be 1" >&2
        echo "Usage: ls_man [1-9]" >&2
        return 1
    fi

    if [[ $1 == [1-9] ]]; then
        ls /usr/share/man/man$1 | awk '{ split($1, name, ".") } { print name[1] }'
        return 0
    else
        echo "Wrong argument" >&2
        echo "Usage: ls_man [1-9]" >&2
        return 1
    fi
}

Verifing OpenBSD Install Media on OS X

New OpenBSD 5.6 has been released. It is time to give it a try.

In the installation maunal, There is a section “Verifying the OpenBSD Installation Media”. signify command is used for verification.

Verifying the install media
1
signify -C -p /etc/signify/openbsd-56-base.pub -x SHA256.sig cd56.iso

The SHA256.sig and cd56.iso can be obtained from the mirror. However, openbsd-56-base.pub file is not available. In addition, there is no signify in default OS X installation.

To use signify to verify the install media, we can do the following:

1) Install signify from either port or homebrew.

Install signify from homebrew
1
brew install signify-osx

2) Copy the public key from http://www.openbsd.org/56.html and create a file to store the key. It is required that the file must start with “untrusted comment: ”.

56.pub
1
2
untrusted comment: signify public key for OpenBSD 5.6
RWR0EANmo9nqhpPbPUZDIBcRtrVcRwQxZ8UKGWY8Ui4RHi229KFL84wV

3) Verify. In my case, I verify the install56.fs which is for creating bootable USB. In my case, every file is in the same directory.

Verify
1
signify -C -p 56.pub -x SHA256.sig install56.fs

The final result should look like this.

Final result
1
2
Signature Verified
install56.fs: OK