以前学习整理的demo:
/*
* daemon.c
*
* Copyright 2012 vince <vince@vince-vsing>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <errno.h>
#include <syslog.h>
#include <libgen.h>
#define DEV_NULL "/dev/null"
#define ROOT_PATH "/"
void daemon_handle_connection(int fd) {
/*child process do something here*/
}
int redirect_fd(int fd, int flags) {
/* Attempt to open bit bucket */
int new_fd = open(DEV_NULL, flags);
if (new_fd < 0)
return 1;
/* If descriptor is different, redirect old to new and close new */
if (new_fd != fd) {
dup2(new_fd, fd);
close(new_fd);
}
return 0;
}
int daemonize() {
pid_t pid;
/* Fork once to ensure we aren't the process group leader */
pid = fork();
if (pid < 0) {
printf("Could not fork() parent: %s", strerror(errno));
return 1;
}
/* Exit if we are the parent */
if (pid > 0) {
printf("Exiting and passing control to PID %i", pid);
_exit(0);
}
/* Start a new session (if not already group leader) */
setsid();
/* Fork again so the session group leader exits */
pid = fork();
if (pid < 0) {
printf("Could not fork() group leader: %s", strerror(errno));
return 1;
}
/* Exit if we are the parent */
if (pid > 0) {
printf("Exiting and passing control to PID %i", pid);
_exit(0);
}
/* Change to root directory */
if (chdir(ROOT_PATH) < 0) {
printf(
"Unable to change working directory to "
ROOT_PATH);
return 1;
}
/* Reopen the 3 stdxxx to /dev/null */
if (redirect_fd(STDIN_FILENO, O_RDONLY)
|| redirect_fd(STDOUT_FILENO, O_WRONLY)
|| redirect_fd(STDERR_FILENO, O_WRONLY)) {
printf(
"Unable to redirect standard file descriptors to "
DEV_NULL);
return 1;
}
/* Success */
return 0;
}
int main(int argc, char* argv[]) {
/* Server */
int socket_fd;
struct addrinfo* addresses;
struct addrinfo* current_address;
char bound_address[1024];
char bound_port[64];
int opt_on = 1;
struct addrinfo hints = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM,
.ai_protocol = IPPROTO_TCP
};
/* Client */
struct sockaddr_in client_addr;
socklen_t client_addr_len;
int connected_socket_fd;
/* Arguments */
char* listen_address = NULL; /* Default address of INADDR_ANY */
char* listen_port = "4822"; /* Default port */
char* pidfile = NULL;
int opt;
int foreground = 0;
/* General */
int retval;
/* Parse arguments */
while ((opt = getopt(argc, argv, "l:b:p:f")) != -1) {
if (opt == 'l') {
listen_port = strdup(optarg);
}
else if (opt == 'b') {
listen_address = strdup(optarg);
}
else if (opt == 'f') {
foreground = 1;
}
else if (opt == 'p') {
pidfile = strdup(optarg);
}
else {
fprintf(stderr, "USAGE: %s"
" [-l LISTENPORT]"
" [-b LISTENADDRESS]"
" [-p PIDFILE]"
" [-f]\n", argv[0]);
exit(EXIT_FAILURE);
}
}
/* Log start */
printf("daemon (%s) start\n", argv[0]);
/* Get addresses for binding */
if ((retval = getaddrinfo(listen_address, listen_port,
&hints, &addresses))) {
printf("Error parsing given address or port: %s",
gai_strerror(retval));
exit(EXIT_FAILURE);
}
/* Get socket */
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd < 0) {
printf("Error opening socket: %s", strerror(errno));
exit(EXIT_FAILURE);
}
/* Allow socket reuse */
if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR,
(void*) &opt_on, sizeof(opt_on))) {
printf("Unable to set socket options for reuse: %s",
strerror(errno));
}
/* Attempt binding of each address until success */
current_address = addresses;
while (current_address != NULL) {
int retval;
/* Resolve hostname */
if ((retval = getnameinfo(current_address->ai_addr,
current_address->ai_addrlen,
bound_address, sizeof(bound_address),
bound_port, sizeof(bound_port),
NI_NUMERICHOST | NI_NUMERICSERV)))
printf("Unable to resolve host: %s",
gai_strerror(retval));
/* Attempt to bind socket to address */
if (bind(socket_fd,
current_address->ai_addr,
current_address->ai_addrlen) == 0) {
printf("Successfully bound socket to "
"host %s, port %s", bound_address, bound_port);
/* Done if successful bind */
break;
}
/* Otherwise log information regarding bind failure */
else
printf("Unable to bind socket to "
"host %s, port %s: %s",
bound_address, bound_port, strerror(errno));
current_address = current_address->ai_next;
}
/* If unable to bind to anything, fail */
if (current_address == NULL) {
printf("Unable to bind socket to any addresses.");
exit(EXIT_FAILURE);
}
/* Daemonize if requested */
if (!foreground) {
/* Attempt to daemonize process */
if (daemonize()) {
printf("Could not become a daemon.");
exit(EXIT_FAILURE);
}
}
/* Write PID file if requested */
if (pidfile != NULL) {
/* Attempt to open pidfile and write PID */
FILE* pidf = fopen(pidfile, "w");
if (pidf) {
fprintf(pidf, "%d\n", getpid());
fclose(pidf);
}
/* Fail if could not write PID file*/
else {
printf("Could not write PID file: %s", strerror(errno));
exit(EXIT_FAILURE);
}
}
/* Ignore SIGPIPE */
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
printf("Could not set handler for SIGPIPE to ignore. "
"SIGPIPE may cause termination of the daemon.");
}
/* Ignore SIGCHLD (force automatic removal of children) */
if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
printf("Could not set handler for SIGCHLD to ignore. "
"Child processes may pile up in the process table.");
}
/* Log listening status */
printf("Listening on host %s, port %s", bound_address, bound_port);
/* Free addresses */
freeaddrinfo(addresses);
/* Daemon loop (Use multi_process, not multi_thread)*/
for (;;) {
pid_t child_pid;
/* Listen for connections */
if (listen(socket_fd, 5) < 0) {
printf("Could not listen on socket: %s", strerror(errno));
return 3;
}
/* Accept connection */
client_addr_len = sizeof(client_addr);
connected_socket_fd = accept(socket_fd,
(struct sockaddr*) &client_addr, &client_addr_len);
if (connected_socket_fd < 0) {
printf("Could not accept client connection: %s",
strerror(errno));
return 3;
}
/*
* Once connection is accepted, send child into background.
*
* Note that we prefer fork() over threads for connection-handling
* processes as they give each connection its own memory area, and
* isolate the main daemon and other connections from errors in any
* particular client plugin.
*/
child_pid = fork();
/* If error, log */
if (child_pid == -1)
printf("Error forking child process: %s", strerror(errno));
/* If child, start client, and exit when finished */
else if (child_pid == 0) {
daemon_handle_connection(connected_socket_fd);
close(connected_socket_fd);
return 0;
}
/* If parent, close reference to child's descriptor */
else if (close(connected_socket_fd) < 0) {
printf("Error closing daemon reference to "
"child descriptor: %s", strerror(errno));
}
}
/* Close socket */
if (close(socket_fd) < 0) {
printf("Could not close socket: %s", strerror(errno));
return 3;
}
return 0;
}