Pages

2011-11-09

In general, those <linux/*> headers are not intended for direct inclusion by apps. If they're needed, one of the standard headers will generally include them, or else will just duplicate the info from them, perhaps in a slightly different way.
But, glibc and the rest of user-space is working off those standard headers, so those are the ones you want to be using.
The <linux/*> ones are typically straight out of the kernel source, and often aren't appropriate for user-space.
                    -- referenced here.

So <netinet/tcp.h> <netinet/udp.h> <netinet/icmp.h> are what we want.

If on x86 or x86_64, little-endian will set to macro __BIT_ORDER in <endian.h>.
In <netinet/tcp.h>:

 # else /* !__FAVOR_BSD */
struct tcphdr
  {
    u_int16_t source;
    u_int16_t dest;
    u_int32_t seq;
    u_int32_t ack_seq;
#  if __BYTE_ORDER == __LITTLE_ENDIAN
    u_int16_t res1:4;
    u_int16_t doff:4;
    u_int16_t fin:1;
    u_int16_t syn:1;
    u_int16_t rst:1;
    u_int16_t psh:1;
    u_int16_t ack:1;
    u_int16_t urg:1;
    u_int16_t res2:2;
#  elif __BYTE_ORDER == __BIG_ENDIAN
    u_int16_t doff:4;
    u_int16_t res1:4;
    u_int16_t res2:2;
    u_int16_t urg:1;
    u_int16_t ack:1;
    u_int16_t psh:1;
    u_int16_t rst:1;
    u_int16_t syn:1;
    u_int16_t fin:1;
#  else
#   error "Adjust your  defines"
#  endif
    u_int16_t window;
    u_int16_t check;
    u_int16_t urg_ptr;
};
# endif /* __FAVOR_BSD */
I've tested and checked that __BYTE_ORDER == __LITTLE_ENDIAN.

_FILE_OFFSET_BITS

Be careful when using _FILE_OFFSET_BITS=64 to compile a program that calls a library or a library if any of the interfaces uses off_t. With _FILE_OFFSET_BITS=64 glibc will change the type of off_t to off64_t. You can either change the interface to always use off64_t, use a different function if _FILE_OFFSET_BITS=64 is used (like glibc does). Otherwise take care that both library and program have the same _FILE_OFFSET_BITS setting. Note that glibc is aware of the _FILE_OFFSET_BITS setting, there's no problem with it but there might be problems with other libraries.

@淘宝雕梁:nginx中定义了_FILE_OFFSET_BITS=64, 这个宏会导致off_t为8个字节(32位系统),而这个宏并没有通过gcc的编译参数传递进来,只是定义在ngx_linux_config.h的开头。于是如果编写模块,在模块的代码中必须把nginx的头文件包含在最上面,否则会导致在模块的代码中off_t的大小和在nginx中的大小不一致。

2011-11-06

Hotplug cpu

There's a way to make specified cpus online -- maxcpus=N as an initialization argument. The shortcoming is obvious that every time have to reboot.

We can change dynamically if the current kernel has CONFIG_HOTPLUG_CPU set. After that:
% ll /sys/devices/system/cpu/cpu1
total 0
 4498 drwxr-xr-x 5 root root    0 Nov  6 17:38 cache
10292 drwxr-xr-x 2 root root    0 Nov  6 16:29 cpufreq
10185 drwxr-xr-x 5 root root    0 Nov  6 17:38 cpuidle
  201 -r-------- 1 root root 4.0K Nov  6 17:38 crash_notes
  200 -rw-r--r-- 1 root root 4.0K Nov  6 17:38 online
 7337 drwxr-xr-x 2 root root    0 Nov  6 17:38 topology
$ su -c 'echo 0 > /sys/devices/system/cpu/cpu1/online'
to shutdown cpu1.

There must be at least one cpu, so that cpu0 has no online
% ll /sys/devices/system/cpu/cpu0
total 0
 4467 drwxr-xr-x 5 root root    0 Nov  6 17:43 cache
10276 drwxr-xr-x 2 root root    0 Nov  6 16:29 cpufreq
10140 drwxr-xr-x 5 root root    0 Nov  6 17:43 cpuidle
  198 -r-------- 1 root root 4.0K Nov  6 17:43 crash_notes
 7330 drwxr-xr-x 2 root root    0 Nov  6 17:43 topology

Simple calculations in vim

From here.
In insert mode, <C-r>= to input calculations.
But only + - * / supported. And result auto converted to decimal when meeting hex.
Useful for hex to decimal.

Exchange capslock and left-ctrl using xmodmap

remove Lock = Caps_Lock
remove Control = Control_L
keysym Caps_Lock = Control_L
keysym Control_L = Caps_Lock
add Lock = Caps_Lock
add Control = Control_L
Save in a normal file, then:
$ xmodmap ./caps-ctrl

Or save as a shell script:
xmodmap -e "remove Lock = Caps_Lock"
xmodmap -e "remove Control = Control_L"
xmodmap -e "keysym Caps_Lock = Control_L"
xmodmap -e "keysym Control_L = Caps_Lock"
xmodmap -e "add Lock = Caps_Lock"
xmodmap -e "add Control = Control_L"

That's pretty cool for *nix working, like shell control & emacs & vim and so forth.