We should forget about small efficiencies — Donald Knuth
9 Sep
前一陣子上班時, Samuel 跑過來問:
“咦? 你們之前寫某個 Server 的時候, 怎麼讓程式自己產生 Core dump 啊?”
“就是用 setrlimit(2) 的啊!”
“那幫我看一下為啥我照著那樣寫不會動..”
沒想到就開始了殘酷的惡夢~ 讓我們花費了不少工夫才發現為甚麼.
在 Linux 上, 預設是不會有 Core dump 的, 而要讓程式產生 Core dump 的方法就是利用 bash built-in 的 ulimit 指令. 我去年這篇 How to enlarge Coredump Size and File Descriptor Limitations 剛好也有寫到.
不過意外常常有, 所以除了讓 System administrator 設 ulimit -c unlimited 之外, 我自己也會在程式裡面利用 setrlimit(2) 這隻 system call, 讓程式在執行時, 能夠不管 administrator 有沒有用 ulimit 設定 Core dump size, 保證一定會產生 Core dump. 在程式 crash 的時候, Core dump 是很重要線索啊! 就像 CSI 一樣, 讓證據會說話.
可是在看完 Samuel 的程式時, 我和他兩個就覺得很奇怪, 應該是會 work 才對, 因為同樣的 code 寫的程式已經在 production server 上跑了一段時間, 應該不會有問題才對, 這時候, 我才猛然想起來, 管機器的 administrator 曾說過有時候 Core dump 不會出現. “God! 該不會就是同一個問題吧?”
和 Samuel 兩個人找完資料的答案, 就是寫這篇文章的動機了. 我們發現, 如果一個程式按照上面的方法都無法產生 Core dump, 那要看看這個程式是否是用了 setuid(2) 這個 System call. 我們發現, 一個 setuid(2) 或 seteuid(2) 過後的程式, 是沒有辦法產生 Core dump 的.
好巧不巧, 通常 Server 為了一些安全性的考量, 也會實作 setuid(2) 或 seteuid(2) 來達到 Running with Least Privilege (相對應 Windows 的指令就是 Run As). 也就是說, setuid(2) 一定是不能略過的. 解決方法有兩種.
第一種是只有這個程式有效. 用的方法就是 prctl(2). 這個方法是可以改 source, 然後 rebuild 的狀況下才能用. Sample code 如下:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/resource.h>
#include <sys/prctl.h>
#define SU_USER "nobody"
int main(void)
{
struct rlimit corelimit;
struct passwd *pw = NULL;
char *cp = NULL;
/* if switch to nobody failed */
if (NULL == (pw = getpwnam(SU_USER)))
{
fprintf(stderr, "Cannot get uid from user(%s). Error: %s\n",
SU_USER, strerror(errno));
return -1;
}
/* try to switch to nobody */
if ((setuid(pw->pw_uid) < 0) || (seteuid(pw->pw_uid) < 0))
{
fprintf(stderr, "Cannot switch to user(%s). Error: %s\n",
SU_USER, strerror(errno));
return -2;
}
/* force to make coredump */
if (prctl(PR_SET_DUMPABLE, 1) < 0)
{
fprintf(stderr, "Cannot enable core dumping. Error: %s\n",
strerror(errno));
return -3;
}
/* set core size to unlimited */
corelimit.rlim_cur = RLIM_INFINITY;
corelimit.rlim_max = RLIM_INFINITY;
if (setrlimit(RLIMIT_CORE, &corelimit) < 0)
{
fprintf(stderr, "Setrlimit failed! Error: %s\n",
strerror(errno));
return -4;
}
/* force to coredump */
*cp = '1';
return 0;
}
這種方法要注意的事情有:
第二種方法是 system-wide 的, 也就是會影響到所有在系統執行的程式. 方法就是 /proc/sys/fs/suid_dumpable. 因為這個和 kernel 的版本有關, 請 man 5 proc 比較保險. 這是在無法動 source code, 而且原程式沒有用 prctl(2) 情況下的殺手鐧.
要注意的事情和第一種方法一樣, 把 /proc/sys/fs/suid_dumpable 設成 1 或 2 的差別, 是會決定 core dump file 的擁有者. 如果設成 1, 就會和第一種方法一樣是被 switch 的 owner (本例是 nobody). 如果設 2 就一定是 root, 但是這個似乎要 kernel 2.6.13 以上才有 support? 我不是很確定.
希望這篇文章給大家當作個參考囉!
PS: 這篇竟然寫了快二個周末, 真是夠久的~
Popularity: 68% [?]
5 Jul
When we write a network server program, I think lots of system calls have their own explicit parameters like socket(), bind(), accept(). But it’s very interesting when we use this system call listen(). Let’s see its prototype:
int listen(int sockfd, int backlog);
Yes, it’s very obvious that the first parameter is the socket fd. But, what’s the meaning of backlog number? Some body would tell us like manpage LISTEN(2) says: “The backlog parameter defines the maximum length the queue of pending connections may grow to. If a connection request arrives with the queue full the client may receive an error with an indication of ECONNREFUSED or, if the underlying protocol supports retransmission, the request may be ignored so that retries succeed.”
From a robust server’s perspective, what’s the maximum value it should be assign? At first, I assigned very large number like 1,024 (of course, listen system call still returns successfully). After saw the manpage LISTEN(2) in Linux, I was wrong..
If the socket is of type AF_INET, and the backlog argument is greater than the constant SOMAXCONN (128 in Linux 2.0 & 2.2), it is silently truncated to SOMAXCONN.
It doesn’t mention kernel 2.6. But it’s fine. Let’s investigate into Linux kernel source code.
In Linux kernel 2.6.20.1, we can see the listen system call implementation in net/socket.c line 1306. As it shows, the maximum number of backlog cannot be large than sysctl_somaxconn, which is assigned to SOMAXCONN. Furthermore, SOMAXCONN is defined 128 in include/linux/socket.h line 226.
In my opinion, in Linux 2.0 to 2.6, this means backlog cannot exceed 128 by default, or it would be truncated to SOMAXCONN silently like the manpage says.
How about FreeBSD? We can see the note of manpage LISTEN(2) in FreeBSD 6:
The listen() system call appeared in 4.2BSD. The ability to configure the maximum backlog at run-time, and to use a negative backlog to request the maximum allowable value, was introduced in FreeBSD 2.2.
I’m not very familiar with FreeBSD kernel, but let me try to trace. The start point is to check sys/kern/uipc_syscalls.c of cvstag RELENG_6 in FreeBSD. We can see listen system call will invoke solisten(so, uap->backlog, td). Thus, we go to sys/kern/uipc_socket.c now to see the implementation of solisten(struct socket *so, int backlog, struct thread *td). The same, the maximum value is somaxconn which is assigned to SOMAXCONN by default. Finally, we can see the value is defined in sys/sys/socket.h. The value is the same as Linux — 128.
To put it another way, if you’re writing a server program in either Linux or FreeBSD platform, it’s very appropriate to assign the value of backlog to 128. in FreeBSD, however, you can assign a negative backlog to request the maximum allowable value.
You may ask what’s the value of backlog in popular modern server? Let’s check the source of Apache HTTP Server. As you see in /server/listen.c, ap_listenbacklog is assigned to DEFAULT_LISTENBACKLOG which is defined 511 in /include/mpm_common.h.
Popularity: 57% [?]
31 Jan
According to Darryl Gove’s post yesterday, the UltraSPARC-T1 tuning guide has been updated to include information about the Cool Tools.
Popularity: 62% [?]
28 Sep
Zero copy between ISR, kernel and User, from LKML.
Q:
I would like to allow the transferring of data between ISR’s, kernel and user code, without requiring copying.
I envision allocating buffers in the kernel and then mapping them so that they appear at the same addresses to all code, and never being swapped out of memory.
Is this feasible for all supported Linux architectures and is there existing code that someone could point me towards?
A:
Your better off having application mmap a device, then transfer the data to there. Something like AF_PACKET.
Popularity: 29% [?]
27 Sep
POSIX Asynchronous I/O, from OSNews.
Used judiciously, asynchronous I/O (short for AIO) can provide a significant speed benefit, says David Chisnall. Perhaps enough to help your program overcome the fact that modern processors can really zoom, while hard drives still drag.
Code using AIO on Linux must be linked with -lrt to provide support for the POSIX real-time extensions, which means it may not be the kernel I/O mechanism, especially Kernel AIO Support for Linux has not been merged into mainline kernel yet.
After studying rt extensions in glibc source, I find how do they implement posix AIO in glibc — a thread pool plus a request waiting queue. Besides, helper thread will be spawned to manage the request, hence it’s not so trivial.
There is something interesting when freeing allocated resources. You can see the function libc_freeres_fn() in aio_misc.c. This is also why it is unnecessary to implement “destroy” API.
Whereas, FreeBSD has AIO family system calls. I, however, have no experience with AIO in FreeBSD, but it seems AIO in FreeBSD has kernel support? If so, it would be more efficient than Linux until now?
Popularity: 29% [?]
26 Sep
Cool picture of all the new Vista Icons.
Besides, here is a tutorial from MSDN on how Microsoft makes their Windows XP icons.
Popularity: 56% [?]
26 Sep
Elevate through ShellExecute, from Vista Compatibility Team Blog.
We often get the question how to elevate a process through ShellExecute. From the docs it is not immediately clear. The trick is passing in “runas” in the lpVerb.
Here is a snippet to run notepad elevated.
#include “stdafx.h”
#include “windows.h”
#include “shellapi.h”
int _tmain(int argc, _TCHAR* argv[])
{
SHELLEXECUTEINFO shExecInfo;
shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
shExecInfo.fMask = NULL;
shExecInfo.hwnd = NULL;
shExecInfo.lpVerb = L”runas“;
shExecInfo.lpFile = L”notepad.exe”;
shExecInfo.lpParameters = NULL;
shExecInfo.lpDirectory = NULL;
shExecInfo.nShow = SW_MAXIMIZE;
shExecInfo.hInstApp = NULL;
ShellExecuteEx(&shExecInfo);
return 0;
}
Here is some screenshot:

Popularity: 51% [?]