⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.c

📁 一套客户/服务器模式的备份系统代码,跨平台,支持linux,AIX, IRIX, FreeBSD, Digital Unix (OSF1), Solaris and HP-UX.
💻 C
📖 第 1 页 / 共 5 页
字号:
/****************** Start of $RCSfile: server.c,v $  ****************** $Source: /home/alb/afbackup/afbackup-3.3.8.1/RCS/server.c,v $* $Id: server.c,v 1.7 2005/01/15 08:59:30 alb Exp alb $* $Date: 2005/01/15 08:59:30 $* $Author: alb $********* description *********************************************************************************************************************/#include <conf.h>#include <version.h>  static char * fileversion = "$RCSfile: server.c,v $ $Source: /home/alb/afbackup/afbackup-3.3.8.1/RCS/server.c,v $ $Id: server.c,v 1.7 2005/01/15 08:59:30 alb Exp alb $ " PACKAGE " " VERSION_STRING;#include <stdio.h>#include <string.h>#include <fcntl.h>#include <unistd.h>#include <stdarg.h>#include <sys/utsname.h>#ifdef	HAVE_SYS_TIME_H#include <sys/time.h>#endif#ifdef	HAVE_SYS_RESOURCE_H#include <sys/resource.h>#endif#include <sys/types.h>#ifdef  HAVE_SYS_WAIT_H#include <sys/wait.h>#endif#include <sys/socket.h>#include <netinet/in.h>#ifdef	HAVE_ARPA_INET_H#include <arpa/inet.h>#endif#include <netdb.h>#ifdef  HAVE_NETINET_IN_SYSTM_H#include <netinet/in_systm.h>#endif#ifdef  HAVE_NETINET_IP_H#include <netinet/ip.h>#endif#include <signal.h>#include <syslog.h>#include <errno.h>#ifdef  HAVE_SYS_PARAM_H#include <sys/param.h>#endif#ifdef	sun#include <sys/ioctl.h>#include <sys/mtio.h>#endif#ifdef	HAVE_SYS_MMAN_H#include <sys/mman.h>#endif#if	defined(__hpux) || defined(hpux)#ifdef	HAVE_SYS_MTIO_H#include <sys/mtio.h>#endif#endif#ifdef	HAVE_POSIX_THREADS#ifdef	HAVE_PTHREAD_H#include <pthread.h>#endif#endif#include <x_types.h>#include <sysutils.h>#include <genutils.h>#include <netutils.h>#include <fileutil.h>#include <packer.h>#define	DEBUGFILE	"/tmp/afserver.log"#include "cryptkey.h"#include "budefs.h"#include "backup.h"#include "server.h"#define	GETOUT		{ goto getout; }#define	GETOUTR(rval)	{ r = rval; goto getout; }#define	CLEANUP		{ goto cleanup; }#define	CLEANUPR(ret)	{ r = ret ; goto cleanup; }#define	BUFFEREDOP	(streamermode & BUFFERED_OPERATION)#define	AUTOCHCART	(streamermode & CHANGE_CART_ON_EOT)#define	MAX_BUFFER_MEMORY	10485760	/* 10 MB */#define	MAX_UNINTR_CYCLES	5#define	PREF_CLIENT_DELAY	5#define	BU_NO_LOCK		0#define	BU_LOCKED_RD		1#define	BU_LOCKED_WR		2#define	BU_LOCKED		(BU_LOCKED_RD | BU_LOCKED_WR)#define	BU_GOT_LOCK_RD		4#define	BU_GOT_LOCK_WR		8#define	BU_GOT_LOCK		(BU_GOT_LOCK_RD | BU_GOT_LOCK_WR)#define	BU_CANT_LOCK		64#define	LOCK_READ		1#define	LOCK_WRITE		2UChar		*programdir = "nowhere";UChar		*default_configfilenames[] = {	\				DEFAULT_SERVER_CONFIGFILES, NULL, NULL };UChar		*configfilename = NULL;UChar		*backuphome = NULL;UChar		*cryptfile = NULL;UChar		*serverid = NULL;StreamerDevice	*streamers = NULL;Int32		num_streamers = 0;ChangerDevice	*changers = NULL;Int32		num_changers = 0;Int32		curdevidx = 0;UChar		*devicesstr = NULL;UChar		*devicename = NULL;struct stat	devstatb;Int32		devprobe_interval = DEFAULT_DEVPROBEITV;UChar		*changername = NULL;UChar		*storefile = NULL;UChar		*vardir = NULL;UChar		*bindir = NULL;UChar		*libdir = NULL;UChar		*confdir = NULL;ReplSpec	replacements[] = {	{ "%B",	NULL, &bindir },	{ "%L",	NULL, &libdir },	{ "%V",	NULL, &vardir },	{ "%C", NULL, &confdir },	};UChar		streamermode = BUFFERED_OPERATION | CHANGE_CART_ON_EOT;UChar		*streamerbuffer = NULL;Uns32		streamerbufferlen = 0;Uns32		streamerbuf_insert_idx = 0;Uns32		streamerbuf_processed_idx = 0;Flag 		streamerbuf_writing = NO;Flag		streamerbuf_reading = NO;Uns32		streamerbuf_highmark;Uns32		streamerbuf_lowmark;Uns32		streamerbuf_conf_size = 0;Real		streamerbuf_conf_highmark = DEFAULT_TAPEBUFHIGHMARK;Real		streamerbuf_conf_lowmark = DEFAULT_TAPEBUFLOWMARK;int		streamerbuf_memfilefd = -1;Uns32		streamerbuf_alloced_size = 0;UChar		*streamerbuf_memfile = NULL;UChar		*streamerbuf_confstr = NULL;#define		num_blocks_in_streamerbuf	( \			streamerbuf_insert_idx >= streamerbuf_processed_idx ? \			streamerbuf_insert_idx - streamerbuf_processed_idx : \			streamerbufferlen - streamerbuf_processed_idx \				+ streamerbuf_insert_idx )Flag		at_mediaend = NO;Flag		force_cartchange = NO;Flag		newfilerequested = NO;Uns32		*tapefilenumbuf = NULL;Uns32		*numbytesread = NULL;Flag		*reqnewfilebuf = NULL;UChar		*init_cmd = NULL;UChar		*exit_cmd = NULL;UChar		*setcartcmd = NULL;UChar		*nextcartcmd = NULL;UChar		*setfilecmd = NULL;UChar		*skipfilescmd = NULL;UChar		*erasetapecmd = NULL;UChar		*initmediacmd = NULL;UChar		*weofcmd = NULL;UChar		*tapefullcmd = NULL;UChar		*tapeposfile = NULL;UChar		*bytesontape_file = NULL;UChar		*loggingfile = NULL;UChar		*syslog_ident = NULL;UChar		*statusfile = NULL;Flag		statusf_append = NO;UChar		*precious_tapes_file = NULL;UChar		*ro_tapes_file = NULL;UChar		*cartorder_file = NULL;UChar		*tape_usage_file = NULL;UChar		*used_blocksizes_file = NULL;UChar		*client_message_file = NULL;UChar		*user_to_inform = NULL;UChar		*mail_program = NULL;Int16		cartridge_handler = 0;Int32		num_cartridges = 1;Flag		full_append = NO;Flag		var_append = NO;UChar		*cartsetstr = NULL;Uns32Range	**cartridge_sets = NULL;Int32		*endcarts = NULL;Uns32		num_cartsets = 0;UChar		**cartset_clients = NULL;Int32		active_cartset = 0;	/* index i.e. cart set - 1 */Flag		reject_unlabeled = NO;Flag		prefer_cart_in_changer = NO;Int32		conf_tapeblocksize = DEFAULT_TAPEBLOCKSIZE;Int32		tapeblocksize = DEFAULT_TAPEBLOCKSIZE;Int32		lbl_tapeblocksize = -1;Uns32		maxbytes_per_file = DEFAULT_MAXBYTESPERFILE;Real64		*maxbytes_per_tape = NULL;UChar		*maxbytes_per_tape_str = NULL;Real64		bytes_on_tape = 0.0;Uns32Range	*written_tapes = NULL;UChar		*pref_client_file = NULL;int		commrfd = -1;int		commwfd = -1;char		*remotehost = "<unknown>";UChar		remoteuser[256] = "";UChar		*clientuname = &(remoteuser[128]);UChar		client_id[128] = "";int		lfd = -1;int		tapefd = -1;Flag		nobuffering = NO;UChar		*tapebuffer = NULL;Int32		outptr = 0;UChar		*inputbuffer = NULL;Int32		tapeptr = 0;Flag		endofrec = NO;Int32		buff_tape_ptr = 0;Int32		num_transf_bytes_valid = 0;Int32		commbufsiz = DEFCOMMBUFSIZ;Int32		bytes_in_buffer = 0;UChar		*iauxtapebuffer = NULL;Int32		iauxtapebufferlen = 0;Uns32		write_rate = 0;time_t		write_meas_time = 0;Uns32		read_rate = 0;time_t		read_meas_time = 0;time_t		actime;UChar		*infobuffer = NULL;Int32		infobuffersize;Flag		have_infobuffer = NO;Flag		tried_infobuffer = NO;Int32		tape_label_cartno;Int32		scnd_tape_label_cartno;Int32		tapeaccessmode = NOT_OPEN;	/* reflects the success of a						* previous open tape command */Int32		actfilenum = 0;Int32		actcart = 1;Int32		*insfilenum = NULL;Int32		*inscart = NULL;Int32		rdfilenum = 0;Int32		rdcart = 1;UChar		*tlbl_comment = NULL;		/* tape label comment */Int32		tlbl_cartno = 0;		/* for consistency check */Uns32		cartins_gracetime = DEFAULT_CARTGRACETIME;Uns32		devunavail_send_mail = DEFAULT_DEVUNAVSENDMAIL;Uns32		devunavail_give_up = DEFAULT_DEVUNAVGIVEUP;Uns32		bytes_in_tapefile = 0;Uns32		bytes_read = 0;struct utsname	unam;Flag		interrupted = NO;int		pgid_to_kill_on_interrupt = -1;Flag		tape_moved = NO;Flag		tape_rewound = NO;Flag		media_initialized = NO;Flag		filenum_incr_by_eof = NO;UChar		*cartctlcmd = NULL;char		**gargv;int		gargc;UChar		*programpath = NULL;struct stat	gstatb;			/* global multipurpose stat buffer */struct timeval	null_timeval;typedef	struct __lockData	{  UChar		*lockfile;  int		lockfd;  UChar		locked;}	LockData;LockData	g_lock;LockData	cart_lock;Flag		slavemode = NO;Flag		exiting = NO;Flag		exiting_on_cerr = NO;UChar		edebug = 0;#ifdef	HAVE_POSIX_THREADSpthread_cond_t	can_write_to_buffer, buffer_contains_data;pthread_mutex_t	may_reconfig_buffer_mutex, may_modify_buffer_ptrs;pthread_mutex_t	tapepos_mutex;pthread_t	buffer_thread;Flag		buffer_thread_running = NO;Flag		buffer_thread_initialized = NO;Flag		buffer_must_end = NO;#endif#define	EM__(nmem)	{ nomemfatal(nmem); }#define	EEM__(st)	{ if(st) nomemfatal(NULL); }#define ER__(func, i)   { if( (i = (func)) ) return(i); }ParamFileEntry	entries[] = {	{ &serverid, NULL,	(UChar *) "^[ \t]*[Ss]erver[-_ \t]*[Ii]denti\\(ty\\|fier\\):?[ \t]*",		TypeUCharPTR	},	{ &nextcartcmd, NULL,	(UChar *) "^[ \t]*[Cc]hange[-_ \t]*[Cc]artr?i?d?g?e?[-_ \t]*[Cc]o?mm?a?n?d:?[ \t]*",		TypeUCharPTR	},	{ &setcartcmd, NULL,	(UChar *) "^[ \t]*[Ss]et[-_ \t]*[Cc]artr?i?d?g?e?[-_ \t]*[Cc]o?mm?a?n?d:?[ \t]*",		TypeUCharPTR	},	{ &init_cmd, NULL,	(UChar *) "^[ \t]*[iI]niti?a?l?i?z?a?t?i?o?n?[-_ \t]*[Cc]o?mm?a?n?d:?[ \t]*",		TypeUCharPTR	},	{ &exit_cmd, NULL,	(UChar *) "^[ \t]*[eE]xit[-_ \t]*[Cc]o?mm?a?n?d:?[ \t]*",		TypeUCharPTR	},	{ &setfilecmd, NULL,	(UChar *) "^[ \t]*[Ss]et[-_ \t]*[Ff]ile[-_ \t]*[Cc]o?mm?a?n?d:?[ \t]*",		TypeUCharPTR	},	{ &skipfilescmd, NULL,	(UChar *) "^[ \t]*[Ss]kip[-_ \t]*[Ff]iles?[-_ \t]*[Cc]o?mm?a?n?d:?[ \t]*",		TypeUCharPTR	},	{ &erasetapecmd, NULL,	(UChar *) "^[ \t]*[Ee]rase[-_ \t]*[Tt]ape[-_ \t]*[Cc]o?mm?a?n?d:?[ \t]*",		TypeUCharPTR	},	{ &initmediacmd, NULL,	(UChar *) "^[ \t]*[Ii]niti?a?l?i?z?e?[-_ \t]*[Mm]edia[-_ \t]*[Cc]o?mm?a?n?d:?[ \t]*",		TypeUCharPTR	},	{ &weofcmd, NULL,	(UChar *) "^[ \t]*[Ww]r?i?t?e?[-_ \t]*[Ee][Oo][Ff][-_ \t]*[Cc]o?mm?a?n?d:?[ \t]*",		TypeUCharPTR	},	{ &tapefullcmd, NULL,	(UChar *) "^[ \t]*[Tt]ape[-_ \t]*[Ff]ull[-_ \t]*[Cc]o?mm?a?n?d:?[ \t]*",		TypeUCharPTR	},	{ &tapeposfile, NULL,	(UChar *) "^[ \t]*[Tt]ape[-_ \t]*[Pp]osi?t?i?o?n?[-_ \t]*[Ff]ile:?[ \t]*",		TypeUCharPTR	},	{ &devicesstr, NULL,	(UChar *) "^[ \t]*[Bb]ackup[-_ \t]*[Dd]evi?c?e?s?:?[ \t]*",		TypeUCharPTR	},	{ &streamerbuf_confstr, NULL,	(UChar *) "^[ \t]*\\([Ss]treamer\\|[Tt]ape\\)[-_ \t]*[Bb]uffer:?",		TypeUCharPTR	},	{ &devprobe_interval, NULL,	(UChar *) "^[ \t]*[Dd]evi?c?e?[-_ \t]*[Pp]rob\\(e\\|ing\\)[-_ \t]*[Ii]nterval:?",		TypeInt32	},	{ &full_append, NULL,	(UChar *) "^[ \t]*\\([Ff]ull[-_ \t]*\\)?[Aa]ppend\\([-_ \t]*[Mm]ode\\)?:?",		TypeFlag	},	{ &var_append, NULL,	(UChar *) "^[ \t]*[Vv]ar\\(iable\\)?[-_ \t]*[Aa]ppend\\([-_ \t]*[Mm]ode\\)?:?",		TypeFlag	},	{ &prefer_cart_in_changer, NULL,	(UChar *) "^[ \t]*[Pp]refer[-_ \t]*[Cc]artr?i?d?g?e?s?[-_ \t]*[Ii]n[-_ \t]*[Cc]hanger:?",		TypeFlag	},	{ &loggingfile, NULL,	(UChar *) "^[ \t]*[Ll]ogg?i?n?g?[-_ \t]*[Ff]ile:?[ \t]*",		TypeUCharPTR	},	{ &statusfile, NULL,	(UChar *) "^[ \t]*[Ss]tatus[-_ \t]*[Ff]ile:?[ \t]*",		TypeUCharPTR	},	{ &g_lock.lockfile, NULL,	(UChar *) "^[ \t]*[Ll]ocki?n?g?[-_ \t]*[Ff]ile:?[ \t]*",		TypeUCharPTR	},	{ &user_to_inform, NULL,	(UChar *) "^[ \t]*[Uu]ser[-_ \t]*[Tt]o[-_ \t]*[Ii]nfor?m?:?[ \t]*",		TypeUCharPTR	},	{ &mail_program, NULL,	(UChar *) "^[ \t]*[Mm]ail[-_ \t]*[Pp]rogram:?[ \t]*",		TypeUCharPTR	},	{ &cartridge_handler, NULL,	(UChar *) "^[ \t]*[Cc]artr?i?d?g?e?[-_ \t]*[Hh]andler:?[ \t]*",		TypeInt16	},	{ &reject_unlabeled, NULL,	(UChar *) "^[ \t]*[Rr]eject[-_ \t]*[Uu]nlabeled\\([-_ \t]*[Tt]apes?\\)?:?[ \t]*",		TypeFlag	},	{ &num_cartridges, NULL,	(UChar *) "^[ \t]*[Nn]umb?e?r?[-_ \t]*[Oo]f[-_ \t]*[Cc]artr?i?d?g?e?s[-_ \t]*:?",		TypeInt32	},	{ &endcarts, &num_cartsets,	(UChar *) "^[ \t]*[Ll]ast[-_ \t]*[Cc]artr?i?d?g?e?s[-_ \t]*:?",		TypeInt32PTR	},	{ &cartsetstr, NULL,	(UChar *) "^[ \t]*[Cc]artr?i?d?g?e?[-_ \t]*[Ss]ets:?",		TypeUCharPTR	},	{ &conf_tapeblocksize, NULL,	(UChar *) "^[ \t]*[Tt]ape[-_ \t]*[Bb]lock[-_ \t]*[Ss]ize:?",		TypeInt32	},	{ &maxbytes_per_file, NULL,	(UChar *) "^[ \t]*[Mm]axi?m?u?m?[-_ \t]*[Bb]ytes[-_ \t]*[Pp]er[-_ \t]*[Ff]ile:?",		TypeUns32	},	{ &maxbytes_per_tape_str, NULL,	(UChar *) "^[ \t]*[Mm]axi?m?u?m?[-_ \t]*[Bb]ytes[-_ \t]*[Pp]er[-_ \t]*[Tt]apes?:?",		TypeUCharPTR	},	{ &cartins_gracetime, NULL,	(UChar *) "^[ \t]*[Cc]artr?i?d?g?e?[-_ \t]*[Ii]nse?r?t?[-_ \t]*[Gg]race[-_ \t]*[Tt]ime:?",		TypeUns32	},	{ &devunavail_send_mail, NULL,	(UChar *) "^[ \t]*[Dd]evi?c?e?[-_ \t]*[Uu]n[Aa]vaila?b?l?e?[-_ \t]*[Ss]end[-_ \t]*[Mm]ail[-_ \t]*\\([Aa]fter[-_ \t]*\\)?[Mm]inu?t?e?s?:?",		TypeUns32	},	{ &devunavail_give_up, NULL,	(UChar *) "^[ \t]*[Dd]evi?c?e?[-_ \t]*[Uu]n[Aa]vaila?b?l?e?[-_ \t]*[Gg]ive[-_ \t]*[Uu]p[-_ \t]*\\([Aa]fter[-_ \t]*\\)?[Mm]inu?t?e?s?:?",		TypeUns32	},	{ &programdir, NULL,	(UChar *) "^[ \t]*[Pp]rogram[-_ \t]*[Dd]ire?c?t?o?r?y?:?[ \t]*",		TypeUCharPTR	},	{ &cryptfile, NULL,	(UChar *) "^[ \t]*\\([Ee]n\\)?[Cc]rypti?o?n?[-_ \t]*[Kk]ey[-_ \t]*[Ff]iles?:?[ \t]*",		TypeUCharPTR	},	{ &vardir, NULL,	(UChar *) "^[ \t]*[Vv][Aa][Rr][-_ \t]*[Dd]ire?c?t?o?r?y?:?[ \t]*",		TypeUCharPTR	},	};UChar	*lockdirs[] = {		"/var/locks", "/var/lock", "/var/spool/locks",		"/var/spool/lock", "/var/tmp", "/tmp", NULL,};#define	repl_dirs(string)	repl_substrings((string), replacements,	\		sizeof(replacements) / sizeof(replacements[0]))#define	cart_seq_after_eject(act)	\		((act) + 1 > num_cartridges ? 1 : (act) + 1)#define	TAPE_NOT_CLOSED	\		(tapeaccessmode != NOT_OPEN && tapeaccessmode != CLOSED_NOTREW)#define	TAPE_CLOSED	\		(tapeaccessmode == NOT_OPEN || tapeaccessmode == CLOSED_NOTREW)void	do_exit(int);Int32	set_cartridge_cmd(Int32);Int32	set_cartridge(Int32, Flag);Int32	set_filenum_cmd(Int32);Int32	set_filenum(Int32, Flag, Flag);Int32	update_devicesettings(Int32, Flag);Int32	skip_files_cmd(Int32);Int32	skip_files(Int32);Int32	set_cartset_cmd(Int32);Int32	poll_device_cmd(UChar *, Flag, Int32);Int32	closetape(Flag);Int32	closetape_cmd();Int32	closetapen_cmd();Int32	opentapewr_cmd(Uns32);Int32	opentapewr(Uns32);Int32	opentaperd_cmd(Uns32);Int32	opentaperd(Uns32);Int32	read_from_tape();Int32	erasetape();Int32	erasetape_cmd();Int32	requestnewcart_cmd();Int32	rewindtape(Flag, Flag);Int32	initmedia();Int32	sendtapepos();Int32	sendtapewrpos();Int32	sendtaperdpos();Int32	sendwrtapes(Uns32Range *);Int32	sendclientstapes();Int32	sendvardata(Uns32, Int32, UChar *, Int32, UChar *);Int32	osendtapepos();Int32	osendtapewrpos();Int32	setnumwritevalid();Int32	set_commbufsiz(Int32);Int32	sendint(Int32, Int32);Int32	sendifbusy();Int32	flush_buffer_to_tape(UChar *, Int32 *, Int32 *);Int32	write_tape_buffer(Int32 *, Int32 *);Int32	get_tape_buffer(UChar *, Int32 *, Int32 *);Int32	write_infobuffer(Int32);Int32	goodbye(Int32);Int32	goodbye_cmd();Int32	read_tapepos_file();Int32	write_tapepos_file();Int32	write_tapepos_file_msg();Int32	get_bytes_per_tape_msg(UChar *, Int32, Real64 *, Int32 *, Flag *, time_t *);Int32	save_bytes_per_tape_msg(UChar *, Int32, Real64, Int32, Flag, time_t);Int32	save_cart_order(Int32, Int32, Int32);Int32	run_tape_full_cmd(Int32, Int32);Int32	run_tape_full_cmd_msg(Int32, Int32);Int32	tape_full_actions(Int32);Int32	find_next_free_pos(Int32 *, Int32 *, Int32, Int32);Int32	next_read_pos(Int32 *, Int32 *, Int32, Int32);Int32	can_overwrite_cart(Flag *, Int32);Int32	write_to_tape();Int32	client_backup_cmd(Int8);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -