Single-instance Implementation of daemon

Source: Internet
Author: User
To work properly, the daemon should be implemented as a single instance, that is, to run only one copy of the daemon at any time, because the daemon needs to access a device. The file lock mechanism is used here. If the daemon creates a file and adds a lock to the entire file, only one such write lock can be created, after that, if you try to create another write lock, it will fail, so as to indicate to the replica of the daemon process that a copy is already running. This lock file is usually stored in the/var/run directory. The name of the lock file is usually name. PID, where name is the name of the daemon. Note: The daemon may need root permission to create files in this directory.

Code implementation and analysis:

# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include

# Define lockfile "/var/run/test. PID"
# Define lockmode (s_irusr | s_iwusr | s_irgrp | s_iroth)

// 0 is returned for success. If an error is returned,-1 is returned. The cause of the error is errno.

Int lockfile (INT ff)
{
Struct flock FL;

// Rochelle type is the locked state. It has three states: f_rdlck is used to create a lock for reading, f_wrlck is used to create a lock for writing, and f_unlck is used to delete a previously created lock.

Fl. l_type = f_wrlck;

// Rochelle start indicates the starting position of the locked area.

Fl. l_start = 0;

// Rochelle whenec determines the Rochelle start position. It has three states: seek_set is the starting position of the file to be locked; seek_cur is the starting position of the current file read/write location as the lock; seek_end is the starting position of the file to be locked at the end.

Fl. l_whence = seek_set;

// L_len is used to set the size of the locked area.

Fl. l_len = 0;
// Another Rochelle PID is used to set the lock action.

// Fcntl () is the only POSIX-compliant file lock implementation, so it is also the only portable and powerful.

// F_setlk: If l_whence, l_start, and l_len are all set, a lock is assigned if l_type is set to f_rdlck or f_wrlck, if l_type is set to f_unlck, a lock is released. If it fails,-1 is returned, and errno error is set to eacces or eagain.

Return (fcntl (FF, f_setlk, & FL ));
}

Void already_running (void)
{
Int FD;
Char Buf [16];

// Here, open is the system call function, and the first parameter is the file path to open; the second parameter is the permission to operate on the file, which is readable and writable, can be created (if the file does not exist, it will be automatically created). The third parameter is the permission granted to the file after the file is created. For example, s_irusr is equivalent to 400, and the user has the read permission.

FD = open (lockfile, o_rdwr | o_creat, lockmode );

If (FD = 0)
{
IOCTL (FP, tiocnotty, null );
Close (FP );
}

// The process has become the no-end session leader, but it can re-apply to open a new control terminal. You can disable the process from re-opening the control terminal by making the process no longer the session leader. You need to call the fork function again.

If (Fork ()! = 0)
{
Exit (1 );
}

// The current working directory inherited from the parent process may be in an assembled file system. Because the daemon usually exists before the system restarts, if the current working directory of the daemon is in an assembly file system, the file system cannot be uninstalled. For example, the current directory inherited from the parent process is a mounted directory under/MNT.

If (chdir ("/tmp") =-1)
{
Exit (1 );
}

// Disable the opened file descriptor or redirect the file descriptor of the standard input, standard output, and standard error output. A process inherits the opened file descriptor from the parent process that created it. If you do not close the service, system resources will be wasted and unexpected errors will occur. Getdtablesize () returns the maximum number of files that a process can open.

For (FD = 0, fdtablesize = getdtablesize (); FD

  • Next article: entrepreneurs will have nine experiences
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.