New "module" proc.c/proc.h for generic process handling

The new "module" proc.c is used for functions dealing with child
processes. At the moment, it is only used by the asynchronous resolver.

All the functions already implemented habe been migrated from the
resolver code base, and the rest of the ngIRCd source code has been
adepted to the new namespace and calling conventions.

The goal is to develop "generic" process handling functions that can
be used for other purposes as well, e.g. running processes on client
connects etc.
This commit is contained in:
Alexander Barton 2010-05-18 17:38:12 +02:00
parent cc336b7558
commit 54e67ea9ee
11 changed files with 187 additions and 131 deletions

View File

@ -36,6 +36,7 @@
FA322DC10CEF77CB001761B3 /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = FA322DC00CEF77CB001761B3 /* libz.dylib */; };
FA407F2E0DB159F400271AF1 /* ng_ipaddr.c in Sources */ = {isa = PBXBuildFile; fileRef = FA407F2C0DB159F400271AF1 /* ng_ipaddr.c */; };
FA85178C0FA061EC006A1F5A /* op.c in Sources */ = {isa = PBXBuildFile; fileRef = FA85178B0FA061EC006A1F5A /* op.c */; };
FA99428C10E82A27007F27ED /* proc.c in Sources */ = {isa = PBXBuildFile; fileRef = FA99428B10E82A27007F27ED /* proc.c */; };
FAA3D27B0F139CDC00B2447E /* conn-ssl.c in Sources */ = {isa = PBXBuildFile; fileRef = FAA3D2790F139CDC00B2447E /* conn-ssl.c */; };
FAE5CC2E0CF2308A007D69B6 /* numeric.c in Sources */ = {isa = PBXBuildFile; fileRef = FAE5CC2D0CF2308A007D69B6 /* numeric.c */; };
/* End PBXBuildFile section */
@ -196,6 +197,8 @@
FA407F380DB15AC700271AF1 /* GIT.txt */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = text; path = GIT.txt; sourceTree = "<group>"; };
FA85178A0FA061EC006A1F5A /* op.h */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.h; path = op.h; sourceTree = "<group>"; };
FA85178B0FA061EC006A1F5A /* op.c */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.c; path = op.c; sourceTree = "<group>"; };
FA99428A10E82A27007F27ED /* proc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = proc.h; sourceTree = "<group>"; };
FA99428B10E82A27007F27ED /* proc.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = proc.c; sourceTree = "<group>"; };
FAA3D2700F139CB300B2447E /* invite-test.e */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = text; path = "invite-test.e"; sourceTree = "<group>"; };
FAA3D2710F139CB300B2447E /* join-test.e */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = text; path = "join-test.e"; sourceTree = "<group>"; };
FAA3D2720F139CB300B2447E /* kick-test.e */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = text; path = "kick-test.e"; sourceTree = "<group>"; };
@ -339,6 +342,8 @@
FA85178B0FA061EC006A1F5A /* op.c */,
FA322D080CEF74B1001761B3 /* parse.c */,
FA322D090CEF74B1001761B3 /* parse.h */,
FA99428B10E82A27007F27ED /* proc.c */,
FA99428A10E82A27007F27ED /* proc.h */,
FA322D0A0CEF74B1001761B3 /* rendezvous.c */,
FA322D0B0CEF74B1001761B3 /* rendezvous.h */,
FA322D0C0CEF74B1001761B3 /* resolve.c */,
@ -682,6 +687,7 @@
FA407F2E0DB159F400271AF1 /* ng_ipaddr.c in Sources */,
FAA3D27B0F139CDC00B2447E /* conn-ssl.c in Sources */,
FA85178C0FA061EC006A1F5A /* op.c in Sources */,
FA99428C10E82A27007F27ED /* proc.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -1,6 +1,6 @@
#
# ngIRCd -- The Next Generation IRC Daemon
# Copyright (c)2001-2003 by Alexander Barton (alex@barton.de)
# Copyright (c)2001-2010 Alexander Barton (alex@barton.de)
#
# 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
@ -8,8 +8,6 @@
# (at your option) any later version.
# Please read the file COPYING, README and AUTHORS for more information.
#
# $Id: Makefile.am,v 1.51 2008/02/26 22:04:17 fw Exp $
#
AUTOMAKE_OPTIONS = ../portab/ansi2knr
@ -23,7 +21,7 @@ sbin_PROGRAMS = ngircd
ngircd_SOURCES = ngircd.c array.c channel.c client.c conf.c conn.c conn-func.c \
conn-ssl.c conn-zip.c hash.c io.c irc.c irc-channel.c irc-info.c irc-login.c \
irc-mode.c irc-op.c irc-oper.c irc-server.c irc-write.c lists.c log.c \
match.c op.c numeric.c parse.c rendezvous.c resolve.c
match.c op.c numeric.c parse.c proc.c rendezvous.c resolve.c
ngircd_LDFLAGS = -L../portab -L../tool -L../ipaddr
@ -32,8 +30,8 @@ ngircd_LDADD = -lngportab -lngtool -lngipaddr
noinst_HEADERS = ngircd.h array.h channel.h client.h conf.h conf-ssl.h conn.h \
conn-func.h conn-ssl.h conn-zip.h hash.h io.h irc.h irc-channel.h \
irc-info.h irc-login.h irc-mode.h irc-op.h irc-oper.h irc-server.h \
irc-write.h lists.h log.h match.h numeric.h op.h parse.h rendezvous.h \
resolve.h defines.h messages.h
irc-write.h lists.h log.h match.h numeric.h op.h parse.h proc.h \
rendezvous.h resolve.h defines.h messages.h
clean-local:
rm -f check-version check-help lint.out

View File

@ -1481,7 +1481,7 @@ Init_Server_Struct( CONF_SERVER *Server )
if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
Resolve_Init(&Server->res_stat);
Proc_InitStruct(&Server->res_stat);
Server->conn_id = NONE;
memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
} /* Init_Server_Struct */

View File

@ -22,7 +22,7 @@
#include "portab.h"
#include "tool.h"
#include "ng_ipaddr.h"
#include "resolve.h"
#include "proc.h"
#include "conf-ssl.h"
@ -41,7 +41,7 @@ typedef struct _Conf_Server
UINT16 port; /* Server port */
int group; /* Group of server */
time_t lasttry; /* Last connect attempt */
RES_STAT res_stat; /* Status of the resolver */
PROC_STAT res_stat; /* Status of the resolver */
int flags; /* Flags */
CONN_ID conn_id; /* ID of server connection or NONE */
ng_ipaddr_t bind_addr; /* source address to use for outgoing

View File

@ -21,9 +21,9 @@
#include "imp.h"
#include <assert.h>
#ifdef PROTOTYPES
# include <stdarg.h>
# include <stdarg.h>
#else
# include <varargs.h>
# include <varargs.h>
#endif
#include <stdio.h>
#include <stdlib.h>
@ -53,20 +53,23 @@
#include "array.h"
#include "defines.h"
#include "resolve.h"
#include "exp.h"
#include "conn.h"
#include "imp.h"
#include "ngircd.h"
#include "array.h"
#include "client.h"
#include "conf.h"
#include "conn-ssl.h"
#include "conn-zip.h"
#include "conn-func.h"
#include "log.h"
#include "ng_ipaddr.h"
#include "parse.h"
#include "proc.h"
#include "resolve.h"
#include "tool.h"
#ifdef ZEROCONF
@ -752,7 +755,7 @@ Conn_Handler(void)
if (SSL_WantWrite(&My_Connections[i]))
continue; /* TLS/SSL layer needs to write data; deal with this first */
#endif
if (Resolve_INPROGRESS(&My_Connections[i].res_stat)) {
if (Proc_InProgress(&My_Connections[i].res_stat)) {
/* Wait for completion of resolver sub-process ... */
io_event_del(My_Connections[i].sock,
IO_WANTREAD);
@ -1071,8 +1074,8 @@ Conn_Close( CONN_ID Idx, const char *LogMsg, const char *FwdMsg, bool InformClie
}
/* cancel running resolver */
if (Resolve_INPROGRESS(&My_Connections[Idx].res_stat))
Resolve_Shutdown(&My_Connections[Idx].res_stat);
if (Proc_InProgress(&My_Connections[Idx].res_stat))
Proc_Kill(&My_Connections[Idx].res_stat);
/* Servers: Modify time of next connect attempt? */
Conf_UnsetServer( Idx );
@ -1763,7 +1766,7 @@ Check_Servers( void )
/* Okay, try to connect now */
Conf_Server[i].lasttry = time_now;
Conf_Server[i].conn_id = SERVER_WAIT;
assert(Resolve_Getfd(&Conf_Server[i].res_stat) < 0);
assert(Proc_GetPipeFd(&Conf_Server[i].res_stat) < 0);
Resolve_Name(&Conf_Server[i].res_stat, Conf_Server[i].host, cb_Connect_to_Server);
}
} /* Check_Servers */
@ -1886,7 +1889,7 @@ Init_Conn_Struct(CONN_ID Idx)
My_Connections[Idx].signon = now;
My_Connections[Idx].lastdata = now;
My_Connections[Idx].lastprivmsg = now;
Resolve_Init(&My_Connections[Idx].res_stat);
Proc_InitStruct(&My_Connections[Idx].res_stat);
} /* Init_Conn_Struct */
@ -1941,7 +1944,7 @@ cb_Connect_to_Server(int fd, UNUSED short events)
LogDebug("Resolver: Got forward lookup callback on fd %d, events %d", fd, events);
for (i=0; i < MAX_SERVERS; i++) {
if (Resolve_Getfd(&Conf_Server[i].res_stat) == fd )
if (Proc_GetPipeFd(&Conf_Server[i].res_stat) == fd )
break;
}
@ -2000,7 +2003,7 @@ cb_Read_Resolver_Result( int r_fd, UNUSED short events )
/* Search associated connection ... */
for( i = 0; i < Pool_Size; i++ ) {
if(( My_Connections[i].sock != NONE )
&& ( Resolve_Getfd(&My_Connections[i].res_stat) == r_fd ))
&& (Proc_GetPipeFd(&My_Connections[i].res_stat) == r_fd))
break;
}
if( i >= Pool_Size ) {

View File

@ -48,7 +48,7 @@ typedef long CONN_ID;
#ifdef CONN_MODULE
#include "defines.h"
#include "resolve.h"
#include "proc.h"
#include "array.h"
#include "tool.h"
#include "ng_ipaddr.h"
@ -69,7 +69,7 @@ typedef struct _Connection
{
int sock; /* Socket handle */
ng_ipaddr_t addr; /* Client address */
RES_STAT res_stat; /* Status of resolver process */
PROC_STAT res_stat; /* Status of resolver process */
char host[HOST_LEN]; /* Hostname */
array rbuf; /* Read buffer */
array wbuf; /* Write buffer */

View File

@ -104,8 +104,7 @@ static bool io_event_change_devpoll(int fd, short what);
#ifdef IO_USE_SELECT
#include "defines.h" /* for conn.h */
#include "conn.h" /* for CONN_IDX (needed by resolve.h) */
#include "resolve.h" /* for RES_STAT (needed by conf.h) */
#include "proc.h" /* for PROC_STAT (needed by conf.h) */
#include "conf.h" /* for Conf_MaxConnections */
static fd_set readers;

102
src/ngircd/proc.c Normal file
View File

@ -0,0 +1,102 @@
/*
* ngIRCd -- The Next Generation IRC Daemon
* Copyright (c)2001-2010 Alexander Barton (alex@barton.de)
*
* 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.
* Please read the file COPYING, README and AUTHORS for more information.
*
* Process management
*/
#include "portab.h"
#include "imp.h"
#include <assert.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include "log.h"
#include "io.h"
#include "exp.h"
#include "proc.h"
/**
* Initialize process structure.
*/
GLOBAL void
Proc_InitStruct (PROC_STAT *proc)
{
assert(proc != NULL);
proc->pid = 0;
proc->pipe_fd = -1;
}
/**
* Fork a child process.
*/
GLOBAL pid_t
Proc_Fork(PROC_STAT *proc, int *pipefds, void (*cbfunc)(int, short))
{
pid_t pid;
assert(proc != NULL);
assert(pipefds != NULL);
assert(cbfunc != NULL);
if (pipe(pipefds) != 0) {
Log(LOG_ALERT, "Can't create output pipe for child process: %s!",
strerror(errno));
return -1;
}
pid = fork();
switch (pid) {
case -1:
/* Error on fork: */
Log(LOG_CRIT, "Can't fork child process: %s!", strerror(errno));
close(pipefds[0]);
close(pipefds[1]);
return -1;
case 0:
/* New child process: */
close(pipefds[0]);
return 0;
}
/* Old parent process: */
close(pipefds[1]);
if (!io_setnonblock(pipefds[0])
|| !io_event_create(pipefds[0], IO_WANTREAD, cbfunc)) {
Log(LOG_CRIT, "Can't register callback for child process: %s!",
strerror(errno));
close(pipefds[0]);
return -1;
}
proc->pid = pid;
proc->pipe_fd = pipefds[0];
return pid;
}
/**
* Kill forked child process.
*/
GLOBAL void
Proc_Kill(PROC_STAT *proc)
{
assert(proc != NULL);
assert(proc->pipe_fd >= 0);
io_close(proc->pipe_fd);
kill(proc->pid, SIGTERM);
Proc_InitStruct(proc);
}
/* -eof- */

35
src/ngircd/proc.h Normal file
View File

@ -0,0 +1,35 @@
/*
* ngIRCd -- The Next Generation IRC Daemon
* Copyright (c)2001-2010 Alexander Barton (alex@barton.de)
*
* 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.
* Please read the file COPYING, README and AUTHORS for more information.
*
* Process management (header)
*/
#ifndef __proc_h__
#define __proc_h__
/* This struct must not be accessed directly! */
typedef struct _Proc_Stat {
pid_t pid; /* PID of the child process or 0 if none */
int pipe_fd; /* Pipe file descriptor or -1 if none */
} PROC_STAT;
#define Proc_InProgress(x) ((x)->pid != 0)
#define Proc_GetPipeFd(x) ((x)->pipe_fd)
GLOBAL void Proc_InitStruct PARAMS((PROC_STAT *proc));
GLOBAL pid_t Proc_Fork PARAMS((PROC_STAT *proc, int *pipefds,
void (*cbfunc)(int, short)));
GLOBAL void Proc_Kill PARAMS((PROC_STAT *proc));
#endif
/* -eof- */

View File

@ -30,9 +30,12 @@
#endif
#endif
#include "array.h"
#include "conn.h"
#include "defines.h"
#include "log.h"
#include "ng_ipaddr.h"
#include "proc.h"
#include "exp.h"
#include "resolve.h"
@ -41,46 +44,18 @@
static void Do_ResolveAddr PARAMS(( const ng_ipaddr_t *Addr, int Sock, int w_fd ));
static void Do_ResolveName PARAMS(( const char *Host, int w_fd ));
static bool register_callback PARAMS((RES_STAT *s, void (*cbfunc)(int, short)));
#ifdef WANT_IPV6
extern bool Conf_ConnectIPv4;
extern bool Conf_ConnectIPv6;
#endif
static pid_t
Resolver_fork(int *pipefds)
{
pid_t pid;
if (pipe(pipefds) != 0) {
Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
return -1;
}
pid = fork();
switch(pid) {
case -1:
Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
close(pipefds[0]);
close(pipefds[1]);
return -1;
case 0: /* child */
close(pipefds[0]);
Log_Init_Resolver( );
return 0;
}
/* parent */
close(pipefds[1]);
return pid;
}
/**
* Resolve IP (asynchronous!).
*/
GLOBAL bool
Resolve_Addr(RES_STAT * s, const ng_ipaddr_t *Addr, int identsock,
Resolve_Addr(PROC_STAT * s, const ng_ipaddr_t *Addr, int identsock,
void (*cbfunc) (int, short))
{
int pipefd[2];
@ -88,15 +63,13 @@ Resolve_Addr(RES_STAT * s, const ng_ipaddr_t *Addr, int identsock,
assert(s != NULL);
pid = Resolver_fork(pipefd);
pid = Proc_Fork(s, pipefd, cbfunc);
if (pid > 0) {
LogDebug("Resolver for %s created (PID %d).", ng_ipaddr_tostr(Addr), pid);
s->pid = pid;
s->resolver_fd = pipefd[0];
return register_callback(s, cbfunc);
return true;
} else if( pid == 0 ) {
/* Sub process */
Log_Init_Resolver();
Do_ResolveAddr( Addr, identsock, pipefd[1]);
Log_Exit_Resolver( );
exit(0);
@ -109,24 +82,23 @@ Resolve_Addr(RES_STAT * s, const ng_ipaddr_t *Addr, int identsock,
* Resolve hostname (asynchronous!).
*/
GLOBAL bool
Resolve_Name( RES_STAT *s, const char *Host, void (*cbfunc)(int, short))
Resolve_Name( PROC_STAT *s, const char *Host, void (*cbfunc)(int, short))
{
int pipefd[2];
pid_t pid;
assert(s != NULL);
pid = Resolver_fork(pipefd);
pid = Proc_Fork(s, pipefd, cbfunc);
if (pid > 0) {
/* Main process */
#ifdef DEBUG
Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
#endif
s->pid = pid;
s->resolver_fd = pipefd[0];
return register_callback(s, cbfunc);
return true;
} else if( pid == 0 ) {
/* Sub process */
Log_Init_Resolver();
Do_ResolveName(Host, pipefd[1]);
Log_Exit_Resolver( );
exit(0);
@ -135,15 +107,6 @@ Resolve_Name( RES_STAT *s, const char *Host, void (*cbfunc)(int, short))
} /* Resolve_Name */
GLOBAL void
Resolve_Init(RES_STAT *s)
{
assert(s != NULL);
s->resolver_fd = -1;
s->pid = 0;
}
#if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETNAMEINFO)
#if !defined(WANT_IPV6) && defined(h_errno)
static char *
@ -482,52 +445,18 @@ Do_ResolveName( const char *Host, int w_fd )
} /* Do_ResolveName */
static bool
register_callback( RES_STAT *s, void (*cbfunc)(int, short))
{
assert(cbfunc != NULL);
assert(s != NULL);
assert(s->resolver_fd >= 0);
if (io_setnonblock(s->resolver_fd) &&
io_event_create(s->resolver_fd, IO_WANTREAD, cbfunc))
return true;
Log( LOG_CRIT, "Resolver: Could not register callback function: %s!", strerror(errno));
close(s->resolver_fd);
Resolve_Init(s);
return false;
}
GLOBAL bool
Resolve_Shutdown( RES_STAT *s)
{
bool ret = false;
assert(s != NULL);
assert(s->resolver_fd >= 0);
if (s->resolver_fd >= 0)
ret = io_close(s->resolver_fd);
Resolve_Init(s);
return ret;
}
/**
* Read result of resolver sub-process from pipe
*/
GLOBAL size_t
Resolve_Read( RES_STAT *s, void* readbuf, size_t buflen)
Resolve_Read( PROC_STAT *s, void* readbuf, size_t buflen)
{
ssize_t bytes_read;
assert(buflen > 0);
/* Read result from pipe */
bytes_read = read(s->resolver_fd, readbuf, buflen);
bytes_read = read(Proc_GetPipeFd(s), readbuf, buflen);
if (bytes_read < 0) {
if (errno == EAGAIN)
return 0;
@ -539,7 +468,7 @@ Resolve_Read( RES_STAT *s, void* readbuf, size_t buflen)
else if (bytes_read == 0)
Log( LOG_DEBUG, "Resolver: Can't read result: EOF");
#endif
Resolve_Shutdown(s);
Proc_Kill(s);
return (size_t)bytes_read;
}

View File

@ -1,6 +1,6 @@
/*
* ngIRCd -- The Next Generation IRC Daemon
* Copyright (c)2001-2003 by Alexander Barton (alex@barton.de)
* Copyright (c)2001-2010 by Alexander Barton (alex@barton.de)
*
* 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
@ -8,34 +8,18 @@
* (at your option) any later version.
* Please read the file COPYING, README and AUTHORS for more information.
*
* $Id: resolve.h,v 1.14 2008/02/26 22:04:17 fw Exp $
*
* Asynchronous resolver (header)
*/
#ifndef __resolve_h__
#define __resolve_h__
#include "array.h"
#include "tool.h"
#include "ng_ipaddr.h"
/* This struct must not be accessed directly */
typedef struct _Res_Stat {
pid_t pid; /* PID of resolver process */
int resolver_fd; /* pipe fd for lookup result. */
} RES_STAT;
#define Resolve_Getfd(x) ((x)->resolver_fd)
#define Resolve_INPROGRESS(x) ((x)->resolver_fd >= 0)
GLOBAL bool Resolve_Addr PARAMS(( RES_STAT *s, const ng_ipaddr_t *Addr, int identsock, void (*cbfunc)(int, short)));
GLOBAL bool Resolve_Name PARAMS(( RES_STAT *s, const char *Host, void (*cbfunc)(int, short) ));
GLOBAL size_t Resolve_Read PARAMS(( RES_STAT *s, void *buf, size_t buflen));
GLOBAL void Resolve_Init PARAMS(( RES_STAT *s));
GLOBAL bool Resolve_Shutdown PARAMS(( RES_STAT *s));
GLOBAL bool Resolve_Addr PARAMS((PROC_STAT * s, const ng_ipaddr_t * Addr,
int identsock, void (*cbfunc) (int, short)));
GLOBAL bool Resolve_Name PARAMS((PROC_STAT * s, const char *Host,
void (*cbfunc) (int, short)));
GLOBAL size_t Resolve_Read PARAMS((PROC_STAT * s, void *buf, size_t buflen));
#endif
/* -eof- */