hexdump command examples in linux

hexdump:

linux1machine-~/ssdir: cat dumptext.txt
aj14Qy8

Character
linux1machine-~/ssdir: hexdump -c dumptext.txt
0000000   a   j   1   4   Q   y   8  \n
0000008

Install VMWARE Tools in CentOS

1. change to root

su

2. yum install gcc

run vmware without gui

download vix api from vmware site


https://www.vmware.com/support/developer/vix-api/

VIX SDK for Linux 64-bit 


Setting static ip in Linux/Centos/RHEL/

1. login as root

[fedo@dhcppc6 ~]$ su root
Password:
[root@dhcppc6 fedo]#


2. Stop network manager

[root@dhcppc6 fedo]# service NetworkManager stop
Stopping NetworkManager daemon:                            [  OK  ]


Daemonizing Server Process in Linux

If we have the server it has to handle all the request and response without the help of 
user. So better running server process as background is good idea.

We can run the Linux process as foreground or background. A process is called daemon when it running in background
and doing necessary work.

Steps to make Server process as daemon:

1. Create child process and separate from parent - fork()

create child process using fork() system call, then exit parent process. Once parent exits new child process
will child of init process and behave as background process.

pid = fork()
if (pid < 0)
exit(1);

if (pid > 0)
exit(0);

/* child continue as daemon */


2. Make new child process independent - setsid()

When terminal receives signal, all the process connected to this terminal get signal. 
child process created through fork() system call will inherit process group of parent process. All the signal passed
to process group will passed to inherited process. 

daemon server should not get signal from where was started. 

We can make daemon process independent from parent using setsid() system call

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

3. Handle standard I/O stream - getdtablesize(), open(), dup(), dup2()

fork system call will inherit all open file descriptor of parent. so close all the unnecessary open file 
descriptor.

for( fds=getdtablesize(); fds >=0; fd--)
close(fds);

standard libraries use standard stream for some work, better close all three standard ( stdin - 0, stdout - 1, stderr - 2)
for safety purpose redirect the out put to /dev/null.

i = open("/dev/null", O_RDWR);
dup(i);
dup(i);

for ease understanding read the man page of 'open', 'dup' & 'dup2'.

4. Change file creation mask - umask()

Most of the server run as root user mode(not mandatory). So it should protect the files it created when it run.
we can change the process privilege using umask system call.

umask(027);

This will restrict file creation mode to 750

5. Change working directory - chdir()

Server should be run from root directory (not mandatory), then only it can see all the files 
from system.

chdir("/");

"/" means root directory.

6. Run single copy of server

For a system it requires only one copy server at a time. File locking is a good method to achieve this by making
other process wait on the file lock. when process terminate lock on this file automatically released then server
which waiting on the lock will start work.

write the pid of the process to file, which will help to know the running pid of the server. 

lfp = open("lockfile.lock", O_RDWR|O_CREAT, 0650);
if (lfp < 0)
exit(1);
if (lockf(lfp, F_TLOCK, 0) < 0)
exit(1);
sprintf(strpid, "%d\n", getpid());
write(lfp, strpid, strlen(strpid));

7. Logging

When server is running some messages should be logged. This will help for developer to debug the server
if any problem arise.

void logging(char *filename, char* log)
{
FILE *fp;
fp = open(filename, "a");
if (fp == NULL) 
return;
fprintf(logfile, "%s\n", log);
fclose(fp);
}

/* log messages as below */
logging("logfile.txt", "This is server log");


Program:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

#define WORKING_DIR "/home/sujin/c"
#define LOCK_FILE "lockfile.lock"
#define LOG_FILE "logfile.log"

void make_daemon();
void logging(char *file, char *log);

void logging(char *file, char *log)
{
        FILE *fp;
        if ((fp = fopen(file, "a")) == NULL)
                return;
        fprintf(fp, "%s\n", log);
        fclose(fp);
}

void make_daemon()
{
        int pid, lockfd, i;
        char pidstr[16];

        pid = fork();
        if (pid < 0) {
                printf("fork error.\n");
                exit(1);
        }
        if (pid > 0)
                exit(0);

        setsid();

        for (i = getdtablesize(); i >= 0; i--)
                close(i);

        i = open("/dev/null", O_RDWR);
        dup(i);
        dup(i);

        umask(027);

        chdir(WORKING_DIR);

        lockfd = open(LOCK_FILE, O_RDWR|O_CREAT, 0640);
        if (lockfd < 0)
                exit(1);

        if (lockf(lockfd, F_TLOCK, 0) < 0)
                exit(1);

        sprintf(pidstr, "%d\n", getpid());
        write(lockfd, pidstr, strlen(pidstr));

}

int main(int argc, char *argv[])
{
        make_daemon();
        sleep(5);
        while(1)
        {
                /* Do server work */
                logging(LOG_FILE, "This is write from main server.\n");
        }

        return 0;
}


I am not full owner of this article. I grabbed some details from other articles. Full credits goes to author of Unix Daemon Server Programming