Dec 19, 2007

明天放假

2007年12月20日是穆斯林的古尔邦节,我也跟着沾次光,明天放假。
还是第一次古尔邦节让我休息,爽啊

Dec 10, 2007

Linux远程桌面控制

1. vnc
vncserver 启动vnc服务
vncpasswd 修改连接密码
vncviewer vnc客户端

Tips:
a. linux上的vnc server内定的桌面管理环境是twm,实在是太简陋了.
把 ~/.vnc/xstartup 替换成 /etc/X11/xinit/Xsession
$ cp ~/.vnc/xstartup ~/.vnc/xstartup.old
$ rm ~/.vnc/xstartup
$ cp /etc/X11/xsession ~/.vnc/xstartup
$ vncserver -kill :1 // 启动vnc server, 其中1为桌面序号
$ vncserver

b. 通过浏览器使用vnc
通过浏览器使用vnc,要注意端口号的变化.
假设vnc server是172.16.1.2:1的话,那么,可用浏览器访问http://172.16.1.2:5801
端口号=display number + 5800

2. rdesktop
Linux通过rdesktop访问windows的远程桌面, 命令如下:
$ rdesktop -g 1024x768 -d 24 rhostname(or ip)
其中 "g 1024×768"设置分辨率为1024×768,"d 24"设置真彩24 位,hostname为 Windows 机器的主机名或者IP 地址。
在输入了Windows XP的用户名和密码后,就可以登录并操作远程的Windows系统

3. Remote X
I. 本机和远程机器都为Linux or UNIX
  现在设想登录到远程主机rhostname上,运行gimp 程序,并把它的显示输出到本机的屏幕上,那么需要依次执行以下操作:

  a.启动X 服务器

  # xhost +rhostname (允许远程rhostname 机使用本机的X Server)

  b.telnet (或ssh)登录远程主机

  # telnet rhostname

  c.设置DISPLAY 环境变量

  指定了一个显示设备,所有的图形程序都将显示到这个设备。

  DISPLAY 的格式为:hostname:displaynumber.screennumber。

  hostname是本机主机名,或者是它的I P 地址。一般d i s p l a y n u m b e r 、screennumber 都是0。

  # echo $SHELL

  如果返回的是 /bin/ksh,那么可以用:# export DISPLAY= 本机的IP 地址:0.0

  如果返回的是 /bin/csh,那么可以用:# setenv DISPLAY 本机的IP地址:0.0

  如果返回的是 /bin/bash,那么可以用:# DISPLAY= 本机的IP 地址:0.0

  # export DISPLAY

  d.启动gimp 程序

  # gimp

II. 本机为windows, 远程主机为*nix
在windows中安装xmanager, http://server.ccw.com.cn/yyjq/htm2005/20050906_0994B.htm



Dec 7, 2007

无法访问网上邻居

访问网上邻居提示:

\\192.168.1.2

无任何网络提供程序接受指定的网络路径。


Solution:

将 Workstation 服务启动起来,Ok

Dec 3, 2007

太牛了吧

看视频



Nov 28, 2007

FreeBSD 安装vim

习惯了Linux下的Vim,在FreeBSD下让 方向键 把我都快弄晕了
不得已只能重新装一个,具体过程:
# pkg_delete vim*
# pkg_add -r vim
# mv /usr/bin/vi /usr/bin/vi.back
# ln -s /usr/local/bin/vim /usr/bin/vi
# cp /usr/local/share/vim/vim63/vimrc_example.vim /root/.vimrc
# vi /root/.vimrc
set nobackup

FreeBSD 终端显示颜色

在/etc/csh.cshrc中添加下面的内容
# display color
setenv LSCOLORS ExGxFxdxCxegedabagExEx
setenv CLICOLOR yes

Nov 22, 2007

Happy Thanksgiving Day

2007.11.20 18:52 很特殊的时刻 :-)



最近去了两次做假肢的公司,看到一个很小的孩子就残疾了,心里很是难受(不过晚上弄得我做噩梦)



在感恩节到来之际,祝大家幸福快乐 ^_^



Nov 20, 2007

关闭SHELL的提示声

基本来源于下面的链接,我添加了FreeBSD的csh

From :

http://www.linuxforum.net/forum/showflat.php?Cat=&Board=office&Number=596796&page=5&view=collapsed&s

Introduction
当电脑对我嘟嘟嚷的时候,我真的觉得很讨厌。我常常在 shell 里面使用 Tab-补
全来节省大量的输入时间,但是我受不了扬声器没完没了的嘟嘟声!

下面就为大家介绍如何快速去除讨厌的叫声。在 shell 里面,你可以按下 crtl-g
来测试一下这个嘟嘟声是否已经去掉。
关掉所有的提示音
在 Linux 控制台下(没有 X11),你可以使用一下命令:

setterm -blength 0

#alternatively you can change the frequency of the beep to a
#very low value:

setterm -bfreq 10

而在 X11 下面(不管是 KDE、Gnome、XFCE 或者……) 你可以:

xset b off


对每种 shell 操作
作为一种可能的选择,你可以直接关掉某种 shell 里的提示音。

对 Bash:

# has to go into /etc/inputrc or .inputrc
# It will not work in a .bashrc file!
set bell-style none

对 Tcsh:

# put this into your .tcshrc file

# just tab completion beep off:
set matchbeep = never
# any beep off:
set nobeep = 1

对FreeBSD的Csh:
# put into /etc/csh.cshrc
set nobeep=1

结论
为避免误解,特此声明:以上操作只是关掉了(蜂鸣器的)嘟嘟声,你仍然可以在
你的电脑上自在的听歌。

世界清静了……

Nov 8, 2007

Unix Daemon Server Programming [zt]


Unix Daemon Server Programming

Introduction

Unix processes works either in foreground or background. A process running in foreground interacts with the user in front of the terminal (makes I/O), whereas a background process runs by itself. The user can check its status but he doesn't (need to) know what it is doing. The term 'daemon' is used for processes that performs service in background. A server is a process that begins execution at startup (not neccessarily), runs forever, usually do not die or get restarted, operates in background, waits for requests to arrive and respond to them and frequently spawn other processes to handle these requests.

Readers are suppossed to know Unix fundamentals and C language. For further description on any topic use "man" command (I write useful keywords in brackets), it has always been very useful, trust me :)) Keep in mind that this document does not contain everything, it is just a guide.

1) Daemonizing (programming to operate in background) [fork]

First the fork() system call will be used to create a copy of our process(child), then let parent exit. Orphaned child will become a child of init process (this is the initial system process, in other words the parent of all processes). As a result our process will be completely detached from its parent and start operating in background.

 i=fork();
if (i<0) exit(1); /* fork error */
if (i>0) exit(0); /* parent exits */
/* child (daemon) continues */

2) Process Independency [setsid]

A process receives signals from the terminal that it is connected to, and each process inherits its parent's controlling tty. A server should not receive signals from the process that started it, so it must detach itself from its controlling tty.

In Unix systems, processes operates within a process group, so that all processes within a group is treated as a single entity. Process group or session is also inherited. A server should operate independently from other processes.

 setsid() /* obtain a new process group */

This call will place the server in a new process group and session and detach its controlling terminal. (setpgrp() is an alternative for this)

3) Inherited Descriptors and Standart I/0 Descriptors [gettablesize,fork,open,close,dup,stdio.h]

Open descriptors are inherited to child process, this may cause the use of resources unneccessarily. Unneccesarry descriptors should be closed before fork() system call (so that they are not inherited) or close all open descriptors as soon as the child process starts running.

 for (i=getdtablesize();i>=0;--i) close(i); /* close all descriptors */

There are three standart I/O descriptors: standart input 'stdin' (0), standart output 'stdout' (1), standart error 'stderr' (2). A standard library routine may read or write to standart I/O and it may occur to a terminal or file. For safety, these descriptors should be opened and connectedthem to a harmless I/O device (such as /dev/null).

 i=open("/dev/null",O_RDWR); /* open stdin */
dup(i); /* stdout */
dup(i); /* stderr */

As Unix assigns descriptors sequentially, fopen call will open stdin and dup calls will provide a copy for stdout and stderr.

4) File Creation Mask [umask]

Most servers runs as super-user, for security reasons they should protect files that they create. Setting user mask will pre vent unsecure file priviliges that may occur on file creation.

 umask(027);

This will restrict file creation mode to 750 (complement of 027).

5) Running Directory [chdir]

A server should run in a known directory. There are many advantages, in fact the opposite has many disadvantages: suppose that our server is started in a user's home directory, it will not be able to find some input and output files.

 chdir("/servers/");

The root "/" directory may not be appropriate for every server, it should be choosen carefully depending on the type of the server.

6) Mutual Exclusion and Running a Single Copy [open,lockf,getpid]

Most services require running only one copy of a server at a time. File locking method is a good solution for mutual exclusion. The first instance of the server locks the file so that other instances understand that an instance is already running. If server terminates lock will be automatically released so that a new instance can run. Recording the pid of the running instance is a good idea. It will surely be efficient to make 'cat mydaamon.lock' instead of 'ps -ef|grep mydaemon'

 lfp=open("exampled.lock",O_RDWR|O_CREAT,0640);
if (lfp<0) exit(1); /* can not open */
if (lockf(lfp,F_TLOCK,0)<0) exit(0); /* can not lock */
/* only first instance continues */

sprintf(str,"%d\n",getpid());
write(lfp,str,strlen(str)); /* record pid to lockfile */

7) Catching Signals [signal,sys/signal.h]

A process may receive signal from a user or a process, its best to catch those signals and behave accordingly. Child processes send SIGCHLD signal when they terminate, server process must either ignore or handle these signals. Some servers also use hang-up signal to restart the server and it is a good idea to rehash with a signal. Note that 'kill' command sends SIGTERM (15) by default and SIGKILL (9) signal can not be caught.

 signal(SIG_IGN,SIGCHLD); /* child terminate signal */

The above code ignores the child terminate signal (on BSD systems parents should wait for their child, so this signal should be caught to avoid zombie processes), and the one below demonstrates how to catch the signals.

 void Signal_Handler(sig) /* signal handler function */
int sig;
{
switch(sig){
case SIGHUP:
/* rehash the server */
break;
case SIGTERM:
/* finalize the server */
exit(0)
break;
}
}

signal(SIGHUP,Signal_Handler); /* hangup signal */
signal(SIGTERM,Signal_Handler); /* software termination signal from kill */

First we construct a signal handling function and then tie up signals to that function.

8) Logging [syslogd,syslog.conf,openlog,syslog,closelog]

A running server creates messages, naturally some are important and should be logged. A programmer wants to see debug messages or a system operator wants to see error messages. There are several ways to handle those messages.

Redirecting all output to standard I/O : This is what ancient servers do, they use stdout and stderr so that messages are written to console, terminal, file or printed on paper. I/O is redirected when starting the server. (to change destination, server must be restarted) In fact this kind of a server is a program running in foreground (not a daemon).

 # mydaemon 2> error.log

This example is a program that prints output (stdout) messages to console and error (stderr) messages to a file named "error.log". Note that this is not a daemon but a normal program.

Log file method : All messages are logged to files (to different files as needed). There is a sample logging function below.

 void log_message(filename,message)
char *filename;
char *message;
{
FILE *logfile;
logfile=fopen(filename,"a");
if(!logfile) return;
fprintf(logfile,"%s\n",message);
fclose(logfile);
}

log_message("conn.log","connection accepted");
log_message("error.log","can not open file");

Log server method : A more flexible logging technique is using log servers. Unix distributions have system log daemon named "syslogd". This daemon groups messages into classes (known as facility) and these classes can be redirected to different places. Syslog uses a configuration file (/etc/syslog.conf) that those redirection rules reside in.

 openlog("mydaemon",LOG_PID,LOG_DAEMON)
syslog(LOG_INFO, "Connection from host %d", callinghostname);
syslog(LOG_ALERT, "Database Error !");
closelog();

In openlog call "mydaemon" is a string that identifies our daemon, LOG_PID makes syslogd log the process id with each message and LOG_DAEMON is the message class. When calling syslog call first parameter is the priority and the rest works like printf/sprintf. There are several message classes (or facility names), log options and priority levels. Here are some examples :

Message classes : LOG_USER, LOG_DAEMON, LOG_LOCAL0 to LOG_LOCAL7
Log options : LOG_PID, LOG_CONS, LOG_PERROR
Priority levels : LOG_EMERG, LOG_ALERT, LOG_ERR, LOG_WARNING, LOG_INFO

About

This text is written by Levent Karakas <levent at mektup dot at >. Several books, sources and manual pages are used. This text includes a sample daemon program (compiles on Linux 2.4.2, OpenBSD 2.7, SunOS 5.8, SCO-Unix 3.2 and probably on your flavor of Unix). You can also download plain source file : exampled.c. Hope you find this document useful. We do love Unix.

/*
UNIX Daemon Server Programming Sample Program
Levent Karakas <levent at mektup dot at> May 2001

To compile: cc -o exampled examped.c
To run: ./exampled
To test daemon: ps -ef|grep exampled (or ps -aux on BSD systems)
To test log: tail -f /tmp/exampled.log
To test signal: kill -HUP `cat /tmp/exampled.lock`
To terminate: kill `cat /tmp/exampled.lock`
*/

#include <stdio.h>
#include <fcntl.h>
#include < signal.h>
#include <unistd.h>

#define RUNNING_DIR "/tmp"
#define LOCK_FILE "exampled.lock"
#define LOG_FILE "exampled.log"

void log_message(filename,message)
char *filename;
char *message;
{
FILE *logfile;
logfile=fopen(filename,"a");
if(!logfile) return;
fprintf(logfile,"%s\n",message);
fclose(logfile);
}

void signal_handler(sig)
int sig;
{
switch(sig) {
case SIGHUP:
log_message(LOG_FILE,"hangup signal catched");
break;
case SIGTERM:
log_message(LOG_FILE,"terminate signal catched");
exit(0);
break;
}
}

void daemonize()
{
int i,lfp;
char str[10];
if(getppid()==1) return; /* already a daemon */
i=fork();
if (i<0) exit(1); /* fork error */
if (i>0) exit(0); /* parent exits */
/* child (daemon) continues */
setsid(); /* obtain a new process group */
for (i=getdtablesize();i>=0;--i) close(i); /* close all descriptors */
i=open("/dev/null",O_RDWR); dup(i); dup(i); /* handle standart I/O */
umask(027); /* set newly created file permissions */
chdir(RUNNING_DIR); /* change running directory */
lfp=open(LOCK_FILE,O_RDWR|O_CREAT,0640);
if (lfp<0) exit(1); /* can not open */
if (lockf(lfp,F_TLOCK,0)<0) exit(0); /* can not lock */
/* first instance continues */
sprintf(str,"%d\n",getpid());
write(lfp,str,strlen(str)); /* record pid to lockfile */
signal(SIGCHLD,SIG_IGN); /* ignore child */
signal(SIGTSTP,SIG_IGN); /* ignore tty signals */
signal(SIGTTOU,SIG_IGN);
signal(SIGTTIN,SIG_IGN);
signal(SIGHUP,signal_handler); /* catch hangup signal */
signal(SIGTERM,signal_handler); /* catch kill signal */
}

main()
{
daemonize();
while(1) sleep(1); /* run */
}

/* EOF */

 

Last Update : 16.05.2001

 


Nov 7, 2007

ascii

ASCII �大致可以分作三部分�成。

第一部分由 00H 到 1FH 共 32 �,一般用�通�或作�控制之用,有些字元可�示於�幕,有些��法�示在�幕上,但能看到其效果(例如�行字元、�位字元)。如下表:

ASCII 表(0到1FH)

第二部分是由 20H 到 7FH 共 96 �,� 95 �字元是用�表示阿拉伯�字、英文字母大小�和底�、括�等符�,都可以�示在�幕上。如下表:

ASCII � 字元   ASCII � 字元   ASCII � 字元   ASCII � 字元
十�位 十六�位   十�位 十六�位   十�位 十六�位   十�位 十六�位
03220    05638 8  08050 P  10468 h
03321 !  05739 9  08151 Q  10569 i
03422 "  0583A :  08252 R  1066A j
03523 #  0593B ;  08353 S  1076B k
03624 $  0603C <  08454 T  1086C l
03725 %  0613D =  08555 U  1096D m
03826 &  0623E >  08656 V  1106E n
03927 '  0633F ?  08757 W  1116F o
04028 (  06440 @  08858 X  11270 p
04129 )  06541 A  08959 Y  11371 q
0422A *  06642 B  0905A Z  11472 r
0432B +  06743 C  0915B [  11573 s
0442C ,  06844 D  0925C \  11674 t
0452D -  06945 E  0935D ]  11775 u
0462E .  07046 F  0945E ^  11876 v
0472F /  07147 G  0955F _  11977 w
04830 0  07248 H  09660 `  12078 x
04931 1  07349 I  09761 a  12179 y
05032 2  0744A J  09862 b  1227A z
05133 3  0754B K  09963 c  1237B {
05234 4  0764C L  10064 d  1247C |
05335 5  0774D M  10165 e  1257D }
05436 6  0784E N  10266 f  1267E ~
05537 7  0794F O  10367 g  1277F

第三部分由 80H 到 0FFH 共 128 �字元,一般��『�充字元』,� 128 ��充字元是由 IBM 制定的,�非��的 ASCII �。�些字元是用�表示框�、音�和其他�洲非英�系的字母。

ASCII �(80H到0FFH)