Saturday, December 24, 2011

[L] Ubuntu: library curl not found

Just try this:

sudo apt-get install libcurl4-openssl-dev

You can't check it out in "aptitude search curl".

Friday, December 23, 2011

To my dear Yu

You Are Beautiful
No Matter What They Say
You Can Bring Me Down


You Are Wonderful
In Every Single Way
You Can Bring Me Down


So Don't You Bring Me Down Today

Thursday, December 22, 2011

[C] Resource Limit in Linux

之前开会大师兄问了下 'ulimit' 在底层用什么搞的,今天刚好遇到,就顺便写下来了。

内部有个“sys/resource.h”的库,里面主要有3个重要功能:
1. rlimit
2. rusage
3. priority

rlimit 挺好理解。rusage 跟 priority 不太懂,跟系统底层有关:一个是相关信息,一个是调度的优先级。

下面直接贴代码:
/*
 * =====================================================================================
 *
 *       Filename:  rlimit.c
 *
 *    Description:  Resources limit test
 *
 *        Version:  1.0
 *        Created:  Thursday, December 22, 2011 07:20:02 HKT
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Hongchao Deng (fengjingchao), fengjingchao@gmail.com
 *        Company:  Sun Yat-sen University
 *
 * =====================================================================================
 */




#include    <errno.h>
#include    <math.h> 
#include    <stdio.h>
#include    <stdlib.h>
#include    <string.h>


#include <sys/types.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <unistd.h>




/*
 * ===  FUNCTION  ======================================================================
 *         Name:  work
 *  Description:  This function writes a string to a temporary file 10,000 times and then
 *                performs some arithmetic to generate load on CPU. HOW BAD...
 * =====================================================================================
 */
void
work (  )
{
   FILE *f;
   int i;
   double x = 4.5;


   f = tmpfile();


   // Writing about 10K to the tmp file
   for (i = 0; i < 10000; i++) {
      /* code */
      fprintf(f, "Do some output.\n");
      if( ferror(f) )
      {
         fprintf(stderr,"Error while writing to temporary file.\n");
         exit(EXIT_FAILURE);
      }
   }
  
   for (i = 0; i < 1000000; i++) {
      /* code */
      x = log(x*x+3.21);
   }


}        /* -----  end of function work  ----- */




/*
 * ===  FUNCTION  ======================================================================
 *         Name:  rusage_test
 *  Description: 
 * =====================================================================================
 */
void
rusage_test ( )
{
   struct rusage r_usage;
   work();
   getrusage(RUSAGE_SELF, &r_usage);
  
   printf("\n**************************************\n");


   printf ( "rusage information:\n" );
   printf ( "  ru_utime:\n    tv_sec.tv_usec: %ld.%06ld (s)\n",
                    r_usage.ru_utime.tv_sec, r_usage.ru_utime.tv_usec );
   printf ( "  ru_stime:\n    tv_sec.tv_usec: %ld.%06ld (s)\n",
                    r_usage.ru_stime.tv_sec, r_usage.ru_stime.tv_usec );

}        /* -----  end of function rusage_test  ----- */


/*
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description:  main function
 * =====================================================================================
 */
    int
main ( int argc, char *argv[] )
{
   // parameters
   struct rlimit r_limit;


   // todo
   rusage_test();
   rusage_test();
   rusage_test();


   printf("\n**************************************\n"); 
   printf("priority information:\n");
   printf("  process priority: %d\n", getpriority(PRIO_PROCESS,getpid()));
   printf("  group   priority: %d\n", getpriority(PRIO_PGRP,getgid()));
   printf("  user    priority: %d\n", getpriority(PRIO_PGRP,getuid()));


   printf("\n**************************************\n"); 
   printf("rlimit information: (RLIM_INFINITY=%ld)\n", RLIM_INFINITY);


   getrlimit(RLIMIT_CPU, &r_limit);
   printf ( "  rlimit_cpu:\n    s: %ld ; h: %ld\n", r_limit.rlim_cur, r_limit.rlim_max );


   getrlimit(RLIMIT_DATA, &r_limit);
   printf ( "  rlimit_data:\n    s: %ld ; h: %ld\n", r_limit.rlim_cur, r_limit.rlim_max );


   getrlimit(RLIMIT_FSIZE, &r_limit);
   printf ( "  rlimit_fsize:\n    s: %ld ; h: %ld\n", r_limit.rlim_cur, r_limit.rlim_max );


   getrlimit(RLIMIT_NOFILE, &r_limit);
   printf ( "  rlimit_nofile:\n    s: %ld ; h: %ld\n", r_limit.rlim_cur, r_limit.rlim_max );


   getrlimit(RLIMIT_NPROC, &r_limit);
   printf ( "  rlimit_nproc:\n    s: %ld ; h: %ld\n", r_limit.rlim_cur, r_limit.rlim_max );


   printf("\nNow try to set some limits:\n");
   r_limit.rlim_cur = 1024; r_limit.rlim_max = RLIM_INFINITY;
   setrlimit(RLIMIT_FSIZE, &r_limit);
   work();


    return EXIT_SUCCESS;
}        /* ----------  end of function main  ---------- */


输出样例:
**************************************
rusage information:
  ru_utime:
    tv_sec.tv_usec: 0.040002 (s)
  ru_stime:
    tv_sec.tv_usec: 0.000000 (s)

**************************************
rusage information:
  ru_utime:
    tv_sec.tv_usec: 0.084005 (s)
  ru_stime:
    tv_sec.tv_usec: 0.000000 (s)

**************************************
rusage information:
  ru_utime:
    tv_sec.tv_usec: 0.128008 (s)
  ru_stime:
    tv_sec.tv_usec: 0.000000 (s)

**************************************
priority information:
  process priority: 0
  group   priority: -1
  user    priority: -1

**************************************
rlimit information: (RLIM_INFINITY=-1)
  rlimit_cpu:
    s: -1 ; h: -1
  rlimit_data:
    s: -1 ; h: -1
  rlimit_fsize:
    s: -1 ; h: -1
  rlimit_nofile:
    s: 1024 ; h: 4096
  rlimit_nproc:
    s: 15915 ; h: 15915

Now try to set some limits:
File size limit exceeded



感悟:
1. 调用getrusage的时候,只是得到当前耗掉的时间,这点在上面我用了几次work中体现出来了。
2. priority是数值越小,优先越高。
3. 普通人set的时候只能变弱,即priority+,limit - 。superuser有能力set,但是在内核限制的范围内。
4. 为什么要有soft & hard limit 两种呢?查了下,是因为soft 是能提高的,但是hard普通用户只能减少,这样有个限度。

References:
http://linux.about.com/library/cmd/blcmdl2_setrlimit.htm
http://linux.die.net/man/2/getrusage
http://linux.die.net/man/2/gettimeofday
http://linux.die.net/man/2/setpriority
http://linux.die.net/man/2/nice

Tuesday, December 20, 2011

[S] TPM Emulator

 最简单的TPM Emulator安装。。可惜很乱。
http://tpm-emulator.berlios.de/installation.html

补充一点,
之前出现 ”Module tpmd_dev not found” 的错误,最后网上查到,要在
modprobe tpmd_dev "之前,先
depmod -a

至于你有没解决,反正我是解决了。。-_-|||

Monday, December 19, 2011

[C] ‘getopt()' function in Linux

先贴个网站(没错,是万恶又全能的Linux man page):
http://linux.die.net/man/3/getopt

再晒下自己代码:
/*
 * =====================================================================================
 *
 *       Filename:  argopt.c
 *
 *    Description:  Test example for 'getopt' function
 *
 *        Version:  1.0
 *        Created:  Monday, December 19, 2011 04:54:49 HKT
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Hongchao Deng (fengjingchao), fengjingchao@gmail.com
 *        Company:  Sun Yat-sen University
 *
 * =====================================================================================
 */

#include    <errno.h>
#include    <math.h> 
#include    <stdio.h>
#include    <stdlib.h>
#include    <string.h>
#include        <unistd.h>


/*
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description:  main function
 * =====================================================================================
 */
    int
main ( int argc, char *argv[] )
{
   int opt;

   while ( (opt = getopt(argc, argv, ":if:lr")) != -1 )
   {
     
      switch ( opt ) {
      case 'i':   
      case 'l':   
      case 'r':   
         printf("option: %c\n",opt);
         break;

      case 'f':
         if(optarg[0] != '-')      printf ( "filename: %s\n",optarg );
         else{
            --optind;
            printf ( "Option '-f' needs a value\n" );
         }
         break;

      case ':':   
         printf ( "Option '-%c' needs a value\n", optopt );
         break;
        
      case '?':   
         printf ( "Unknown option: %c\n", optopt );
         break;

      default:   
         break;
      }                /* -----  end switch  ----- */
   }

  
   for ( ; optind < argc; ++optind)
   {
      printf ( "argument: %s\n", argv[optind] );
   }

   return EXIT_SUCCESS;
}  /* ----------  end of function main  ---------- */

测试用例:
./argopt -i -lr -f -i -f filename -q "Hello,world"
option: i
option: l
option: r
Option '-f' needs a value
option: i
filename: filename
Unknown option: q
argument: Hello,world

好玩吧。。说说感悟:
1. 匹配出错的时候 '当前option' 放在 'optopt' 这个变量里面。
2. optind是一个个move的。。你看上面那个 '-f' 出错的时候直接 " -- optind " 解决了。
3. option有value 的话放在 'optarg' 里面.
4. getopt()的第三个参数 optstring 前面有个 "+" | "-" , 还有个什么POSIX*****之类的环境变量,无视他们。。用最基础的就行。顺便说说加了个 ‘:’(注意如果有 +-, +- 要放前面),主要是关注出错的时候是 1.没有value, 还是 2. option不识别。
5. getopt()是自动permute的,注意最后所有变量都放在 optind -- argc之间。。

getiopt_long() 是GNU扩展版。看了man page 的样例后说说感想:
1. optstring可以有数字字符,即 [0-9], 其他的不知道,也没必要知道。你看Linux命令有 '-*', '-%'这样的吗。。。
2. optarg: 如果没有 arg, optarg = 0. 这个特性可以方便编程。

Saturday, December 17, 2011

[LK] Linux kernel book

I have found an interesting website with no code lines talking about the mechanisms and principles that Linux uses. It's a little obsolete as with 2.0 kernel, but good for learning, I guess...

http://tldp.org/LDP/tlk/tlk-toc.html

Friday, December 16, 2011

[L] 在Fedora 15下开FTP (vsftpd)

网上有很多如何开FTP的教程,主要都是vsftpd.
但是,作为有着SELINUX的bug级fedora 15, 还需要我这样的小菜做更多努力才行,下面总结几点:

1.  vsftpd.conf
anonymous_enable=YES/NO
local_enable=YES/NO
不要傻傻地去注释

2. chroot_list
在vsftpd.conf里,如果chroot_local_user=YES,那么chroot_list里面有就是给整个目录,否则给chroot。比如我是ftptm, 那么我写进了chroot_list之后,用我的名字连上去就是"/home/ftptm", else it's just "/", like anonymous.

3. iptables
记得在iptables里面开20,21端口啊亲。还有iptables-config里面
iptables_modules="ip_conntrack_ftp"

4. selinux
如果你以为这样就结束了,你就上当了-- 你会发现用users连接的时候出现 "cannot change directory"这样的error.
getsebool -a | grep ftp
setsebool -P ftp_home_dir on
getsebool -a | grep ftp

就是让ftp_home_dir --> on就解决了。

*************************
结束,顺带一句,vsftpd is short for "very secure ftp daemon". Again, "very" secure. But I don't use it(ftps) though I study it...

Monday, December 12, 2011

[L] Linux装输入法

Linux中的中文输入法一直是个头疼的问题,尤其是云输入法。不要怕,网上有大神给出了:

http://www.linuxdiyf.com/viewarticle.php?id=207519

哎,可惜不能上传文件yong,不过这个输入法真的很好用!