2015年5月20日 星期三

Build LUA in CentOS 6.5

ref:
http://www.lua.org/download.html
http://lua-users.org/lists/lua-l/2014-08/msg00308.html


1. download and extract lua

curl -R -O http://www.lua.org/ftp/lua-5.3.0.tar.gz
tar zxf lua-5.3.0.tar.gz
cd lua-5.3.0


2. install readline


yum install readline-devel


3. vim src/Makefile  and add -lncurses

linux:
        $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -ldl -lreadline -lncurses"

4. make linux test


2015年5月12日 星期二

Upgrade MySQL (5.0 -> 5.6)

1.mysqldump 5.0 server data and, import to 5.6 server


2. let 5.0 be master and 5.6 be slave, replication


3. ensure all the sql statements  are correct


4. change 5.6 to master


NOTICE :

Some statement could execute by replication but generate error when change 5.6 to master
because sql_mode is strict sql mode

reference :
https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html
https://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_sql_mode


examples :

CREATE TABLE `test2` (
`no` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`data1` INT( 11 ) NOT NULL ,
`PS` TINYTEXT NOT NULL ,
`sdate` DATETIME NOT NULL ,
PRIMARY KEY ( `no` )
)
A.
INSERT INTO test2( `no` , `data1` , `PS` , `sdate` ) 
VALUES (
'', 1, "testps", '2015-05-12 18:00:00'
)
#1366 - Incorrect integer value: '' for column 'no' at row 1

B.
INSERT INTO test2( `no` , `data1` , `sdate` ) 
VALUES (
NULL , 1, '2015-05-12 18:00:00'
)

#1364 - Field 'PS' doesn't have a default value

C.
INSERT INTO test2( `no` , `data1` , `sdate` ) 
VALUES (
NULL , 1, '2015-05-12 18:00:00'
)

#1292 - Incorrect datetime value: '' for column 'sdate' at row 1

D.
INSERT INTO test2( `no` , `data1` , `PS` , `sdate` ) 
VALUES (
NULL , 1, 'pstest', '0000-13-00 00:00:00'
)

 #1292 - Incorrect datetime value: '0000-13-00 00:00:00' for column 'sdate' at row 1 


In none-strict sql mode or in 5.0, those statements will be executed correctly.

The correct form should be

INSERT INTO `test2` (`no`, `data1`, `PS`, `sdate` ) VALUES (NULL, 1, 'pstest', '0000-00-00 00:00:00');
or
INSERT INTO `test2` (`no`, `data1`) VALUES (0, 1, 'pstest', '0000-00-00 00:00:00');
or
INSERT INTO `test2` (`data1`) VALUES (1, 'pstest', '0000-00-00 00:00:00');


To disable strict_sql_mode (reference)

 1. check default sql_mode by

mysql> select @@sql_mode;
+------------------------+
| @@sql_mode       |
+------------------------+
| NO_ENGINE_SUBSTITUTION, STRICT_TRANS_TABLES |
+------------------------+

 2. in my.cnf, change

sql_mode="NO_ENGINE_SUBSTITUTION, STRICT_TRANS_TABLES"
to
sql_mode="NO_ENGINE_SUBSTITUTION"









2015年5月8日 星期五

Linux daemonize

1. from stackoverflow
http://stackoverflow.com/questions/17954432/creating-a-daemon-in-linux

Sample Source Code
/*
 * daemonize.c
 * This example daemonizes a process, writes a few log messages,
 * sleeps 20 seconds and terminates afterwards.
 */

#include 
#include 
#include 
#include 
#include 
#include 
#include 

static void skeleton_daemon()
{
    pid_t pid;

    /* Fork off the parent process */
    pid = fork();

    /* An error occurred */
    if (pid < 0)
        exit(EXIT_FAILURE);

    /* Success: Let the parent terminate */
    if (pid > 0)
        exit(EXIT_SUCCESS);

    /* On success: The child process becomes session leader */
    if (setsid() < 0)
        exit(EXIT_FAILURE);

    /* Catch, ignore and handle signals */
    //TODO: Implement a working signal handler */
    signal(SIGCHLD, SIG_IGN);
    signal(SIGHUP, SIG_IGN);

    /* Fork off for the second time*/
    pid = fork();

    /* An error occurred */
    if (pid < 0)
        exit(EXIT_FAILURE);

    /* Success: Let the parent terminate */
    if (pid > 0)
        exit(EXIT_SUCCESS);

    /* Set new file permissions */
    umask(0);

    /* Change the working directory to the root directory */
    /* or another appropriated directory */
    chdir("/");

    /* Close all open file descriptors */
    int x;
    for (x = sysconf(_SC_OPEN_MAX); x>0; x--)
    {
        close (x);
    }

    /* Open the log file */
    openlog ("firstdaemon", LOG_PID, LOG_DAEMON);
}
int main()
{
    skeleton_daemon();

    while (1)
    {
        //TODO: Insert daemon code here.
        syslog (LOG_NOTICE, "First daemon started.");
        sleep (20);
        break;
    }

    syslog (LOG_NOTICE, "First daemon terminated.");
    closelog();

    return EXIT_SUCCESS;
}

  • Compile the code: gcc -o firstdaemon daemonize.c
  • Start the daemon: ./firstdaemon
  • Check if everything is working properly: ps -xj | grep firstdaemon
  • The output should be similar to this one:
+------+------+------+------+-----+-------+------+------+------+-----+
| PPID | PID  | PGID | SID  | TTY | TPGID | STAT | UID  | TIME | CMD |
+------+------+------+------+-----+-------+------+------+------+-----+
|    1 | 3387 | 3386 | 3386 | ?   |    -1 | S    | 1000 | 0:00 | ./  |
+------+------+------+------+-----+-------+------+------+------+-----+
What you should see here is:
  • The daemon has no controlling terminal (TTY = ?)
  • The parent process ID (PPID) is 1 (The init process)
  • The PID != SID which means that our process is NOT the session leader
    (because of the second fork())
  • Because PID != SID our process can't take control of a TTY again
Reading the syslog:
  • Locate your syslog file. Mine is here: /var/log/syslog
  • Do a: grep firstdaemon /var/log/syslog
  • The output should be similar to this one:
  firstdaemon[3387]: First daemon started.
  firstdaemon[3387]: First daemon terminated.

2. from redis github sample
https://github.com/antirez/redis/blob/unstable/src/redis.c




void daemonize(void) {

int fd;


if (fork() != 0) exit(0); /* parent exits */

setsid(); /* create a new session */


/* Every output goes to /dev/null. If Redis is daemonized but

* the 'logfile' is set to 'stdout' in the configuration file

* it will not log at all. */

if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {

dup2(fd, STDIN_FILENO);

dup2(fd, STDOUT_FILENO);

dup2(fd, STDERR_FILENO);

if (fd > STDERR_FILENO) close(fd);

}

}



  • The output should be similar to this one:
+------+------+------+------+-----+-------+------+------+------+-----+
| PPID | PID  | PGID | SID  | TTY | TPGID | STAT | UID  | TIME | CMD |
+------+------+------+------+-----+-------+------+------+------+-----+
|    1 | 17508| 17508| 17508| ?   |    -1 | S    | 1000 | 0:00 | ./  |
+------+------+------+------+-----+-------+------+------+------+-----+

the pid = sid