base
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
Revision 0386
|
||||
-------------------
|
||||
수정일 : 2010-10-21
|
||||
수정자 : 유희곤
|
||||
|
||||
- NEW: SVN 신규 등록
|
||||
- KT, X-CDN 에 적용된 neo_statusd 최신 소스를 SVN 상에 등록함.
|
||||
- 본 소스는 김석환 주임이 2010-10-21 X-CDN 에 적용한 마지막 소스임.
|
||||
- 본 소스는 기존 svn://211.38.137.33/old/gms/neo_statusd 에 등록된 소스에 대해
|
||||
SMS 전송 관련 부분을 신동호 부장 요청으로 김석환 주임이 추가 수정한 소스임.
|
||||
수정 부분은 check_neocast.c 파일의 297 라인이며 09/12/31 로 검색하여 확인 가능함.
|
||||
- 본 소스는 SMS 전송 관련 부분이 최종 적용한 X-CDN 으로 하드코딩 되어 있음.
|
||||
|
||||
- NEW: conf 파일 추가
|
||||
- X-CDN 에 적용된 마지막 neo_statusd.conf 파일을 src 폴더에 추가함.
|
||||
|
||||
- NEW: Changelog.txt 파일을 추가
|
||||
- 앞으로 각 작업에 대한 로그를 확인하도록 Changelog.txt 파일을 추가
|
||||
|
||||
- CHG:
|
||||
- BUG:
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef __CFGCONFIG_H__
|
||||
#define __CFGCONFIG_H__
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define MAX_BUF 1024
|
||||
|
||||
long Str2Dec(char *);
|
||||
char* makeToken(char *, char *);
|
||||
int ReadProfile(char *, char *);
|
||||
void SetIniName(char *);
|
||||
int GetProfileInt(char *, int);
|
||||
char *GetProfile(char *, char *, char *);
|
||||
char cfgname[60];
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef __CHECK_NEOCAST_H__
|
||||
#define __CHECK_NEOCAST_H__
|
||||
|
||||
#define CFGFILE "/user/service/etc/neo_statusd.conf"
|
||||
#define DEFAULT_LOGFILE_PATH "/user/service/log/neo_statusd/"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <mysql.h>
|
||||
|
||||
void safe_wait();
|
||||
void loop_check();
|
||||
int check_neocast(time_t, unsigned long *, unsigned long *);
|
||||
void SendToWebServ(const char *, FILE *);
|
||||
char *PostDataEncode(const char *);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,406 @@
|
||||
/*
|
||||
* Copyright (C) 2004-2008 Internet Systems Consortium, Inc. ("ISC")
|
||||
* Copyright (C) 2000-2003 Internet Software Consortium.
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
|
||||
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
||||
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
* PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/* $Id: dig.h,v 1.105.130.2 2008/04/03 06:08:26 tbox Exp $ */
|
||||
|
||||
#ifndef DIG_H
|
||||
#define DIG_H
|
||||
|
||||
/*! \file */
|
||||
|
||||
#include <dns/rdatalist.h>
|
||||
|
||||
#include <dst/dst.h>
|
||||
|
||||
#include <isc/boolean.h>
|
||||
#include <isc/buffer.h>
|
||||
#include <isc/bufferlist.h>
|
||||
#include <isc/formatcheck.h>
|
||||
#include <isc/lang.h>
|
||||
#include <isc/list.h>
|
||||
#include <isc/mem.h>
|
||||
#include <isc/print.h>
|
||||
#include <isc/sockaddr.h>
|
||||
#include <isc/socket.h>
|
||||
|
||||
#define MXSERV 20
|
||||
#define MXNAME (DNS_NAME_MAXTEXT+1)
|
||||
#define MXRD 32
|
||||
/*% Buffer Size */
|
||||
#define BUFSIZE 512
|
||||
#define COMMSIZE 0xffff
|
||||
#ifndef RESOLV_CONF
|
||||
/*% location of resolve.conf */
|
||||
#define RESOLV_CONF "/etc/resolv.conf"
|
||||
#endif
|
||||
/*% output buffer */
|
||||
#define OUTPUTBUF 32767
|
||||
/*% Max RR Limit */
|
||||
#define MAXRRLIMIT 0xffffffff
|
||||
#define MAXTIMEOUT 0xffff
|
||||
/*% Max number of tries */
|
||||
#define MAXTRIES 0xffffffff
|
||||
/*% Max number of dots */
|
||||
#define MAXNDOTS 0xffff
|
||||
/*% Max number of ports */
|
||||
#define MAXPORT 0xffff
|
||||
/*% Max serial number */
|
||||
#define MAXSERIAL 0xffffffff
|
||||
|
||||
/*% Default TCP Timeout */
|
||||
#define TCP_TIMEOUT 10
|
||||
/*% Default UDP Timeout */
|
||||
#define UDP_TIMEOUT 5
|
||||
|
||||
#define SERVER_TIMEOUT 1
|
||||
|
||||
#define LOOKUP_LIMIT 64
|
||||
/*%
|
||||
* Lookup_limit is just a limiter, keeping too many lookups from being
|
||||
* created. It's job is mainly to prevent the program from running away
|
||||
* in a tight loop of constant lookups. It's value is arbitrary.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Defaults for the sigchase suboptions. Consolidated here because
|
||||
* these control the layout of dig_lookup_t (among other things).
|
||||
*/
|
||||
#ifdef DIG_SIGCHASE
|
||||
#ifndef DIG_SIGCHASE_BU
|
||||
#define DIG_SIGCHASE_BU 1
|
||||
#endif
|
||||
#ifndef DIG_SIGCHASE_TD
|
||||
#define DIG_SIGCHASE_TD 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
ISC_LANG_BEGINDECLS
|
||||
|
||||
typedef struct dig_lookup dig_lookup_t;
|
||||
typedef struct dig_query dig_query_t;
|
||||
typedef struct dig_server dig_server_t;
|
||||
#ifdef DIG_SIGCHASE
|
||||
typedef struct dig_message dig_message_t;
|
||||
#endif
|
||||
typedef ISC_LIST(dig_server_t) dig_serverlist_t;
|
||||
typedef struct dig_searchlist dig_searchlist_t;
|
||||
|
||||
/*% The dig_lookup structure */
|
||||
struct dig_lookup {
|
||||
isc_boolean_t
|
||||
pending, /*%< Pending a successful answer */
|
||||
waiting_connect,
|
||||
doing_xfr,
|
||||
ns_search_only, /*%< dig +nssearch, host -C */
|
||||
identify, /*%< Append an "on server <foo>" message */
|
||||
identify_previous_line, /*% Prepend a "Nameserver <foo>:"
|
||||
message, with newline and tab */
|
||||
ignore,
|
||||
recurse,
|
||||
aaonly,
|
||||
adflag,
|
||||
cdflag,
|
||||
trace, /*% dig +trace */
|
||||
trace_root, /*% initial query for either +trace or +nssearch */
|
||||
tcp_mode,
|
||||
ip6_int,
|
||||
comments,
|
||||
stats,
|
||||
section_question,
|
||||
section_answer,
|
||||
section_authority,
|
||||
section_additional,
|
||||
servfail_stops,
|
||||
new_search,
|
||||
need_search,
|
||||
done_as_is,
|
||||
besteffort,
|
||||
dnssec,
|
||||
nsid; /*% Name Server ID (RFC 5001) */
|
||||
#ifdef DIG_SIGCHASE
|
||||
isc_boolean_t sigchase;
|
||||
#if DIG_SIGCHASE_TD
|
||||
isc_boolean_t do_topdown,
|
||||
trace_root_sigchase,
|
||||
rdtype_sigchaseset,
|
||||
rdclass_sigchaseset;
|
||||
/* Name we are going to validate RRset */
|
||||
char textnamesigchase[MXNAME];
|
||||
#endif
|
||||
#endif
|
||||
|
||||
char textname[MXNAME]; /*% Name we're going to be looking up */
|
||||
char cmdline[MXNAME];
|
||||
dns_rdatatype_t rdtype;
|
||||
dns_rdatatype_t qrdtype;
|
||||
#if DIG_SIGCHASE_TD
|
||||
dns_rdatatype_t rdtype_sigchase;
|
||||
dns_rdatatype_t qrdtype_sigchase;
|
||||
dns_rdataclass_t rdclass_sigchase;
|
||||
#endif
|
||||
dns_rdataclass_t rdclass;
|
||||
isc_boolean_t rdtypeset;
|
||||
isc_boolean_t rdclassset;
|
||||
char namespace[BUFSIZE];
|
||||
char onamespace[BUFSIZE];
|
||||
isc_buffer_t namebuf;
|
||||
isc_buffer_t onamebuf;
|
||||
isc_buffer_t renderbuf;
|
||||
char *sendspace;
|
||||
dns_name_t *name;
|
||||
isc_timer_t *timer;
|
||||
isc_interval_t interval;
|
||||
dns_message_t *sendmsg;
|
||||
dns_name_t *oname;
|
||||
ISC_LINK(dig_lookup_t) link;
|
||||
ISC_LIST(dig_query_t) q;
|
||||
dig_query_t *current_query;
|
||||
dig_serverlist_t my_server_list;
|
||||
dig_searchlist_t *origin;
|
||||
dig_query_t *xfr_q;
|
||||
isc_uint32_t retries;
|
||||
int nsfound;
|
||||
isc_uint16_t udpsize;
|
||||
isc_int16_t edns;
|
||||
isc_uint32_t ixfr_serial;
|
||||
isc_buffer_t rdatabuf;
|
||||
char rdatastore[MXNAME];
|
||||
dst_context_t *tsigctx;
|
||||
isc_buffer_t *querysig;
|
||||
isc_uint32_t msgcounter;
|
||||
dns_fixedname_t fdomain;
|
||||
};
|
||||
|
||||
/*% The dig_query structure */
|
||||
struct dig_query {
|
||||
dig_lookup_t *lookup;
|
||||
isc_boolean_t waiting_connect,
|
||||
pending_free,
|
||||
waiting_senddone,
|
||||
first_pass,
|
||||
first_soa_rcvd,
|
||||
second_rr_rcvd,
|
||||
first_repeat_rcvd,
|
||||
recv_made,
|
||||
warn_id;
|
||||
isc_uint32_t first_rr_serial;
|
||||
isc_uint32_t second_rr_serial;
|
||||
isc_uint32_t msg_count;
|
||||
isc_uint32_t rr_count;
|
||||
char *servname;
|
||||
char *userarg;
|
||||
isc_bufferlist_t sendlist,
|
||||
recvlist,
|
||||
lengthlist;
|
||||
isc_buffer_t recvbuf,
|
||||
lengthbuf,
|
||||
slbuf;
|
||||
char *recvspace,
|
||||
lengthspace[4],
|
||||
slspace[4];
|
||||
isc_socket_t *sock;
|
||||
ISC_LINK(dig_query_t) link;
|
||||
isc_sockaddr_t sockaddr;
|
||||
isc_time_t time_sent;
|
||||
isc_uint64_t byte_count;
|
||||
isc_buffer_t sendbuf;
|
||||
};
|
||||
|
||||
struct dig_server {
|
||||
char servername[MXNAME];
|
||||
char userarg[MXNAME];
|
||||
ISC_LINK(dig_server_t) link;
|
||||
};
|
||||
|
||||
struct dig_searchlist {
|
||||
char origin[MXNAME];
|
||||
ISC_LINK(dig_searchlist_t) link;
|
||||
};
|
||||
#ifdef DIG_SIGCHASE
|
||||
struct dig_message {
|
||||
dns_message_t *msg;
|
||||
ISC_LINK(dig_message_t) link;
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef ISC_LIST(dig_searchlist_t) dig_searchlistlist_t;
|
||||
typedef ISC_LIST(dig_lookup_t) dig_lookuplist_t;
|
||||
|
||||
/*
|
||||
* Externals from dighost.c
|
||||
*/
|
||||
|
||||
extern dig_lookuplist_t lookup_list;
|
||||
extern dig_serverlist_t server_list;
|
||||
extern dig_searchlistlist_t search_list;
|
||||
extern unsigned int extrabytes;
|
||||
|
||||
extern isc_boolean_t check_ra, have_ipv4, have_ipv6, specified_source,
|
||||
usesearch, showsearch, qr;
|
||||
extern in_port_t port;
|
||||
extern unsigned int timeout;
|
||||
extern isc_mem_t *mctx;
|
||||
extern dns_messageid_t id;
|
||||
extern int sendcount;
|
||||
extern int ndots;
|
||||
extern int lookup_counter;
|
||||
extern int exitcode;
|
||||
extern isc_sockaddr_t bind_address;
|
||||
extern char keynametext[MXNAME];
|
||||
extern char keyfile[MXNAME];
|
||||
extern char keysecret[MXNAME];
|
||||
extern dns_name_t *hmacname;
|
||||
extern unsigned int digestbits;
|
||||
#ifdef DIG_SIGCHASE
|
||||
extern char trustedkey[MXNAME];
|
||||
#endif
|
||||
extern dns_tsigkey_t *key;
|
||||
extern isc_boolean_t validated;
|
||||
extern isc_taskmgr_t *taskmgr;
|
||||
extern isc_task_t *global_task;
|
||||
extern isc_boolean_t free_now;
|
||||
extern isc_boolean_t debugging, memdebugging;
|
||||
|
||||
extern char *progname;
|
||||
extern int tries;
|
||||
extern int fatalexit;
|
||||
#ifdef WITH_IDN
|
||||
extern int idnoptions;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Routines in dighost.c.
|
||||
*/
|
||||
void
|
||||
get_address(char *host, in_port_t port, isc_sockaddr_t *sockaddr);
|
||||
|
||||
isc_result_t
|
||||
get_reverse(char *reverse, size_t len, char *value, isc_boolean_t ip6_int,
|
||||
isc_boolean_t strict);
|
||||
|
||||
void
|
||||
fatal(const char *format, ...) ISC_FORMAT_PRINTF(1, 2);
|
||||
|
||||
void
|
||||
debug(const char *format, ...) ISC_FORMAT_PRINTF(1, 2);
|
||||
|
||||
void
|
||||
check_result(isc_result_t result, const char *msg);
|
||||
|
||||
void
|
||||
setup_lookup(dig_lookup_t *lookup);
|
||||
|
||||
void
|
||||
destroy_lookup(dig_lookup_t *lookup);
|
||||
|
||||
void
|
||||
do_lookup(dig_lookup_t *lookup);
|
||||
|
||||
void
|
||||
start_lookup(void);
|
||||
|
||||
void
|
||||
onrun_callback(isc_task_t *task, isc_event_t *event);
|
||||
|
||||
int
|
||||
dhmain(int argc, char **argv);
|
||||
|
||||
void
|
||||
setup_libs(void);
|
||||
|
||||
void
|
||||
setup_system(void);
|
||||
|
||||
dig_lookup_t *
|
||||
requeue_lookup(dig_lookup_t *lookold, isc_boolean_t servers);
|
||||
|
||||
dig_lookup_t *
|
||||
make_empty_lookup(void);
|
||||
|
||||
dig_lookup_t *
|
||||
clone_lookup(dig_lookup_t *lookold, isc_boolean_t servers);
|
||||
|
||||
dig_server_t *
|
||||
make_server(const char *servname, const char *userarg);
|
||||
|
||||
void
|
||||
flush_server_list(void);
|
||||
|
||||
void
|
||||
set_nameserver(char *opt);
|
||||
|
||||
void
|
||||
clone_server_list(dig_serverlist_t src,
|
||||
dig_serverlist_t *dest);
|
||||
|
||||
void
|
||||
cancel_all(void);
|
||||
|
||||
void
|
||||
destroy_libs(void);
|
||||
|
||||
void
|
||||
set_search_domain(char *domain);
|
||||
|
||||
#ifdef DIG_SIGCHASE
|
||||
void
|
||||
clean_trustedkey(void);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Routines to be defined in dig.c, host.c, and nslookup.c.
|
||||
*/
|
||||
#ifdef DIG_SIGCHASE
|
||||
isc_result_t
|
||||
printrdataset(dns_name_t *owner_name, dns_rdataset_t *rdataset,
|
||||
isc_buffer_t *target);
|
||||
#endif
|
||||
|
||||
isc_result_t
|
||||
printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers);
|
||||
/*%<
|
||||
* Print the final result of the lookup.
|
||||
*/
|
||||
|
||||
void
|
||||
received(int bytes, isc_sockaddr_t *from, dig_query_t *query);
|
||||
/*%<
|
||||
* Print a message about where and when the response
|
||||
* was received from, like the final comment in the
|
||||
* output of "dig".
|
||||
*/
|
||||
|
||||
void
|
||||
trying(char *frm, dig_lookup_t *lookup);
|
||||
|
||||
void
|
||||
dighost_shutdown(void);
|
||||
|
||||
char *
|
||||
next_token(char **stringp, const char *delim);
|
||||
|
||||
#ifdef DIG_SIGCHASE
|
||||
/* Chasing functions */
|
||||
dns_rdataset_t *
|
||||
chase_scanname(dns_name_t *name, dns_rdatatype_t type, dns_rdatatype_t covers);
|
||||
void
|
||||
chase_sig(dns_message_t *msg);
|
||||
#endif
|
||||
|
||||
ISC_LANG_ENDDECLS
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,36 @@
|
||||
#ifndef __PROTOCOL_H__
|
||||
#define __PROTOCOL_H__
|
||||
|
||||
#define MaxSizeOfServerName 32
|
||||
#define MaxSizeOfServerAddr 64
|
||||
#define MaxSizeOfServerIp 64
|
||||
#define MaxSizeOfServerPort 8
|
||||
#define MaxSizeOfServerType 24
|
||||
#define MaxSizeOfsmsStr 1024
|
||||
|
||||
#define MAX_ALLOWED_DEAD_COUNT 1
|
||||
#define ALLOWED_MAX_ERROR_COUNT 5
|
||||
#define SENDSMSTERM 5
|
||||
#define MAXSENDSMSCOUNT 5
|
||||
#define SERV_ALIVE 1
|
||||
#define SERV_DEAD 2
|
||||
#define NO_MONITOR 3
|
||||
|
||||
#define MYQUERY "SELECT neo_seq, name, domain, dig_domain, ip, type, port, dead_count, status FROM t_neo_server_index WHERE monitor_yn ='Y' AND deleted_yn = 'N' ORDER BY neo_type ASC"
|
||||
|
||||
static int neo_serv_num = 0;
|
||||
|
||||
typedef struct _neo_serv_info {
|
||||
long neo_seq;
|
||||
char name[MaxSizeOfServerName];
|
||||
char domain[MaxSizeOfServerAddr];
|
||||
char dig_domain[MaxSizeOfServerAddr];
|
||||
char ipaddr[MaxSizeOfServerIp];
|
||||
char type[MaxSizeOfServerType];
|
||||
int port;
|
||||
int dead_count;
|
||||
int status;
|
||||
struct _neo_serv_info *next;
|
||||
} neo_serv_info;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifndef __UTIL_H__
|
||||
#define __UTIL_H__
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int devide(char **val, char *str, char delim);
|
||||
char* timeToString(struct tm *t);
|
||||
char *makeLogString();
|
||||
void WriteSHLog(char *msg, FILE *ifp);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,43 @@
|
||||
#library directory
|
||||
LIBDIR = -L/usr/local/lib -L/usr/lib\
|
||||
-lcom
|
||||
|
||||
INCLUDEDIR = -I./include\
|
||||
-I/usr/local/include
|
||||
|
||||
CC = gcc
|
||||
|
||||
CFLAGS = -g -Wall
|
||||
#CFLAGS = -O -Wall
|
||||
#link option
|
||||
LFLAGS =
|
||||
|
||||
#Definition flag
|
||||
#DFLAGS = -D__DEBUG__
|
||||
#DFLAGS = -D__RELEASE__
|
||||
|
||||
#install directory
|
||||
INSTALLDIR = ../../../lib/
|
||||
|
||||
# Source List
|
||||
SRC = ReadConfig.c cfgconfig.h
|
||||
|
||||
# Object List
|
||||
OBJ = ReadConfig.o
|
||||
|
||||
TARGET = libreadconfig.a
|
||||
|
||||
.SUFFIXES: .c .o .h
|
||||
|
||||
.c.o : ${SRC}
|
||||
${CC} $(CFLAGS) $(INCLUDEDIR) -c $<
|
||||
|
||||
all:${TARGET}
|
||||
|
||||
$(TARGET):$(OBJ)
|
||||
ar crv $(TARGET) $(OBJ)
|
||||
|
||||
install:
|
||||
cp -f $(TARGET) $(INSTALLDIR)
|
||||
clean :
|
||||
rm -rf $(OBJ) $(TARGET) *.log *.pid *.core
|
||||
@@ -0,0 +1,172 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <cfgconfig.h>
|
||||
|
||||
char cfgname[60];
|
||||
|
||||
long Str2Dec(char *cpHex)
|
||||
{
|
||||
char cpBuf[100];
|
||||
int nLen,nLoop;
|
||||
long lnResult=0,lnMultiple=1;
|
||||
|
||||
strcpy(cpBuf,cpHex);
|
||||
if(strncmp(cpBuf,"0x",2)==0)
|
||||
strcpy(cpBuf,cpBuf+2);
|
||||
else
|
||||
return atol(cpBuf);
|
||||
|
||||
nLen = strlen(cpBuf);
|
||||
for(nLoop=nLen-1;nLoop>=0;nLoop--)
|
||||
{
|
||||
if(cpBuf[nLoop]>='0'&&cpBuf[nLoop]<='9')
|
||||
lnResult += ((cpBuf[nLoop]-'0')*lnMultiple);
|
||||
else if(cpBuf[nLoop]>='A'&&cpBuf[nLoop]<='F')
|
||||
lnResult += ((cpBuf[nLoop]-'A'+10)*lnMultiple);
|
||||
else if(cpBuf[nLoop]>='a'&&cpBuf[nLoop]<='f')
|
||||
lnResult += ((cpBuf[nLoop]-'A'+10)*lnMultiple);
|
||||
|
||||
lnMultiple *=16;
|
||||
}
|
||||
|
||||
return lnResult;
|
||||
}
|
||||
|
||||
char * makeToken(cpApp, cpRxt)
|
||||
char *cpApp;
|
||||
char *cpRxt;
|
||||
|
||||
{
|
||||
int loop;
|
||||
int in = 0;
|
||||
int leng;
|
||||
|
||||
leng = strlen(cpApp);
|
||||
|
||||
for(loop=0; loop < leng; loop ++)
|
||||
{
|
||||
if(cpApp[loop] == '#')
|
||||
{
|
||||
cpRxt[0] = '\0';
|
||||
return &cpApp[leng];
|
||||
};
|
||||
|
||||
if((cpApp[loop] == ' ') || (cpApp[loop] == '\t') || (cpApp[loop] == '=') || (cpApp[loop] == '\n') )
|
||||
continue;
|
||||
|
||||
for(loop; loop < leng; loop ++)
|
||||
{
|
||||
if((cpApp[loop] == ' ') || (cpApp[loop] == '\t') || (cpApp[loop] == '=') || (cpApp[loop] == '\n') )
|
||||
{
|
||||
cpRxt[in] ='\0';
|
||||
return &cpApp[loop];
|
||||
};
|
||||
cpRxt[in++] = cpApp[loop];
|
||||
};
|
||||
};
|
||||
cpRxt[in] ='\0';
|
||||
return &cpApp[loop];
|
||||
}
|
||||
|
||||
int ReadProfile(cpApp, cpKey, cpRxt)
|
||||
char *cpApp;
|
||||
char *cpKey;
|
||||
char *cpRxt;
|
||||
{
|
||||
|
||||
char cpBuf[MAX_BUF];
|
||||
char cpTmp[MAX_BUF];
|
||||
char cpAppBuf[MAX_BUF];
|
||||
char *cpPtr;
|
||||
FILE *fp;
|
||||
|
||||
sprintf(cpAppBuf,"[%s]", cpApp);
|
||||
|
||||
fp = fopen(cfgname, "r");
|
||||
|
||||
if(fp == NULL)
|
||||
{
|
||||
return -1;
|
||||
};
|
||||
|
||||
if( (strlen(cpApp) < 1) || (strlen(cpKey) < 1) ) return -2;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
fgets(cpBuf,MAX_BUF, fp);
|
||||
if(feof(fp) )
|
||||
{
|
||||
fclose(fp);
|
||||
return -3;
|
||||
};
|
||||
|
||||
cpPtr = makeToken(cpBuf, cpTmp);
|
||||
|
||||
if(strcmp(cpTmp, cpAppBuf) == 0)
|
||||
break;
|
||||
};
|
||||
|
||||
for(;;)
|
||||
{
|
||||
fgets(cpBuf,MAX_BUF, fp);
|
||||
if(feof(fp) )
|
||||
break;
|
||||
|
||||
cpPtr = makeToken(cpBuf, cpTmp);
|
||||
|
||||
if( (cpTmp[0] == '[') && (cpTmp[strlen(cpTmp) -1] == ']') ) return -2;
|
||||
|
||||
if(strcmp(cpTmp, cpKey) == 0)
|
||||
{
|
||||
// printf("%s, %s\n", cpTmp, cpKey);
|
||||
cpPtr = makeToken(cpPtr, cpRxt);
|
||||
fclose(fp);
|
||||
return 1;
|
||||
};
|
||||
};
|
||||
|
||||
fclose(fp);
|
||||
return -4;
|
||||
}
|
||||
|
||||
void SetIniName(name)
|
||||
char *name;
|
||||
{
|
||||
strcpy(cfgname, name);
|
||||
}
|
||||
|
||||
int GetProfileInt(part, spart, dval)
|
||||
char *part;
|
||||
char *spart;
|
||||
int dval;
|
||||
{
|
||||
|
||||
int rxt;
|
||||
char buf[100];
|
||||
rxt = ReadProfile(part, spart,buf);
|
||||
|
||||
if(rxt < 0)
|
||||
return dval;
|
||||
|
||||
return (int) Str2Dec(buf);
|
||||
}
|
||||
|
||||
char *GetProfile(part, spart, rval, dval )
|
||||
char *part;
|
||||
char *spart;
|
||||
char *rval;
|
||||
char *dval;
|
||||
|
||||
{
|
||||
int rxt;
|
||||
|
||||
rxt = ReadProfile(part, spart,rval);
|
||||
if(rxt < 0)
|
||||
{
|
||||
strcpy(rval, dval);
|
||||
return dval;
|
||||
}
|
||||
|
||||
return rval;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
|
||||
|
||||
#ifndef CFGCONFIG_H
|
||||
#define CFGCONFIG_H
|
||||
|
||||
|
||||
#define MAX_BUF 1024
|
||||
|
||||
extern char cfgname[60];
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
#program name
|
||||
PRGNAME = neo_statusd
|
||||
INSTALLDIR = /user/service/bin
|
||||
|
||||
VERSION=9.5.0
|
||||
# Which Compiler
|
||||
CC = gcc
|
||||
CXX = g++
|
||||
|
||||
# Which OS
|
||||
OS = FreeBSD
|
||||
|
||||
#library directory
|
||||
LIBDIR = -L/usr/local/lib -L/usr/lib -L../lib -L/user/db/mysql/lib/mysql\
|
||||
-L/user/app/bind-9.5.0/lib/lwres -L/user/app/bind-9.5.0/lib/dns -L/user/app/bind-9.5.0/lib/bin9\
|
||||
-L/user/app/bind-9.5.0/lib/isc -L/user/app/bind-9.5.0/lib/isccfg
|
||||
|
||||
INCLUDEDIR = -I/usr/local/include -I/usr/include\
|
||||
-I../include -I/user/db/mysql/include/mysql\
|
||||
-I/user/app/bind-9.5.0 -I/user/app/bind-9.5.0/lib/dns/include\
|
||||
-I/user/app/bind-9.5.0/lib/bind9/include -I/user/app/bind-9.5.0/lib/isc/include\
|
||||
-I/user/app/bind-9.5.0/lib/isc/include/ics -I/user/service/bind/include\
|
||||
-I/user/app/bind-9.5.0/lib/ics -I/user/app/bind-9.5.0/lib/isc/unix/include\
|
||||
-I/user/app/bind-9.5.0/lib/nothreads/include -I/user/app/bind-9.5.0/lib/isc/x86_32/include\
|
||||
-I/user/app/bind-9.5.0/lib/lwres/include -I/user/app/bind-9.5.0/lib/lwres/unix/include\
|
||||
-I/usr/local/include/libxml2 -I/usr/include/libxml2
|
||||
#libraries
|
||||
ISCCFGLIBS = /user/app/bind-9.5.0/lib/isccfg/libisccfg.a
|
||||
DNSLIBS = /user/app/bind-9.5.0/lib/dns/libdns.a -lcrypto
|
||||
BIND9LIBS = /user/app/bind-9.5.0/lib/bind9/libbind9.a
|
||||
ISCLIBS = /user/app/bind-9.5.0/lib/isc/libisc.a
|
||||
LWRESLIBS = /user/app/bind-9.5.0/lib/lwres/liblwres.a
|
||||
|
||||
ISCCFGDEPLIBS = /user/app/bind-9.5.0/lib/isccfg/libisccfg.a
|
||||
DNSDEPLIBS = /user/app/bind-9.5.0/lib/dns/libdns.a
|
||||
BIND9DEPLIBS = /user/app/bind-9.5.0/lib/bind9/libbind9.a
|
||||
ISCDEPLIBS = /user/app/bind-9.5.0/lib/isc/libisc.a
|
||||
LWRESDEPLIBS = /user/app/bind-9.5.0/lib/lwres/liblwres.a
|
||||
|
||||
DEPLIBS = ${DNSDEPLIBS} ${BIND9DEPLIBS} ${ISCDEPLIBS} ${ISCCFGDEPLIBS} \
|
||||
${LWRESDEPLIBS}
|
||||
LIBS = ${LWRESLIBS} ${DNSLIBS} ${BIND9LIBS} ${ISCLIBS} \
|
||||
${ISCCFGLIBS} -L/usr/lib -lxml2 -lz -lpthread -lm -lmysqlclient
|
||||
#-lcap -lnsl -L/usr/lib -lxml2 -lz -lpthread -lm -lmysqlclient
|
||||
#LIBS = -lpthread -lmysqlclient\
|
||||
# -lcap -lnsl -lxml2 -lz -lpthread -lm
|
||||
|
||||
#compile option
|
||||
#CFLAGS = -Wall -O -g3 -Wall -Wimplicit -Wreturn-type -Wunused -Wuninitialized
|
||||
CFLAGS = -g3 -O2 -I/usr/include/libxml2
|
||||
STD_CWARNINGS = -W -Wall -Wmissing-prototypes -Wcast-qual -Wwrite-strings -Wformat -Wpointer-arith -fno-strict-aliasing
|
||||
|
||||
BUILD_CC = gcc
|
||||
BUILD_CFLAGS = -g -O2 -I/usr/include/libxml2
|
||||
BUILD_CPPFLAGS =
|
||||
BUILD_LDFLAGS =
|
||||
BUILD_LIBS = -lcap -lnsl -L/usr/lib -lxml2 -lz -lpthread -lm
|
||||
# -Wparentheses -Wshadow -Wpointer-arith -Woverloaded-virtual
|
||||
|
||||
#link option
|
||||
#LFLAGS =
|
||||
|
||||
CDEFINES = -DVERSION=\"${VERSION}\"
|
||||
|
||||
#Definition flag
|
||||
#DFLAGS = -D__DEBUG__
|
||||
#DFLAGS = -D__SETLOG__
|
||||
#DFLAGS = -D__RELEASE__
|
||||
#DFLAGS = -D_REENTRANT
|
||||
|
||||
# Source List
|
||||
SRC = main.c check_neocast.c util.c cfgconfig.c dig.c dighost.c wrapsock.c errfunc.c
|
||||
|
||||
# Object List
|
||||
OBJ = main.o check_neocast.o util.o cfgconfig.o dig.o dighost.o wrapsock.o errfunc.o
|
||||
|
||||
all : Makefile ${OBJ}
|
||||
${CC} ${CFLAGS} ${STD_CWARNINGS} ${OBJ} -o ${PRGNAME} ${LIBDIR} ${LIBS} ${LFLAGS} ${DEPLIBS}
|
||||
.c.o : ${SRC}
|
||||
${CC} $(CFLAGS) -c -o $@ $< ${INCLUDEDIR} ${DFLAGS} ${CDEFINES}
|
||||
install :
|
||||
cp -f $(PRGNAME) $(INSTALLDIR)
|
||||
clean :
|
||||
rm -f $(OBJ) ${HOSTSTATOBJ} $(PRGNAME) *.log *.pid *.core
|
||||
@@ -0,0 +1,2 @@
|
||||
notice :
|
||||
to complie this sources You must install bind9 package.
|
||||
@@ -0,0 +1,133 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "cfgconfig.h"
|
||||
|
||||
long Str2Dec(char *cpHex)
|
||||
{
|
||||
char cpBuf[100];
|
||||
int nLen,nLoop;
|
||||
long lnResult=0,lnMultiple=1;
|
||||
|
||||
strcpy(cpBuf,cpHex);
|
||||
if(strncmp(cpBuf,"0x",2)==0)
|
||||
strcpy(cpBuf,cpBuf+2);
|
||||
else
|
||||
return atol(cpBuf);
|
||||
|
||||
nLen = strlen(cpBuf);
|
||||
for(nLoop=nLen-1;nLoop>=0;nLoop--)
|
||||
{
|
||||
if(cpBuf[nLoop]>='0'&&cpBuf[nLoop]<='9')
|
||||
lnResult += ((cpBuf[nLoop]-'0')*lnMultiple);
|
||||
else if(cpBuf[nLoop]>='A'&&cpBuf[nLoop]<='F')
|
||||
lnResult += ((cpBuf[nLoop]-'A'+10)*lnMultiple);
|
||||
else if(cpBuf[nLoop]>='a'&&cpBuf[nLoop]<='f')
|
||||
lnResult += ((cpBuf[nLoop]-'A'+10)*lnMultiple);
|
||||
|
||||
lnMultiple *=16;
|
||||
}
|
||||
|
||||
return lnResult;
|
||||
}
|
||||
|
||||
char* makeToken(char *cpApp, char *cpRxt)
|
||||
{
|
||||
int loop;
|
||||
int in = 0;
|
||||
int leng;
|
||||
|
||||
leng = strlen(cpApp);
|
||||
|
||||
for(loop=0; loop < leng; loop ++)
|
||||
{
|
||||
if(cpApp[loop] == '#')
|
||||
{
|
||||
cpRxt[0] = '\0';
|
||||
return &cpApp[leng];
|
||||
};
|
||||
|
||||
if((cpApp[loop] == ' ') || (cpApp[loop] == '\t') || (cpApp[loop] == '=') || (cpApp[loop] == '\n') )
|
||||
continue;
|
||||
|
||||
for(loop; loop < leng; loop ++)
|
||||
{
|
||||
if((cpApp[loop] == ' ') || (cpApp[loop] == '\t') || (cpApp[loop] == '=') || (cpApp[loop] == '\n') )
|
||||
{
|
||||
cpRxt[in] ='\0';
|
||||
return &cpApp[loop];
|
||||
};
|
||||
cpRxt[in++] = cpApp[loop];
|
||||
};
|
||||
};
|
||||
cpRxt[in] ='\0';
|
||||
return &cpApp[loop];
|
||||
}
|
||||
|
||||
//int ReadProfile(char *cpApp, char *cpKey, char *cpRxt)
|
||||
int ReadProfile(char *cpKey, char *cpRxt)
|
||||
{
|
||||
|
||||
char cpBuf[MAX_BUF];
|
||||
char cpTmp[MAX_BUF];
|
||||
char *cpPtr;
|
||||
FILE *fp;
|
||||
|
||||
fp = fopen(cfgname, "r");
|
||||
|
||||
if(fp == NULL)
|
||||
{
|
||||
return -1;
|
||||
};
|
||||
|
||||
if( (strlen(cpKey) < 1) ) return -2;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
fgets(cpBuf,MAX_BUF, fp);
|
||||
if(feof(fp) )
|
||||
break;
|
||||
cpPtr = makeToken(cpBuf, cpTmp);
|
||||
|
||||
if(strcmp(cpTmp, cpKey) == 0)
|
||||
{
|
||||
cpPtr = makeToken(cpPtr, cpRxt);
|
||||
fclose(fp);
|
||||
return 1;
|
||||
};
|
||||
};
|
||||
|
||||
fclose(fp);
|
||||
return -4;
|
||||
}
|
||||
|
||||
void SetIniName(char *name)
|
||||
{
|
||||
strcpy(cfgname, name);
|
||||
}
|
||||
|
||||
int GetProfileInt(char *spart, int dval)
|
||||
{
|
||||
int rxt;
|
||||
char buf[100];
|
||||
rxt = ReadProfile(spart,buf);
|
||||
|
||||
if(rxt < 0)
|
||||
return dval;
|
||||
|
||||
return (int)Str2Dec(buf);
|
||||
}
|
||||
|
||||
char* GetProfile(char *spart, char *rval, char *dval)
|
||||
{
|
||||
int rxt;
|
||||
|
||||
rxt = ReadProfile(spart,rval);
|
||||
if(rxt < 0)
|
||||
{
|
||||
strcpy(rval, dval);
|
||||
return dval;
|
||||
}
|
||||
|
||||
return rval;
|
||||
}
|
||||
@@ -0,0 +1,476 @@
|
||||
#include "check_neocast.h"
|
||||
#include "protocol.h"
|
||||
#include "cfgconfig.h"
|
||||
#include "util.h"
|
||||
#include <signal.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/signal.h>
|
||||
|
||||
extern int dig_func(int , char **, int *, int *);
|
||||
extern int Tcp_connect(const char *, const char *);
|
||||
extern void Send(int, const void *, size_t, int);
|
||||
extern ssize_t Recv(int, void *, size_t, int);
|
||||
|
||||
static char hexchars[] = "0123456789ABCDEF";
|
||||
|
||||
void safe_wait()
|
||||
{
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 100;
|
||||
select(0, NULL, NULL, NULL, &tv);
|
||||
}
|
||||
|
||||
#define STAT_STAMP(a) (((a) + (stat_term - 1)) / stat_term)
|
||||
void loop_check()
|
||||
{
|
||||
int ret_val = 0;
|
||||
int stat_term;
|
||||
time_t now, past;
|
||||
unsigned long past_stamp = 0;
|
||||
unsigned long now_stamp = 0;
|
||||
|
||||
SetIniName(CFGFILE);
|
||||
stat_term = GetProfileInt("STAT_TERM", 60);
|
||||
|
||||
now = past = time(NULL);
|
||||
now_stamp = past_stamp = STAT_STAMP(now);
|
||||
|
||||
for( ; ; ) {
|
||||
now = time(NULL);
|
||||
now_stamp = STAT_STAMP(now);
|
||||
|
||||
if (past != now) {
|
||||
// whenever 1 minutes (60 seconds)
|
||||
ret_val = check_neocast(now, &now_stamp, &past_stamp);
|
||||
if (ret_val == 1)
|
||||
{
|
||||
past = now = time(NULL);
|
||||
now_stamp = past_stamp = STAT_STAMP(now);
|
||||
}
|
||||
}
|
||||
safe_wait();
|
||||
}
|
||||
}
|
||||
|
||||
int check_neocast(time_t now, unsigned long *now_stamp, unsigned long *past_stamp)
|
||||
{
|
||||
FILE *shLogFile = NULL;
|
||||
struct tm *t;
|
||||
neo_serv_info *nserv_info, *temp_info;
|
||||
MYSQL mysql, child_mysql;
|
||||
MYSQL_RES *res;
|
||||
MYSQL_ROW row;
|
||||
|
||||
int i;
|
||||
int mysql_port, fields;
|
||||
long numRows;
|
||||
long cnt = 0;
|
||||
char mysql_db[8];
|
||||
char mysql_user[16];
|
||||
char mysql_pass[16];
|
||||
|
||||
char logString[1024];
|
||||
|
||||
/* check time */
|
||||
if (*now_stamp == *past_stamp)
|
||||
return 0;
|
||||
|
||||
/* sh log file open */
|
||||
shLogFile = fopen(makeLogString(), "a");
|
||||
if (!shLogFile)
|
||||
{
|
||||
fprintf(stderr, "Cannot open SH Log file!!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/***********************************************************************************************************/
|
||||
/* initialize mysqlDB */
|
||||
mysql_init(&mysql);
|
||||
mysql_init(&child_mysql);
|
||||
|
||||
// get mysql info
|
||||
SetIniName(CFGFILE);
|
||||
|
||||
GetProfile("DATABASE_NAME", mysql_db, "CCDB");
|
||||
GetProfile("DATABASE_ACCT", mysql_user, "root");
|
||||
GetProfile("DATABASE_PW", mysql_pass, "alfkzmf!?{}");
|
||||
mysql_port = GetProfileInt("DATABASE_PORT", 3306);
|
||||
|
||||
fprintf(stderr, "DATABASE_NAME : %s\n", mysql_db);
|
||||
fprintf(stderr, "DATABASE_ACCT : %s\n", mysql_user);
|
||||
fprintf(stderr, "DATABASE_PW : %s\n", mysql_pass);
|
||||
fprintf(stderr, "DATABASE_PORT : %d\n", mysql_port);
|
||||
|
||||
/* connect mysqlDB */
|
||||
if (!mysql_real_connect(&mysql, NULL, mysql_user, mysql_pass, mysql_db, mysql_port, (char *)NULL, 0))
|
||||
{
|
||||
fprintf(stderr, "parent mysql: %s\n", mysql_error(&mysql));
|
||||
snprintf(logString, 1024, "%s", mysql_error(&mysql));
|
||||
WriteSHLog(logString, shLogFile);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (!mysql_real_connect(&child_mysql, NULL, mysql_user, mysql_pass, mysql_db, mysql_port, (char *)NULL, 0))
|
||||
{
|
||||
fprintf(stderr, "%s\n", mysql_error(&child_mysql));
|
||||
snprintf(logString, 1024, "child mysql: %s", mysql_error(&child_mysql));
|
||||
WriteSHLog(logString, shLogFile);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
/***********************************************************************************************************/
|
||||
/* autocommit on */
|
||||
mysql_autocommit(&child_mysql, 1);
|
||||
|
||||
snprintf(logString, 1024, "Query to get arranged Server List :: %s", MYQUERY);
|
||||
WriteSHLog(logString, shLogFile);
|
||||
|
||||
if (mysql_query(&mysql, MYQUERY) < 0)
|
||||
{
|
||||
snprintf(logString, 1024, "[error]%s", mysql_error(&mysql));
|
||||
WriteSHLog(logString, shLogFile);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(logString, 1024, "%s", "[MySQL]Server List Loading Success!!");
|
||||
WriteSHLog(logString, shLogFile);
|
||||
}
|
||||
res = mysql_store_result(&mysql);
|
||||
fields = mysql_num_fields(res);
|
||||
numRows = mysql_num_rows(res);
|
||||
|
||||
snprintf(logString, 1024, "numRows --> %ld", numRows);
|
||||
WriteSHLog(logString, shLogFile);
|
||||
|
||||
neo_serv_num = numRows;
|
||||
nserv_info = (neo_serv_info *)malloc(sizeof(neo_serv_info));
|
||||
temp_info = nserv_info;
|
||||
|
||||
while((row = mysql_fetch_row(res)))
|
||||
{
|
||||
memset(temp_info, 0, sizeof(temp_info));
|
||||
temp_info->neo_seq = atol(row[0]);
|
||||
strncpy(temp_info->name, row[1], MaxSizeOfServerName);
|
||||
strncpy(temp_info->domain, row[2], MaxSizeOfServerAddr);
|
||||
strncpy(temp_info->dig_domain, row[3], MaxSizeOfServerAddr);
|
||||
strncpy(temp_info->ipaddr, row[4], MaxSizeOfServerIp);
|
||||
strncpy(temp_info->type ,row[5], MaxSizeOfServerType);
|
||||
temp_info->port = atoi(row[6]);
|
||||
temp_info->dead_count = atoi(row[7]);
|
||||
temp_info->status = atoi(row[8]);
|
||||
temp_info->next = NULL;
|
||||
|
||||
temp_info->next = (neo_serv_info *)malloc(sizeof(neo_serv_info));
|
||||
temp_info = temp_info->next;
|
||||
}
|
||||
temp_info = NULL;
|
||||
|
||||
temp_info = nserv_info;
|
||||
for (cnt = 0; cnt < neo_serv_num; cnt++)
|
||||
{
|
||||
pid_t dpid;
|
||||
int status = 0;
|
||||
// check neocast STAT
|
||||
if (!strcmp(temp_info->type, "STAT"))
|
||||
{
|
||||
char buf[1024];
|
||||
int result;
|
||||
|
||||
// CHG 2010-10-21 huibong KT -> X-CDN
|
||||
//sprintf(buf, "wget -T 10 -t 2 -q \"http://222.122.18.6/neo_status\" -O /root/neo_status");
|
||||
sprintf(buf, "wget -T 10 -t 2 -q \"http://www.x-cdn.com/neo_status\" -O /root/neo_status");
|
||||
|
||||
WriteSHLog(buf, shLogFile);
|
||||
result = system(buf);
|
||||
|
||||
if (result == 0)
|
||||
{
|
||||
FILE *fp = NULL;
|
||||
char update_query[1024];
|
||||
char msgStr[1024];
|
||||
|
||||
sprintf(buf, "/root/neo_status");
|
||||
if ((fp = fopen(buf, "r")))
|
||||
{
|
||||
memset(buf, 0, 4);
|
||||
fread(buf, 1, 3, fp);
|
||||
|
||||
if (buf[0] == 'O') { // wget success
|
||||
WriteSHLog("[STAT SUCCESS!]", shLogFile);
|
||||
|
||||
if (temp_info->status == 2 || temp_info->dead_count > 0) {
|
||||
snprintf(update_query, 1024, "UPDATE t_neo_server_index SET dead_count = 0, status = 1, update_datetime = now() WHERE neo_seq = %ld", temp_info->neo_seq);
|
||||
|
||||
WriteSHLog(update_query, shLogFile);
|
||||
if (mysql_query(&child_mysql, update_query) != 0)
|
||||
{
|
||||
snprintf(logString, 1024, "[error]%s", mysql_error(&child_mysql));
|
||||
WriteSHLog(logString, shLogFile);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(logString, 1024, "%s", "Update STAT query Success!!");
|
||||
WriteSHLog(logString, shLogFile);
|
||||
}
|
||||
|
||||
// SMS BACK and SendToWebServ
|
||||
if (temp_info->status == 2) {
|
||||
snprintf(msgStr, MaxSizeOfsmsStr, "[NEO][BACK] STAT IS BACK!");
|
||||
SendToWebServ(msgStr, shLogFile);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
else if (buf[0] == 'E') {
|
||||
WriteSHLog("[!STAT FAIL]", shLogFile);
|
||||
// SMS DEAD and SendToWebServ
|
||||
if (temp_info->dead_count >= MAX_ALLOWED_DEAD_COUNT && ((int)temp_info->dead_count < MAX_ALLOWED_DEAD_COUNT + 5)) {
|
||||
snprintf(msgStr, MaxSizeOfsmsStr, "[NEO][DEAD] STAT IS ERROR!!");
|
||||
snprintf(update_query, 1024, "UPDATE t_neo_server_index SET dead_count = %d, status = 2, update_datetime = now() WHERE neo_seq = %ld", temp_info->dead_count + 1, temp_info->neo_seq);
|
||||
SendToWebServ(msgStr, shLogFile);
|
||||
}
|
||||
else {
|
||||
// SMS DEAD but not SendToWebServ
|
||||
snprintf(update_query, 1024, "UPDATE t_neo_server_index SET dead_count = %d, update_datetime = now() WHERE neo_seq = %ld", temp_info->dead_count + 1, temp_info->neo_seq);
|
||||
}
|
||||
WriteSHLog(update_query, shLogFile);
|
||||
if (mysql_query(&child_mysql, update_query) != 0)
|
||||
{
|
||||
snprintf(logString, 1024, "[error]%s", mysql_error(&child_mysql));
|
||||
WriteSHLog(logString, shLogFile);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(logString, 1024, "%s", "Update STAT query Success!!");
|
||||
WriteSHLog(logString, shLogFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (fp)
|
||||
fclose(fp);
|
||||
}
|
||||
}
|
||||
signal(SIGCHLD, SIG_DFL);
|
||||
|
||||
dpid = fork();
|
||||
// child must process dig func
|
||||
if (dpid == 0)
|
||||
{
|
||||
int i;
|
||||
char dig_cmd[1024];
|
||||
char update_query[1024];
|
||||
char msgStr[1024];
|
||||
char *paraop[512];
|
||||
int qrcount = 0;
|
||||
int ancount = 0;
|
||||
int retval = 0;
|
||||
|
||||
if (!strcmp(temp_info->type, "STAT"))
|
||||
{
|
||||
fclose(shLogFile);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
setproctitle("grandchildren: working for digging...");
|
||||
snprintf(dig_cmd, 1024, "dig @%s %s", temp_info->domain, temp_info->dig_domain);
|
||||
WriteSHLog(dig_cmd, shLogFile);
|
||||
paraop[0] = (char *)malloc(512);
|
||||
paraop[1] = (char *)malloc(512);
|
||||
paraop[2] = (char *)malloc(512);
|
||||
memset(paraop[0], 0, 512);
|
||||
memset(paraop[1], 0, 512);
|
||||
memset(paraop[2], 0, 512);
|
||||
|
||||
strncpy(paraop[0], "dig", 512);
|
||||
snprintf(paraop[1], 512, "@%s", temp_info->domain);
|
||||
strncpy(paraop[2], temp_info->dig_domain, 512);
|
||||
|
||||
retval = dig_func(3, paraop, &qrcount, &ancount);
|
||||
|
||||
/* three case */
|
||||
/* fail : invaild dns name : retval = -1, ancount = 0 */
|
||||
/*fail : invalid domain name, query timout : retval = 0, ancount = 0 */
|
||||
/* success : retval = 0, ancount >= 1*/
|
||||
snprintf(logString, 1024, "dig result : QUERY: %d, ANSWER: %d", qrcount, ancount);
|
||||
WriteSHLog(logString, shLogFile);
|
||||
|
||||
if (retval == -1 || (qrcount == 0 && ancount == 0))
|
||||
{
|
||||
//if (temp_info->dead_count >= MAX_ALLOWED_DEAD_COUNT && ((int)temp_info->dead_count < MAX_ALLOWED_DEAD_COUNT + 5)) {
|
||||
//modify sindong .... 09/12/31
|
||||
if (temp_info->dead_count >= 0 && ((int)temp_info->dead_count < MAX_ALLOWED_DEAD_COUNT + 5)) {
|
||||
// SMS DEAD and SendToWebServ
|
||||
snprintf(msgStr, MaxSizeOfsmsStr, "[NEO][DEAD] %s \n", dig_cmd);
|
||||
snprintf(update_query, 1024, "UPDATE t_neo_server_index SET dead_count = %d, status = 2, update_datetime = now() WHERE neo_seq = %ld",
|
||||
temp_info->dead_count + 1, temp_info->neo_seq);
|
||||
SendToWebServ(msgStr, shLogFile);
|
||||
}
|
||||
else {
|
||||
// SMS DEAD but not SendToWebServ
|
||||
snprintf(update_query, 1024, "UPDATE t_neo_server_index SET dead_count = %d, update_datetime = now() WHERE neo_seq = %ld",
|
||||
temp_info->dead_count + 1, temp_info->neo_seq);
|
||||
}
|
||||
}
|
||||
else if (qrcount >= 1 && ancount >= 1)
|
||||
{
|
||||
if (temp_info->status == 2)
|
||||
{
|
||||
snprintf(update_query, 1024, "UPDATE t_neo_server_index SET dead_count = 0, status = 1, update_datetime = now() WHERE neo_seq = %ld", temp_info->neo_seq);
|
||||
// SMS BACK and SendToWebServ
|
||||
snprintf(msgStr, MaxSizeOfsmsStr, "[NEO][BACK] %s \n", dig_cmd);
|
||||
SendToWebServ(msgStr, shLogFile);
|
||||
}
|
||||
else if (temp_info->status == 1 && temp_info->dead_count == 0)
|
||||
goto free_exit;
|
||||
else
|
||||
snprintf(update_query, 1024, "UPDATE t_neo_server_index SET dead_count = 0, status = 1, update_datetime = now() WHERE neo_seq = %ld", temp_info->neo_seq);
|
||||
}
|
||||
else
|
||||
goto free_exit;
|
||||
|
||||
WriteSHLog(update_query, shLogFile);
|
||||
if (mysql_query(&child_mysql, update_query) != 0)
|
||||
{
|
||||
snprintf(logString, 1024, "[error]%s", mysql_error(&child_mysql));
|
||||
WriteSHLog(logString, shLogFile);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(logString, 1024, "%s", "Update DIG query Success!!");
|
||||
WriteSHLog(logString, shLogFile);
|
||||
}
|
||||
free_exit:
|
||||
for (i=0; i < 3; i++)
|
||||
free(paraop[i]);
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
wait(&status);
|
||||
temp_info = temp_info->next;
|
||||
usleep(1);
|
||||
}
|
||||
WriteSHLog("----------------------------------------------------- end ---------------------------------------------\n", shLogFile);
|
||||
*past_stamp = *now_stamp;
|
||||
mysql_free_result(res);
|
||||
mysql_close(&mysql);
|
||||
mysql_close(&child_mysql);
|
||||
fclose(shLogFile);
|
||||
for (i=0; i < neo_serv_num; i++)
|
||||
{
|
||||
free(nserv_info);
|
||||
nserv_info = nserv_info->next;
|
||||
}
|
||||
free(nserv_info);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// CHG 2010-10-20 huibong KT -> X-CDN
|
||||
//#define SMSServAddr "222.122.136.110"
|
||||
//#define SMSServerHost "sh.0x31.com"
|
||||
//#define SMSServPort "80"
|
||||
//#define MaxSizeOfTRxBuffer 1024
|
||||
|
||||
#define SMSServAddr "www.x-cdn.com"
|
||||
#define SMSServerHost "gms.x-cdn.com"
|
||||
#define SMSServPort "8080"
|
||||
#define MaxSizeOfTRxBuffer 1024
|
||||
|
||||
|
||||
void SendToWebServ(const char *pszMesg, FILE *shLogFile)
|
||||
{
|
||||
char *PostDataEncode(const char *);
|
||||
|
||||
char *strURI;
|
||||
char szSndBuffer[MaxSizeOfTRxBuffer];
|
||||
char szRcvBuffer[MaxSizeOfTRxBuffer];
|
||||
char logString[1024];
|
||||
char sms_admin[32];
|
||||
|
||||
int fd, n, k, param_num;
|
||||
|
||||
bzero(szSndBuffer, MaxSizeOfTRxBuffer);
|
||||
bzero(szRcvBuffer, MaxSizeOfTRxBuffer);
|
||||
|
||||
strURI = PostDataEncode(pszMesg);
|
||||
|
||||
SetIniName(CFGFILE);
|
||||
GetProfile("SMS_ADMIN", sms_admin, "ADMIN");
|
||||
snprintf(szSndBuffer, MaxSizeOfTRxBuffer, "GET /sms/send_sms.html?id=%s&msg=%s HTTP/1.0\n"\
|
||||
"Content-Type: text/plain\n"\
|
||||
"User-Agent: SeanZilla/1.0\n"\
|
||||
"Host: %s\n"\
|
||||
"Connection: close\n"\
|
||||
"Cache-Control: no-cache\n\n"
|
||||
, sms_admin, strURI, SMSServerHost);
|
||||
|
||||
snprintf(logString, 1024, "[SMS Result]::id=%s :: msg=%s", sms_admin, pszMesg);
|
||||
WriteSHLog(logString, shLogFile);
|
||||
|
||||
/* tcp connection, send, receive */
|
||||
fd = Tcp_connect(SMSServAddr, SMSServPort);
|
||||
/* send sndMessage to SMS WEB SERV */
|
||||
Send(fd, szSndBuffer, strlen(szSndBuffer), 0);
|
||||
usleep(1);
|
||||
/* receive result from new sh_statusd */
|
||||
if ( (n = Recv(fd, szRcvBuffer, strlen(szRcvBuffer), 0)) == 0)
|
||||
{
|
||||
#if 0
|
||||
snprintf(logString, 1024, "[SMS] server : %s", strURI);
|
||||
WriteSHLog(logString, shLogFile);
|
||||
#endif
|
||||
}
|
||||
free(strURI);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
char *PostDataEncode(const char *pszCommand)
|
||||
{
|
||||
int i = 0;
|
||||
char tmpBuffer[MaxSizeOfTRxBuffer];
|
||||
static char *strReturn;
|
||||
|
||||
if(pszCommand == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while(*pszCommand != '\0')
|
||||
{
|
||||
if(*pszCommand == ' ')
|
||||
{
|
||||
tmpBuffer[i++] = '+';
|
||||
*pszCommand++;
|
||||
}
|
||||
else if((*pszCommand < '0' && *pszCommand != '-' && *pszCommand != '.' && *pszCommand !='%') ||
|
||||
(*pszCommand < 'A' && *pszCommand > '9' && *pszCommand !='%') ||
|
||||
(*pszCommand > 'Z' && *pszCommand < 'a' && *pszCommand != '_' && *pszCommand !='%') ||
|
||||
(*pszCommand > 'z' && *pszCommand !='%'))
|
||||
{
|
||||
tmpBuffer[i++] = '%';
|
||||
tmpBuffer[i++] = hexchars[(unsigned char)*pszCommand >> 4];
|
||||
tmpBuffer[i++] = hexchars[(unsigned char)*pszCommand & 15];
|
||||
*pszCommand++;
|
||||
}
|
||||
else if(*pszCommand == '%')
|
||||
{
|
||||
tmpBuffer[i++] = '%';
|
||||
tmpBuffer[i++] = '%';
|
||||
*pszCommand++;
|
||||
}
|
||||
else
|
||||
{
|
||||
tmpBuffer[i++] = *pszCommand++;
|
||||
}
|
||||
}
|
||||
tmpBuffer[i] = '\0';
|
||||
|
||||
// allocate heap
|
||||
strReturn = (char *)malloc(strlen(tmpBuffer));
|
||||
memset(strReturn, 0, strlen(tmpBuffer));
|
||||
strncpy(strReturn, tmpBuffer, strlen(tmpBuffer)+1);
|
||||
return strReturn;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,113 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h> /* ANSI C header file */
|
||||
#include <syslog.h> /* for syslog() */
|
||||
|
||||
|
||||
#define MAXLINE 4096
|
||||
|
||||
int daemon_proc; /* set nonzero by daemon_init() */
|
||||
|
||||
static void err_doit(int, int, const char *, va_list);
|
||||
|
||||
/* Nonfatal error related to system call
|
||||
* Print message and return */
|
||||
|
||||
void
|
||||
err_ret(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
err_doit(1, LOG_INFO, fmt, ap);
|
||||
va_end(ap);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Fatal error related to system call
|
||||
* Print message and terminate */
|
||||
|
||||
void
|
||||
err_sys(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
err_doit(1, LOG_ERR, fmt, ap);
|
||||
va_end(ap);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Fatal error related to system call
|
||||
* Print message, dump core, and terminate */
|
||||
|
||||
void
|
||||
err_dump(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
err_doit(1, LOG_ERR, fmt, ap);
|
||||
va_end(ap);
|
||||
abort(); /* dump core and terminate */
|
||||
exit(1); /* shouldn't get here */
|
||||
}
|
||||
|
||||
/* Nonfatal error unrelated to system call
|
||||
* Print message and return */
|
||||
|
||||
void
|
||||
err_msg(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
err_doit(0, LOG_INFO, fmt, ap);
|
||||
va_end(ap);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Fatal error unrelated to system call
|
||||
* Print message and terminate */
|
||||
|
||||
void
|
||||
err_quit(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
err_doit(0, LOG_ERR, fmt, ap);
|
||||
va_end(ap);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Print message and return to caller
|
||||
* Caller specifies "errnoflag" and "level" */
|
||||
|
||||
static void
|
||||
err_doit(int errnoflag, int level, const char *fmt, va_list ap)
|
||||
{
|
||||
int errno_save, n;
|
||||
char buf[MAXLINE + 1];
|
||||
|
||||
errno_save = errno; /* value caller might want printed */
|
||||
#ifdef HAVE_VSNPRINTF
|
||||
vsnprintf(buf, MAXLINE, fmt, ap); /* safe */
|
||||
#else
|
||||
vsprintf(buf, fmt, ap); /* not safe */
|
||||
#endif
|
||||
n = strlen(buf);
|
||||
if (errnoflag)
|
||||
snprintf(buf + n, MAXLINE - n, ": %s", strerror(errno_save));
|
||||
strcat(buf, "\n");
|
||||
|
||||
if (daemon_proc) {
|
||||
syslog(level, buf);
|
||||
} else {
|
||||
fflush(stdout); /* in case stdout and stderr are the same */
|
||||
fputs(buf, stderr);
|
||||
fflush(stderr);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,274 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/signal.h>
|
||||
#include <errno.h>
|
||||
#include "check_neocast.h"
|
||||
|
||||
/*
|
||||
void sig_handler(int signo)
|
||||
{
|
||||
fprintf(stderr, "SIGNAL thid %d : %d\n", pthread_self(), signo);
|
||||
pthread_exit((void *)NULL);
|
||||
}
|
||||
*/
|
||||
|
||||
void makedaemon(int mode)
|
||||
{
|
||||
#ifdef SIGTTOU
|
||||
signal(SIGTTOU, SIG_IGN);
|
||||
#endif
|
||||
#ifdef SIGTTIN
|
||||
signal(SIGTTIN, SIG_IGN);
|
||||
#endif
|
||||
#ifdef SIGTSTP
|
||||
signal(SIGTSTP, SIG_IGN);
|
||||
#endif
|
||||
#ifdef SIGINT
|
||||
signal(SIGINT, SIG_IGN);
|
||||
#endif
|
||||
#ifdef SIGSTP
|
||||
signal(SIGSTP, SIG_IGN);
|
||||
#endif
|
||||
|
||||
pid_t pid;
|
||||
if ((pid = fork()) < 0)
|
||||
exit(0);
|
||||
|
||||
else if (pid != 0)
|
||||
exit(0);
|
||||
|
||||
if (mode == 0)
|
||||
{
|
||||
close(0);
|
||||
close(1);
|
||||
close(2);
|
||||
}
|
||||
setsid();
|
||||
}
|
||||
|
||||
void HandleCoreDump(int signum)
|
||||
{
|
||||
signal(signum, SIG_DFL);
|
||||
kill(getpid(), SIGKILL);
|
||||
sleep(3);
|
||||
}
|
||||
|
||||
void HandleChildHang(int signum)
|
||||
{
|
||||
int status = 1;
|
||||
|
||||
while (waitpid(-1, &status, WNOHANG) > 0)
|
||||
{
|
||||
fprintf(stderr, "decrease one child process!\n");
|
||||
}
|
||||
}
|
||||
|
||||
static int SetSigHandle(int Option)
|
||||
{
|
||||
struct sigaction sa;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
sa.sa_flags = 0;
|
||||
|
||||
#if defined(SA_ONESHOT)
|
||||
sa.sa_flags = SA_ONESHOT;
|
||||
#elif defined(SA_RESETHAND)
|
||||
sa.sa_flags = SA_RESETHAND;
|
||||
#endif
|
||||
|
||||
if(Option == 1)
|
||||
{
|
||||
sa.sa_handler = HandleChildHang;
|
||||
if(sigaction(SIGCHLD, &sa, NULL) < 0)
|
||||
{
|
||||
//DISPLAY_SIGNAL_ACTION_ERROR(-1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sa.sa_handler = HandleCoreDump;
|
||||
}
|
||||
|
||||
if(sigaction(SIGSEGV, &sa, NULL) == -1)
|
||||
{
|
||||
//DISPLAY_SIGNAL_ACTION_ERROR(-2);
|
||||
}
|
||||
|
||||
#ifdef SIGBUS
|
||||
if(sigaction(SIGBUS, &sa, NULL) < 0)
|
||||
{
|
||||
//DISPLAY_SIGNAL_ACTION_ERROR(-3);
|
||||
}
|
||||
#endif
|
||||
#ifdef SIGABORT
|
||||
if(sigaction(SIGABORT, &sa, NULL) < 0)
|
||||
{
|
||||
//DISPLAY_SIGNAL_ACTION_ERROR(-4);
|
||||
}
|
||||
#endif
|
||||
#ifdef SIGABRT
|
||||
if(sigaction(SIGABRT, &sa, NULL) < 0)
|
||||
{
|
||||
//DISPLAY_SIGNAL_ACTION_ERROR(-5);
|
||||
}
|
||||
#endif
|
||||
#ifdef SIGILL
|
||||
if(sigaction(SIGILL, &sa, NULL) < 0)
|
||||
{
|
||||
//DISPLAY_SIGNAL_ACTION_ERROR(-6);
|
||||
}
|
||||
#endif
|
||||
if(sigaction(SIGHUP, &sa, NULL) < 0)
|
||||
{
|
||||
//DISPLAY_SIGNAL_ACTION_ERROR(-7);
|
||||
}
|
||||
if(sigaction(SIGUSR1, &sa, NULL) < 0)
|
||||
{
|
||||
//DISPLAY_SIGNAL_ACTION_ERROR(-8);
|
||||
}
|
||||
#ifdef SIGINT
|
||||
if(sigaction(SIGINT, &sa, NULL) < 0)
|
||||
{
|
||||
//DISPLAY_SIGNAL_ACTION_ERROR(-9);
|
||||
}
|
||||
#endif
|
||||
#ifdef SIGXCPU
|
||||
sa.sa_handler = SIG_DFL;
|
||||
if(sigaction(SIGXCPU, &sa, NULL) < 0)
|
||||
{
|
||||
//DISPLAY_SIGNAL_ACTION_ERROR(-10);
|
||||
}
|
||||
#endif
|
||||
#ifdef SIGXPSZ
|
||||
sa.sa_handler = SIG_DFL;
|
||||
if(sigaction(SIGSEGV, &sa, NULL) < 0)
|
||||
{
|
||||
//DISPLAY_SIGNAL_ACTION_ERROR(-11);
|
||||
}
|
||||
#endif
|
||||
#ifdef SIGPIPE
|
||||
sa.sa_handler = SIG_IGN;
|
||||
if(sigaction(SIGPIPE, &sa, NULL) < 0)
|
||||
{
|
||||
//DISPLAY_SIGNAL_ACTION_ERROR(-12);
|
||||
}
|
||||
#endif
|
||||
#ifdef SIGSYS
|
||||
sa.sa_handler = SIG_IGN;
|
||||
if(sigaction(SIGPIPE, &sa, NULL) < 0)
|
||||
{
|
||||
//DISPLAY_SIGNAL_ACTION_ERROR(-13);
|
||||
}
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
|
||||
void setsighandle_pthread(void)
|
||||
{
|
||||
sigset_t newmask;
|
||||
sigemptyset(&newmask);
|
||||
sigaddset(&newmask, SIGINT);
|
||||
sigaddset(&newmask, SIGSEGV);
|
||||
pthread_sigmask(SIG_BLOCK, &newmask, NULL);
|
||||
}
|
||||
|
||||
static pid_t pids[3];
|
||||
|
||||
#define RELEASE_MODE 0
|
||||
|
||||
int main()
|
||||
{
|
||||
pid_t check_process();
|
||||
|
||||
/* damonlizing... */
|
||||
makedaemon(RELEASE_MODE);
|
||||
|
||||
pids[0] = check_process();
|
||||
|
||||
setproctitle("parents: monitoring all process..");
|
||||
for (;;)
|
||||
pause();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
pid_t check_process()
|
||||
{
|
||||
pid_t pid;
|
||||
void check_child_process();
|
||||
|
||||
if ((pid = fork()) > 0)
|
||||
return (pid);
|
||||
|
||||
check_child_process();
|
||||
}
|
||||
|
||||
void check_child_process()
|
||||
{
|
||||
void thread_func(int);
|
||||
pid_t cpid;
|
||||
int cstatus = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
if ((cpid = fork()) < 0)
|
||||
{
|
||||
fprintf(stderr, "Cannot Fork(child)\n");
|
||||
exit(0);
|
||||
}
|
||||
else if (cpid == 0)
|
||||
{
|
||||
fprintf(stderr, "start check child process\n");
|
||||
setproctitle("child(check): running...");
|
||||
SetSigHandle(0);
|
||||
// NeoCast monitoring section
|
||||
loop_check();
|
||||
}
|
||||
setproctitle("guard(check) : waiting....until work process die");
|
||||
wait(&cstatus);
|
||||
}
|
||||
}
|
||||
|
||||
void thread_func(int arg)
|
||||
{
|
||||
void *thread_one_func(void *);
|
||||
void *thread_two_func(void *);
|
||||
|
||||
pthread_t t1, t2;
|
||||
int a;
|
||||
int b;
|
||||
|
||||
a = arg;
|
||||
b = arg+1;
|
||||
|
||||
pthread_create(&t1, NULL, &thread_one_func, (void *)a);
|
||||
pthread_create(&t2, NULL, &thread_two_func, (void *)b);
|
||||
|
||||
pthread_join(t1, NULL);
|
||||
pthread_join(t2, NULL);
|
||||
}
|
||||
|
||||
void *thread_one_func(void *arg)
|
||||
{
|
||||
int one = 1;
|
||||
|
||||
while(1)
|
||||
{
|
||||
fprintf(stderr, "thread_one --> %d(%d)\n", one, (int)arg);
|
||||
sleep(5);
|
||||
}
|
||||
}
|
||||
|
||||
void *thread_two_func(void *arg)
|
||||
{
|
||||
int two = 2;
|
||||
|
||||
while(1)
|
||||
{
|
||||
fprintf(stderr, "thread_two --> %d(%d)\n", two, (int)arg);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
DATABASE_NAME=CCDB
|
||||
DATABASE_ADDR=127.0.0.1
|
||||
DATABASE_ACCT=root
|
||||
DATABASE_PW=alfkzmf!?{}
|
||||
DATABASE_PORT=3306
|
||||
STAT_TERM=60
|
||||
SMS_ADMIN=ADMIN
|
||||
@@ -0,0 +1,56 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/signal.h>
|
||||
#include <errno.h>
|
||||
#include "check_neocast.h"
|
||||
|
||||
extern int dig_func(int , char **, int *);
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
for ( ; ; )
|
||||
{
|
||||
pid_t pid;
|
||||
int status;
|
||||
|
||||
pid = fork();
|
||||
|
||||
if (pid == 0)
|
||||
{
|
||||
char *paraop[512];
|
||||
int result, i;
|
||||
int ancount = 0;
|
||||
|
||||
paraop[0] = (char *)malloc(512);
|
||||
paraop[1] = (char *)malloc(512);
|
||||
paraop[2] = (char *)malloc(512);
|
||||
memset(paraop[0], 0, 512);
|
||||
memset(paraop[1], 0, 512);
|
||||
memset(paraop[2], 0, 512);
|
||||
|
||||
strncpy(paraop[0], "dig", 512);
|
||||
strncpy(paraop[1], "@ns1.ktics.co.kr", 512);
|
||||
strncpy(paraop[2], "vol2-hangame.ktics.co.kr", 512);
|
||||
|
||||
result = dig_func(3, paraop, &ancount);
|
||||
|
||||
fprintf(stderr, "\n\n\n------------Result : %d\n", result);
|
||||
fprintf(stderr, "------------ANSWER: %d\n", ancount);
|
||||
|
||||
for (i = 0; i < 3; i++)
|
||||
free(paraop[i]);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
waitpid(pid, &status, 0);
|
||||
sleep(2);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/signal.h>
|
||||
#include <errno.h>
|
||||
#include "check_neocast.h"
|
||||
|
||||
extern int dig_func(int , char **, int *);
|
||||
|
||||
void *thread_dig_job(void *arg)
|
||||
{
|
||||
char *paraop[512];
|
||||
int result, i;
|
||||
int ancount = 0;
|
||||
|
||||
paraop[0] = (char *)malloc(512);
|
||||
paraop[1] = (char *)malloc(512);
|
||||
paraop[2] = (char *)malloc(512);
|
||||
memset(paraop[0], 0, 512);
|
||||
memset(paraop[1], 0, 512);
|
||||
memset(paraop[2], 0, 512);
|
||||
|
||||
strncpy(paraop[0], "dig", 512);
|
||||
strncpy(paraop[1], "@ns1.ktics.co.kr", 512);
|
||||
strncpy(paraop[2], "vol2-hangame.ktics.co.kr", 512);
|
||||
|
||||
result = dig_func(3, paraop, &ancount);
|
||||
|
||||
fprintf(stderr, "\n\n (shared_num : %d) ------------Result : %d\n", (int)arg, result);
|
||||
fprintf(stderr, "------------ANSWER: %d\n", ancount);
|
||||
|
||||
for (i = 0; i < 3; i++)
|
||||
free(paraop[i]);
|
||||
|
||||
pthread_exit((void *)NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
void *thread_dig_job(void *);
|
||||
int shared_num = 777;
|
||||
|
||||
for ( ; ; )
|
||||
{
|
||||
pthread_t thrid;
|
||||
|
||||
pthread_create(&thrid, NULL, &thread_dig_job, (void *)shared_num);
|
||||
pthread_join(thrid, NULL);
|
||||
sleep(2);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "util.h"
|
||||
#include "check_neocast.h"
|
||||
|
||||
int devide(char **val, char *str, char delim)
|
||||
{
|
||||
char *p;
|
||||
int i, j;
|
||||
|
||||
val[0] = (char *)malloc(512);
|
||||
memset(val[0], 0, 512);
|
||||
|
||||
for (p=str, i=0, j=0; *p != '\0'; p++)
|
||||
{
|
||||
if (*p == delim)
|
||||
{
|
||||
*(val[i] + j) = '\0';
|
||||
j = 0;
|
||||
i++;
|
||||
val[i] = (char *)malloc(512);
|
||||
memset(val[i], 0, 512);
|
||||
continue;
|
||||
}
|
||||
else if (*p == '\n')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
*(val[i] + j++) = *p;
|
||||
}
|
||||
}
|
||||
*(val[i] + j) = '\0';
|
||||
return (i+1);
|
||||
}
|
||||
|
||||
char* timeToString(struct tm *t) {
|
||||
static char s[20];
|
||||
sprintf(s, "%04d-%02d-%02d %02d:%02d:%02d",
|
||||
t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
|
||||
t->tm_hour, t->tm_min, t->tm_sec
|
||||
);
|
||||
return s;
|
||||
}
|
||||
|
||||
char *makeLogString()
|
||||
{
|
||||
struct tm *t;
|
||||
time_t timer;
|
||||
static char log_path_file[128];
|
||||
|
||||
timer = time(NULL);
|
||||
t = localtime(&timer);
|
||||
|
||||
snprintf(log_path_file, 128, "%sneo_statusd_%04d%02d%02d.log", DEFAULT_LOGFILE_PATH,
|
||||
t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
|
||||
return log_path_file;
|
||||
}
|
||||
|
||||
void WriteSHLog(char *msg, FILE *ifp)
|
||||
{
|
||||
struct tm *t;
|
||||
time_t timer;
|
||||
|
||||
timer = time(NULL);
|
||||
t = localtime(&timer);
|
||||
|
||||
fprintf(ifp, "[%s] %s\n", timeToString(t), msg);
|
||||
fflush(ifp);
|
||||
}
|
||||
@@ -0,0 +1,524 @@
|
||||
#include <sys/types.h> /* basic system data types */
|
||||
#include <sys/socket.h> /* basic socket definitions */
|
||||
#include <sys/time.h> /* timeval{} for select() */
|
||||
#include <time.h> /* timespec{} for pselect() */
|
||||
|
||||
#include <sys/time.h> /* includes <time.h> unsafely */
|
||||
|
||||
#include <netinet/in.h> /* sockaddr_in{} and other Internet defns */
|
||||
#include <arpa/inet.h> /* inet(3) functions */
|
||||
#include <errno.h>
|
||||
#include <fcntl.h> /* for nonblocking */
|
||||
#include <netdb.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h> /* for S_xxx file mode constants */
|
||||
#include <sys/uio.h> /* for iovec{} and readv/writev */
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/un.h> /* for Unix domain sockets */
|
||||
#include <semaphore.h>
|
||||
|
||||
# include <sys/select.h> /* for convenience */
|
||||
|
||||
# include <sys/param.h> /* OpenBSD prereq for sysctl.h */
|
||||
# include <sys/sysctl.h>
|
||||
|
||||
# include <poll.h> /* for convenience */
|
||||
|
||||
# include <sys/event.h> /* for kqueue */
|
||||
|
||||
# include <strings.h> /* for convenience */
|
||||
|
||||
/* Three headers are normally needed for socket/file ioctl's:
|
||||
* * <sys/ioctl.h>, <sys/filio.h>, and <sys/sockio.h>.
|
||||
* */
|
||||
# include <sys/ioctl.h>
|
||||
# include <sys/filio.h>
|
||||
# include <sys/sockio.h>
|
||||
|
||||
int
|
||||
Accept(int fd, struct sockaddr *sa, socklen_t *salenptr)
|
||||
{
|
||||
int n;
|
||||
|
||||
again:
|
||||
if ( (n = accept(fd, sa, salenptr)) < 0) {
|
||||
#ifdef EPROTO
|
||||
if (errno == EPROTO || errno == ECONNABORTED)
|
||||
#else
|
||||
if (errno == ECONNABORTED)
|
||||
#endif
|
||||
goto again;
|
||||
else
|
||||
err_sys("accept error");
|
||||
}
|
||||
return(n);
|
||||
}
|
||||
|
||||
void
|
||||
Bind(int fd, const struct sockaddr *sa, socklen_t salen)
|
||||
{
|
||||
if (bind(fd, sa, salen) < 0)
|
||||
err_sys("bind error");
|
||||
}
|
||||
|
||||
void
|
||||
Connect(int fd, const struct sockaddr *sa, socklen_t salen)
|
||||
{
|
||||
if (connect(fd, sa, salen) < 0)
|
||||
err_sys("connect error");
|
||||
}
|
||||
|
||||
void
|
||||
Getpeername(int fd, struct sockaddr *sa, socklen_t *salenptr)
|
||||
{
|
||||
if (getpeername(fd, sa, salenptr) < 0)
|
||||
err_sys("getpeername error");
|
||||
}
|
||||
|
||||
void
|
||||
Getsockname(int fd, struct sockaddr *sa, socklen_t *salenptr)
|
||||
{
|
||||
if (getsockname(fd, sa, salenptr) < 0)
|
||||
err_sys("getsockname error");
|
||||
}
|
||||
|
||||
void
|
||||
Getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlenptr)
|
||||
{
|
||||
if (getsockopt(fd, level, optname, optval, optlenptr) < 0)
|
||||
err_sys("getsockopt error");
|
||||
}
|
||||
|
||||
#ifdef HAVE_INET6_RTH_INIT
|
||||
int
|
||||
Inet6_rth_space(int type, int segments)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = inet6_rth_space(type, segments);
|
||||
if (ret < 0)
|
||||
err_quit("inet6_rth_space error");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void *
|
||||
Inet6_rth_init(void *rthbuf, socklen_t rthlen, int type, int segments)
|
||||
{
|
||||
void *ret;
|
||||
|
||||
ret = inet6_rth_init(rthbuf, rthlen, type, segments);
|
||||
if (ret == NULL)
|
||||
err_quit("inet6_rth_init error");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void
|
||||
Inet6_rth_add(void *rthbuf, const struct in6_addr *addr)
|
||||
{
|
||||
if (inet6_rth_add(rthbuf, addr) < 0)
|
||||
err_quit("inet6_rth_add error");
|
||||
}
|
||||
|
||||
void
|
||||
Inet6_rth_reverse(const void *in, void *out)
|
||||
{
|
||||
if (inet6_rth_reverse(in, out) < 0)
|
||||
err_quit("inet6_rth_reverse error");
|
||||
}
|
||||
|
||||
int
|
||||
Inet6_rth_segments(const void *rthbuf)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = inet6_rth_segments(rthbuf);
|
||||
if (ret < 0)
|
||||
err_quit("inet6_rth_segments error");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct in6_addr *
|
||||
Inet6_rth_getaddr(const void *rthbuf, int idx)
|
||||
{
|
||||
struct in6_addr *ret;
|
||||
|
||||
ret = inet6_rth_getaddr(rthbuf, idx);
|
||||
if (ret == NULL)
|
||||
err_quit("inet6_rth_getaddr error");
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_KQUEUE
|
||||
int
|
||||
Kqueue(void)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if ((ret = kqueue()) < 0)
|
||||
err_sys("kqueue error");
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
Kevent(int kq, const struct kevent *changelist, int nchanges,
|
||||
struct kevent *eventlist, int nevents, const struct timespec *timeout)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if ((ret = kevent(kq, changelist, nchanges,
|
||||
eventlist, nevents, timeout)) < 0)
|
||||
err_sys("kevent error");
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/* include Listen */
|
||||
void
|
||||
Listen(int fd, int backlog)
|
||||
{
|
||||
char *ptr;
|
||||
|
||||
/*4can override 2nd argument with environment variable */
|
||||
if ( (ptr = getenv("LISTENQ")) != NULL)
|
||||
backlog = atoi(ptr);
|
||||
|
||||
if (listen(fd, backlog) < 0)
|
||||
err_sys("listen error");
|
||||
}
|
||||
/* end Listen */
|
||||
|
||||
#ifdef HAVE_POLL
|
||||
int
|
||||
Poll(struct pollfd *fdarray, unsigned long nfds, int timeout)
|
||||
{
|
||||
int n;
|
||||
|
||||
if ( (n = poll(fdarray, nfds, timeout)) < 0)
|
||||
err_sys("poll error");
|
||||
|
||||
return(n);
|
||||
}
|
||||
#endif
|
||||
|
||||
ssize_t
|
||||
Recv(int fd, void *ptr, size_t nbytes, int flags)
|
||||
{
|
||||
ssize_t n;
|
||||
|
||||
if ( (n = recv(fd, ptr, nbytes, flags)) < 0)
|
||||
err_sys("recv error");
|
||||
return(n);
|
||||
}
|
||||
|
||||
ssize_t
|
||||
Recvfrom(int fd, void *ptr, size_t nbytes, int flags,
|
||||
struct sockaddr *sa, socklen_t *salenptr)
|
||||
{
|
||||
ssize_t n;
|
||||
|
||||
if ( (n = recvfrom(fd, ptr, nbytes, flags, sa, salenptr)) < 0)
|
||||
err_msg("recvfrom error");
|
||||
return(n);
|
||||
}
|
||||
|
||||
ssize_t
|
||||
Recvmsg(int fd, struct msghdr *msg, int flags)
|
||||
{
|
||||
ssize_t n;
|
||||
|
||||
if ( (n = recvmsg(fd, msg, flags)) < 0)
|
||||
err_sys("recvmsg error");
|
||||
return(n);
|
||||
}
|
||||
|
||||
int
|
||||
Select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
|
||||
struct timeval *timeout)
|
||||
{
|
||||
int n;
|
||||
|
||||
if ( (n = select(nfds, readfds, writefds, exceptfds, timeout)) < 0)
|
||||
err_sys("select error");
|
||||
return(n); /* can return 0 on timeout */
|
||||
}
|
||||
|
||||
void
|
||||
Send(int fd, const void *ptr, size_t nbytes, int flags)
|
||||
{
|
||||
if (send(fd, ptr, nbytes, flags) != (ssize_t)nbytes)
|
||||
err_sys("send error");
|
||||
}
|
||||
|
||||
void
|
||||
Sendto(int fd, const void *ptr, size_t nbytes, int flags,
|
||||
const struct sockaddr *sa, socklen_t salen)
|
||||
{
|
||||
if (sendto(fd, ptr, nbytes, flags, sa, salen) != (ssize_t)nbytes)
|
||||
err_sys("sendto error");
|
||||
}
|
||||
|
||||
void
|
||||
Sendmsg(int fd, const struct msghdr *msg, int flags)
|
||||
{
|
||||
unsigned int i;
|
||||
ssize_t nbytes;
|
||||
|
||||
nbytes = 0; /* must first figure out what return value should be */
|
||||
for (i = 0; i < msg->msg_iovlen; i++)
|
||||
nbytes += msg->msg_iov[i].iov_len;
|
||||
|
||||
if (sendmsg(fd, msg, flags) != nbytes)
|
||||
err_sys("sendmsg error");
|
||||
}
|
||||
|
||||
void
|
||||
Setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
|
||||
{
|
||||
if (setsockopt(fd, level, optname, optval, optlen) < 0)
|
||||
err_sys("setsockopt error");
|
||||
}
|
||||
|
||||
void
|
||||
Shutdown(int fd, int how)
|
||||
{
|
||||
if (shutdown(fd, how) < 0)
|
||||
err_sys("shutdown error");
|
||||
}
|
||||
|
||||
int
|
||||
Sockatmark(int fd)
|
||||
{
|
||||
int n;
|
||||
|
||||
if ( (n = sockatmark(fd)) < 0)
|
||||
err_sys("sockatmark error");
|
||||
return(n);
|
||||
}
|
||||
|
||||
/* include Socket */
|
||||
int
|
||||
Socket(int family, int type, int protocol)
|
||||
{
|
||||
int n;
|
||||
|
||||
if ( (n = socket(family, type, protocol)) < 0)
|
||||
err_sys("socket error");
|
||||
return(n);
|
||||
}
|
||||
/* end Socket */
|
||||
|
||||
void
|
||||
Socketpair(int family, int type, int protocol, int *fd)
|
||||
{
|
||||
int n;
|
||||
|
||||
if ( (n = socketpair(family, type, protocol, fd)) < 0)
|
||||
err_sys("socketpair error");
|
||||
}
|
||||
|
||||
ssize_t /* Write "n" bytes to a descriptor. */
|
||||
writen(int fd, const void *vptr, size_t n)
|
||||
{
|
||||
size_t nleft;
|
||||
ssize_t nwritten;
|
||||
const char *ptr;
|
||||
|
||||
ptr = vptr;
|
||||
nleft = n;
|
||||
while (nleft > 0) {
|
||||
if ( (nwritten = write(fd, ptr, nleft)) <= 0) {
|
||||
if (nwritten < 0 && errno == EINTR)
|
||||
nwritten = 0; /* and call write() again */
|
||||
else
|
||||
return(-1); /* error */
|
||||
}
|
||||
|
||||
nleft -= nwritten;
|
||||
ptr += nwritten;
|
||||
}
|
||||
return(n);
|
||||
}
|
||||
/* end writen */
|
||||
|
||||
void
|
||||
Writen(int fd, void *ptr, size_t nbytes)
|
||||
{
|
||||
if ( writen(fd, ptr, nbytes) != nbytes)
|
||||
err_sys("writen error");
|
||||
}
|
||||
|
||||
ssize_t /* Read "n" bytes from a descriptor. */
|
||||
readn(int fd, void *vptr, size_t n)
|
||||
{
|
||||
size_t nleft;
|
||||
ssize_t nread;
|
||||
char *ptr;
|
||||
|
||||
ptr = vptr;
|
||||
nleft = n;
|
||||
while (nleft > 0) {
|
||||
if ( (nread = read(fd, ptr, nleft)) < 0) {
|
||||
if (errno == EINTR)
|
||||
nread = 0; /* and call read() again */
|
||||
else
|
||||
return(-1);
|
||||
} else if (nread == 0)
|
||||
break; /* EOF */
|
||||
|
||||
nleft -= nread;
|
||||
ptr += nread;
|
||||
}
|
||||
return(n - nleft); /* return >= 0 */
|
||||
}
|
||||
/* end readn */
|
||||
|
||||
ssize_t
|
||||
Readn(int fd, void *ptr, size_t nbytes)
|
||||
{
|
||||
ssize_t n;
|
||||
|
||||
if ( (n = readn(fd, ptr, nbytes)) < 0)
|
||||
err_sys("readn error");
|
||||
return(n);
|
||||
}
|
||||
|
||||
int
|
||||
tcp_connect(const char *host, const char *serv)
|
||||
{
|
||||
int sockfd, n;
|
||||
struct addrinfo hints, *res, *ressave;
|
||||
|
||||
bzero(&hints, sizeof(struct addrinfo));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
|
||||
if ( (n = getaddrinfo(host, serv, &hints, &res)) != 0)
|
||||
err_quit("tcp_connect error for %s, %s: %s",
|
||||
host, serv, gai_strerror(n));
|
||||
ressave = res;
|
||||
|
||||
do {
|
||||
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||
if (sockfd < 0)
|
||||
continue; /* ignore this one */
|
||||
|
||||
if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0)
|
||||
break; /* success */
|
||||
|
||||
close(sockfd); /* ignore this one */
|
||||
} while ( (res = res->ai_next) != NULL);
|
||||
|
||||
if (res == NULL) /* errno set from final connect() */
|
||||
err_sys("tcp_connect error for %s, %s", host, serv);
|
||||
|
||||
freeaddrinfo(ressave);
|
||||
|
||||
return(sockfd);
|
||||
}
|
||||
/* end tcp_connect */
|
||||
|
||||
/*
|
||||
* We place the wrapper function here, not in wraplib.c, because some
|
||||
* XTI programs need to include wraplib.c, and it also defines
|
||||
* a Tcp_connect() function.
|
||||
*/
|
||||
|
||||
int
|
||||
Tcp_connect(const char *host, const char *serv)
|
||||
{
|
||||
return(tcp_connect(host, serv));
|
||||
}
|
||||
|
||||
int ConnectWait(int sockfd, struct sockaddr *saddr, int addrsize, int sec)
|
||||
{
|
||||
int newSockStat;
|
||||
int orgSockStat;
|
||||
int res, n;
|
||||
fd_set rset, wset;
|
||||
struct timeval tval;
|
||||
|
||||
int error = 0;
|
||||
int esize;
|
||||
|
||||
if ( (newSockStat = fcntl(sockfd, F_GETFL, NULL)) < 0 )
|
||||
{
|
||||
perror("F_GETFL error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
orgSockStat = newSockStat;
|
||||
newSockStat |= O_NONBLOCK;
|
||||
|
||||
// Non blocking 상태로 만든다.
|
||||
if(fcntl(sockfd, F_SETFL, newSockStat) < 0)
|
||||
{
|
||||
perror("F_SETLF error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 연결을 기다린다.
|
||||
// Non blocking 상태이므로 바로 리턴한다.
|
||||
if((res = connect(sockfd, saddr, addrsize)) < 0)
|
||||
{
|
||||
if (errno != EINPROGRESS)
|
||||
return -1;
|
||||
}
|
||||
|
||||
fprintf(stderr, "Waiting RES : %d\n", res);
|
||||
// 즉시 연결이 성공했을 경우 소켓을 원래 상태로 되돌리고 리턴한다.
|
||||
if (res == 0)
|
||||
{
|
||||
fprintf(stderr, "Waiting Connect Success\n");
|
||||
fcntl(sockfd, F_SETFL, orgSockStat);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
FD_ZERO(&rset);
|
||||
FD_SET(sockfd, &rset);
|
||||
wset = rset;
|
||||
|
||||
|
||||
tval.tv_sec = sec;
|
||||
tval.tv_usec = 0;
|
||||
|
||||
if ( (n = select(sockfd, &rset, &wset, NULL, &tval)) == 0)
|
||||
{
|
||||
// timeout
|
||||
errno = ETIMEDOUT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 읽거나 쓴 데이터가 있는지 검사한다.
|
||||
if (FD_ISSET(sockfd, &rset) || FD_ISSET(sockfd, &wset) )
|
||||
{
|
||||
fprintf(stderr, "Read data\n");
|
||||
esize = sizeof(int);
|
||||
if ((n = getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&esize)) < 0)
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
perror("Socket Not Set");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fcntl(sockfd, F_SETFL, orgSockStat);
|
||||
if(error)
|
||||
{
|
||||
errno = error;
|
||||
perror("Socket");
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user