Esempio n. 1
0
void topology_load(void) {
	FILE *fd;
	char linebuff[10000];
	uint32_t lineno;
	uint32_t fip,tip,rid;
	void *newtree;

	fd = fopen(TopologyFileName,"r");
	if (fd==NULL) {
		if (errno==ENOENT) {
			if (racktree) {
				syslog(LOG_WARNING,"mfstopology configuration file (%s) not found - network topology not changed",TopologyFileName);
			} else {
				syslog(LOG_WARNING,"mfstopology configuration file (%s) not found - network topology not defined",TopologyFileName);
			}
			fprintf(stderr,"mfstopology configuration file (%s) not found - using defaults\n",TopologyFileName);
		} else {
			if (racktree) {
				mfs_arg_errlog(LOG_WARNING,"can't open mfstopology configuration file (%s) - network topology not changed, error",TopologyFileName);
			} else {
				mfs_arg_errlog(LOG_WARNING,"can't open mfstopology configuration file (%s) - network topology not defined, error",TopologyFileName);
			}
		}
		return;
	}

//	hash_clear();
	newtree = NULL;
	lineno = 1;
	while (fgets(linebuff,10000,fd)) {
		if (topology_parseline(linebuff,lineno,&fip,&tip,&rid)>=0) {
			newtree = itree_add_interval(newtree,fip,tip,rid);
//			while (fip<=tip) {
//				hash_insert(fip,rid);
//				fip++;
//			}
		}
		lineno++;
	}
	if (ferror(fd)) {
		fclose(fd);
		if (racktree) {
			syslog(LOG_WARNING,"error reading mfstopology file - network topology not changed");
		} else {
			syslog(LOG_WARNING,"error reading mfstopology file - network topology not defined");
		}
		itree_freeall(newtree);
		fprintf(stderr,"error reading mfstopology file - network topology not defined (using defaults)\n");
		return;
	}
	fclose(fd);
	itree_freeall(racktree);
	racktree = newtree;
	if (racktree) {
		racktree = itree_rebalance(racktree);
	}
	mfs_syslog(LOG_NOTICE,"topology file has been loaded");
}
Esempio n. 2
0
void topology_reload(void) {
	int fd;
	if (TopologyFileName) {
		free(TopologyFileName);
	}
	if (!cfg_isdefined("TOPOLOGY_FILENAME")) {
		TopologyFileName = strdup(ETC_PATH "/mfs/mfstopology.cfg");
		passert(TopologyFileName);
		if ((fd = open(TopologyFileName,O_RDONLY))<0 && errno==ENOENT) {
			free(TopologyFileName);
			TopologyFileName = strdup(ETC_PATH "/mfstopology.cfg");
			if ((fd = open(TopologyFileName,O_RDONLY))>=0) {
				mfs_syslog(LOG_WARNING,"default sysconf path has changed - please move mfstopology.cfg from "ETC_PATH"/ to "ETC_PATH"/mfs/");
			}
		}
		if (fd>=0) {
			close(fd);
		}
	} else {
		TopologyFileName = cfg_getstr("TOPOLOGY_FILENAME",ETC_PATH "/mfs/mfstopology.cfg");
	}
	topology_load();
}
Esempio n. 3
0
int flock_load(bio *fd,uint8_t mver,uint8_t ignoreflag) {
	uint8_t loadbuff[FLOCK_REC_SIZE];
	const uint8_t *ptr;
	int32_t r;
	uint32_t inode,sessionid;
	uint64_t owner;
	uint8_t ltype;
	inodelocks *il;
	lock *l;

	if (mver!=0x10) {
		return -1;
	}

	for (;;) {
		r = bio_read(fd,loadbuff,FLOCK_REC_SIZE);
		if (r!=FLOCK_REC_SIZE) {
			return -1;
		}
		ptr = loadbuff;
		inode = get32bit(&ptr);
		owner = get64bit(&ptr);
		sessionid = get32bit(&ptr);
		ltype = get8bit(&ptr);
		if (inode==0 && owner==0 && sessionid==0) {
			return 0;
		}
		if (of_checknode(sessionid,inode)==0) {
			if (ignoreflag) {
				mfs_syslog(LOG_ERR,"loading flock_locks: lock on closed file !!! (ignoring)");
				continue;
			} else {
				mfs_syslog(LOG_ERR,"loading flock_locks: lock on closed file !!!");
				return -1;
			}
		}
		// add lock
		il = flock_inode_find(inode);
		if (il==NULL) {
			il = flock_inode_new(inode);
		}
		if (il->active!=NULL && (il->active->ltype==LTYPE_WRITER || ltype==LTYPE_WRITER)) {
			if (ignoreflag) {
				mfs_syslog(LOG_ERR,"loading flock_locks: wrong lock !!! (ignoring)");
				continue;
			} else {
				mfs_syslog(LOG_ERR,"loading flock_locks: wrong lock !!!");
				return -1;
			}
		}
		l = malloc(sizeof(lock));
		l->owner = owner;
		l->sessionid = sessionid;
		l->state = STATE_ACTIVE;
		l->ltype = ltype;
		l->lock_instances = NULL;
		l->parent = il;
		l->next = NULL;
		l->prev = NULL;
		flock_do_lock_inode_attach(l);
	}
	return 0; // unreachable
}
Esempio n. 4
0
int main(int argc,char **argv) {
	char *logappname;
//	char *lockfname;
	char *wrkdir;
	char *cfgfile;
	char *appname;
	int ch;
	uint8_t runmode;
	int rundaemon,logundefined;
	int lockmemory;
	int32_t nicelevel;
	uint32_t locktimeout;
	int fd;
	uint8_t movewarning;
	struct rlimit rls;

	strerr_init();
	mycrc32_init();

	movewarning = 0;
	cfgfile=strdup(ETC_PATH "/mfs/" STR(APPNAME) ".cfg");
	passert(cfgfile);
	if ((fd = open(cfgfile,O_RDONLY))<0 && errno==ENOENT) {
		free(cfgfile);
		cfgfile=strdup(ETC_PATH "/" STR(APPNAME) ".cfg");
		passert(cfgfile);
		if ((fd = open(cfgfile,O_RDONLY))>=0) {
			movewarning = 1;
		}
	}
	if (fd>=0) {
		close(fd);
	}
	locktimeout = 1800;
	rundaemon = 1;
	runmode = RM_RESTART;
	logundefined = 0;
	lockmemory = 0;
	appname = argv[0];

	while ((ch = getopt(argc, argv, "uvdfsc:t:h?")) != -1) {
		switch(ch) {
			case 'v':
				printf("version: %u.%u.%u\n",VERSMAJ,VERSMID,VERSMIN);
				return 0;
			case 'd':
				rundaemon=0;
				break;
			case 'f':
				runmode=RM_START;
				break;
			case 's':
				runmode=RM_STOP;
				break;
			case 't':
				locktimeout=strtoul(optarg,NULL,10);
				break;
			case 'c':
				free(cfgfile);
				cfgfile = strdup(optarg);
				passert(cfgfile);
				movewarning = 0;
				break;
			case 'u':
				logundefined=1;
				break;
			default:
				usage(appname);
				return 1;
		}
	}
	argc -= optind;
	argv += optind;
	if (argc==1) {
		if (strcasecmp(argv[0],"start")==0) {
			runmode = RM_START;
		} else if (strcasecmp(argv[0],"stop")==0) {
			runmode = RM_STOP;
		} else if (strcasecmp(argv[0],"restart")==0) {
			runmode = RM_RESTART;
		} else if (strcasecmp(argv[0],"reload")==0) {
			runmode = RM_RELOAD;
		} else if (strcasecmp(argv[0],"test")==0) {
			runmode = RM_TEST;
		} else if (strcasecmp(argv[0],"kill")==0) {
			runmode = RM_KILL;
		} else {
			usage(appname);
			return 1;
		}
	} else if (argc!=0) {
		usage(appname);
		return 1;
	}

	if (movewarning) {
		mfs_syslog(LOG_WARNING,"default sysconf path has changed - please move " STR(APPNAME) ".cfg from "ETC_PATH"/ to "ETC_PATH"/mfs/");
	}

	if ((runmode==RM_START || runmode==RM_RESTART) && rundaemon) {
		makedaemon();
	} else {
		if (runmode==RM_START || runmode==RM_RESTART) {
			set_signal_handlers(0);
		}
	}

	if (cfg_load(cfgfile,logundefined)==0) {
		fprintf(stderr,"can't load config file: %s - using defaults\n",cfgfile);
	}
	free(cfgfile);

	logappname = cfg_getstr("SYSLOG_IDENT",STR(APPNAME));

	if (rundaemon) {
		if (logappname[0]) {
			openlog(logappname, LOG_PID | LOG_NDELAY , LOG_DAEMON);
		} else {
			openlog(STR(APPNAME), LOG_PID | LOG_NDELAY , LOG_DAEMON);
		}
	} else {
#if defined(LOG_PERROR)
		if (logappname[0]) {
			openlog(logappname, LOG_PID | LOG_NDELAY | LOG_PERROR, LOG_USER);
		} else {
			openlog(STR(APPNAME), LOG_PID | LOG_NDELAY | LOG_PERROR, LOG_USER);
		}
#else
		if (logappname[0]) {
			openlog(logappname, LOG_PID | LOG_NDELAY, LOG_USER);
		} else {
			openlog(STR(APPNAME), LOG_PID | LOG_NDELAY, LOG_USER);
		}
#endif
	}

	if (runmode==RM_START || runmode==RM_RESTART) {
		rls.rlim_cur = MFSMAXFILES;
		rls.rlim_max = MFSMAXFILES;
		if (setrlimit(RLIMIT_NOFILE,&rls)<0) {
			syslog(LOG_NOTICE,"can't change open files limit to %u",MFSMAXFILES);
		}

		lockmemory = cfg_getnum("LOCK_MEMORY",0);
#ifdef MFS_USE_MEMLOCK
		if (lockmemory) {
			rls.rlim_cur = RLIM_INFINITY;
			rls.rlim_max = RLIM_INFINITY;
			setrlimit(RLIMIT_MEMLOCK,&rls);
		}
#endif
		nicelevel = cfg_getint32("NICE_LEVEL",-19);
		setpriority(PRIO_PROCESS,getpid(),nicelevel);
	}

	changeugid();

	wrkdir = cfg_getstr("DATA_PATH",DATA_PATH);
	if (runmode==RM_START || runmode==RM_RESTART) {
		fprintf(stderr,"working directory: %s\n",wrkdir);
	}

	if (chdir(wrkdir)<0) {
		mfs_arg_syslog(LOG_ERR,"can't set working directory to %s",wrkdir);
		if (rundaemon) {
			fputc(0,stderr);
			close_msg_channel();
		}
		closelog();
		free(logappname);
		return 1;
	}
	free(wrkdir);

	umask(cfg_getuint32("FILE_UMASK",027)&077);

	/* for upgrading from previous versions of MFS */
	if (check_old_locks(runmode,locktimeout)<0) {
		if (rundaemon) {
			fputc(0,stderr);
			close_msg_channel();
		}
		closelog();
		free(logappname);
		wdunlock();
		return 1;
	}

	if (wdlock(runmode,locktimeout)<0) {
		if (rundaemon) {
			fputc(0,stderr);
			close_msg_channel();
		}
		closelog();
		free(logappname);
		wdunlock();
		return 1;
	}

	remove_old_wdlock();

	if (runmode==RM_STOP || runmode==RM_KILL || runmode==RM_RELOAD || runmode==RM_TEST) {
		if (rundaemon) {
			close_msg_channel();
		}
		closelog();
		free(logappname);
		wdunlock();
		return 0;
	}

#ifdef MFS_USE_MEMLOCK
	if (lockmemory) {
		if (getrlimit(RLIMIT_MEMLOCK,&rls)<0) {
			mfs_errlog(LOG_WARNING,"error getting memory lock limits");
		} else {
			if (rls.rlim_cur!=RLIM_INFINITY && rls.rlim_max==RLIM_INFINITY) {
				rls.rlim_cur = RLIM_INFINITY;
				rls.rlim_max = RLIM_INFINITY;
				if (setrlimit(RLIMIT_MEMLOCK,&rls)<0) {
					mfs_errlog(LOG_WARNING,"error setting memory lock limit to unlimited");
				}
			}
			if (getrlimit(RLIMIT_MEMLOCK,&rls)<0) {
				mfs_errlog(LOG_WARNING,"error getting memory lock limits");
			} else {
				if (rls.rlim_cur!=RLIM_INFINITY) {
					mfs_errlog(LOG_WARNING,"can't set memory lock limit to unlimited");
				} else {
					if (mlockall(MCL_CURRENT|MCL_FUTURE)<0) {
						mfs_errlog(LOG_WARNING,"memory lock error");
					} else {
						mfs_syslog(LOG_NOTICE,"process memory was successfully locked in RAM");
					}
			}	}
		}
	}
#else
	if (lockmemory) {
		mfs_syslog(LOG_WARNING,"memory lock not supported !!!");
	}
#endif
	fprintf(stderr,"initializing %s modules ...\n",logappname);

	if (initialize()) {
		if (getrlimit(RLIMIT_NOFILE,&rls)==0) {
			syslog(LOG_NOTICE,"open files limit: %lu",(unsigned long)(rls.rlim_cur));
		}
		fprintf(stderr,"%s daemon initialized properly\n",logappname);
		if (rundaemon) {
			close_msg_channel();
		}
		if (initialize_late()) {
			mainloop();
			ch=0;
		} else {
			ch=1;
		}
	} else {
		fprintf(stderr,"error occured during initialization - exiting\n");
		if (rundaemon) {
			fputc(0,stderr);
			close_msg_channel();
		}
		ch=1;
	}
	destruct();
	free_all_registered_entries();
	signal_cleanup();
	cfg_term();
	strerr_term();
	closelog();
	free(logappname);
	wdunlock();
	return ch;
}
Esempio n. 5
0
int check_old_locks(uint8_t runmode,uint32_t timeout) {
	char str[13];
	uint32_t l;
	pid_t ptk;
	char *lockfname;

	lockfname = cfg_getstr("LOCK_FILE",RUN_PATH "/" STR(APPNAME) ".lock");
	lfd=open(lockfname,O_RDWR);
	if (lfd<0) {
		if (errno==ENOENT) {    // no old lock file
			free(lockfname);
			return 0;	// ok
		}
		mfs_arg_errlog(LOG_ERR,"open %s error",lockfname);
		free(lockfname);
		return -1;
	}
	if (lockf(lfd,F_TLOCK,0)<0) {
		if (errno!=EAGAIN) {
			mfs_arg_errlog(LOG_ERR,"lock %s error",lockfname);
			free(lockfname);
			return -1;
		}
		if (runmode==RM_START) {
			mfs_syslog(LOG_ERR,"old lockfile is locked - can't start");
			free(lockfname);
			return -1;
		}
		if (runmode==RM_STOP || runmode==RM_KILL || runmode==RM_RESTART) {
			fprintf(stderr,"old lockfile found - trying to kill previous instance using data from old lockfile\n");
		} else if (runmode==RM_RELOAD) {
			fprintf(stderr,"old lockfile found - sending reload signal using data from old lockfile\n");
		}
		l=read(lfd,str,13);
		if (l==0 || l>=13) {
			mfs_arg_syslog(LOG_ERR,"wrong pid in old lockfile %s",lockfname);
			free(lockfname);
			return -1;
		}
		str[l]=0;
		ptk = strtol(str,NULL,10);
		if (runmode==RM_RELOAD) {
			if (kill(ptk,SIGHUP)<0) {
				mfs_errlog(LOG_WARNING,"can't send reload signal");
				free(lockfname);
				return -1;
			}
			fprintf(stderr,"reload signal has beed sent\n");
			return 0;
		}
		if (runmode==RM_KILL) {
			fprintf(stderr,"sending SIGKILL to previous instance (pid:%ld)\n",(long int)ptk);
			if (kill(ptk,SIGKILL)<0) {
				mfs_errlog(LOG_WARNING,"can't kill previous process");
				free(lockfname);
				return -1;
			}
		} else {
			fprintf(stderr,"sending SIGTERM to previous instance (pid:%ld)\n",(long int)ptk);
			if (kill(ptk,SIGTERM)<0) {
				mfs_errlog(LOG_WARNING,"can't kill previous process");
				free(lockfname);
				return -1;
			}
		}
		l=0;
		fprintf(stderr,"waiting for termination ...\n");
		while (lockf(lfd,F_TLOCK,0)<0) {
			if (errno!=EAGAIN) {
				mfs_arg_errlog(LOG_ERR,"lock %s error",lockfname);
				free(lockfname);
				return -1;
			}
			sleep(1);
			l++;
			if (l>=timeout) {
				mfs_arg_syslog(LOG_ERR,"about %"PRIu32" seconds passed and old lockfile is still locked - giving up",l);
				free(lockfname);
				return -1;
			}
			if (l%10==0) {
				mfs_arg_syslog(LOG_WARNING,"about %"PRIu32" seconds passed and old lockfile is still locked",l);
			}
		}
		fprintf(stderr,"terminated\n");
	} else {
		fprintf(stderr,"found unlocked old lockfile\n");
		if (runmode==RM_RELOAD) {
			fprintf(stderr,"can't obtain process id using old lockfile\n");
			return 0;
		}
	}
	fprintf(stderr,"removing old lockfile\n");
	close(lfd);
	unlink(lockfname);
	free(lockfname);
	return 0;
}
Esempio n. 6
0
int sclass_load(bio *fd,uint8_t mver,int ignoreflag) {
	uint8_t *databuff = NULL;
	const uint8_t *ptr;
	uint32_t labelmask;
	uint32_t chunkcount;
	uint16_t sclassid;
	uint16_t arch_delay;
	uint8_t create_mode;
	uint8_t create_labelscnt;
	uint8_t keep_labelscnt;
	uint8_t arch_labelscnt;
	uint8_t descrleng;
	uint8_t nleng;
	uint8_t admin_only;
	uint8_t name[MAXSCLASSNLENG];
	uint8_t i,j;
	uint8_t orgroup;
	uint8_t hdrleng;

	if (mver<0x16) { // skip label descriptions
		for (i=0 ; i<26 ; i++) {
			if (bio_read(fd,&descrleng,1)!=1) {
				int err = errno;
				fputc('\n',stderr);
				errno = err;
				mfs_errlog(LOG_ERR,"loading storage class data: read error");
				return -1;
			}
			if (descrleng>128) {
				mfs_syslog(LOG_ERR,"loading storage class data: description too long");
				return -1;
			}
			bio_skip(fd,descrleng);
		}
	}
	if (mver==0x10) {
		orgroup = 1;
	} else {
		if (bio_read(fd,&orgroup,1)!=1) {
			int err = errno;
			fputc('\n',stderr);
			errno = err;
			mfs_errlog(LOG_ERR,"loading storage class: read error");
			return -1;
		}
		if (orgroup>MASKORGROUP) {
			if (ignoreflag) {
				mfs_syslog(LOG_ERR,"loading storage class data: too many or-groups - ignore");
			} else {
				mfs_syslog(LOG_ERR,"loading storage class data: too many or-groups");
				return -1;
			}
		}
	}
	if (orgroup<1) {
		mfs_syslog(LOG_ERR,"loading storage class data: zero or-groups !!!");
		return -1;
	}
	databuff = malloc(3U*9U*4U*(uint32_t)orgroup);
	passert(databuff);
	hdrleng = (mver==0x12)?11:(mver<=0x13)?3:(mver<=0x14)?5:(mver<=0x15)?8:10;
	while (1) {
		if (bio_read(fd,databuff,hdrleng)!=hdrleng) {
			int err = errno;
			fputc('\n',stderr);
			errno = err;
			mfs_errlog(LOG_ERR,"loading storage class data: read error");
			free(databuff);
			databuff=NULL;
			return -1;
		}
		ptr = databuff;
		sclassid = get16bit(&ptr);
		if (mver>0x15) {
			nleng = get8bit(&ptr);
			admin_only = get8bit(&ptr);
			create_mode = get8bit(&ptr);
			arch_delay = get16bit(&ptr);
			create_labelscnt = get8bit(&ptr);
			keep_labelscnt = get8bit(&ptr);
			arch_labelscnt = get8bit(&ptr);
			chunkcount = 0;
		} else if (mver>0x14) {
			nleng = 0;
			admin_only = 0;
			create_mode = get8bit(&ptr);
			arch_delay = get16bit(&ptr);
			create_labelscnt = get8bit(&ptr);
			keep_labelscnt = get8bit(&ptr);
			arch_labelscnt = get8bit(&ptr);
			chunkcount = 0;
		} else if (mver>0x13) {
			nleng = 0;
			admin_only = 0;
			create_mode = get8bit(&ptr);
			create_labelscnt = get8bit(&ptr);
			keep_labelscnt = get8bit(&ptr);
			arch_labelscnt = keep_labelscnt;
			arch_delay = 0;
			chunkcount = 0;
		} else {
			nleng = 0;
			admin_only = 0;
			create_labelscnt = get8bit(&ptr);
			keep_labelscnt = create_labelscnt;
			arch_labelscnt = create_labelscnt;
			create_mode = CREATE_MODE_STD;
			arch_delay = 0;
			if (mver==0x12) {
				chunkcount = get32bit(&ptr);
				ptr+=4;
			} else {
				chunkcount = 0;
			}
		}
		if (nleng==0) {
			if (sclassid>=FIRSTSCLASSID) {
				nleng = snprintf((char*)name,MAXSCLASSNLENG,"sclass_%"PRIu32,(uint32_t)(sclassid+1-FIRSTSCLASSID));
			} else {
				nleng = 0;
			}
		} else {
			if (bio_read(fd,name,nleng)!=nleng) {
				int err = errno;
				fputc('\n',stderr);
				errno = err;
				mfs_errlog(LOG_ERR,"loading storage class data: read error");
				free(databuff);
				databuff=NULL;
				return -1;
			}
		}
		if (sclassid==0 && create_labelscnt==0 && keep_labelscnt==0 && arch_labelscnt==0 && chunkcount==0 && arch_delay==0) {
			break;
		}
		if (create_labelscnt==0 || create_labelscnt>MAXLABELSCNT || keep_labelscnt==0 || keep_labelscnt>MAXLABELSCNT || arch_labelscnt==0 || arch_labelscnt>MAXLABELSCNT) {
			mfs_arg_syslog(LOG_ERR,"loading storage class data: data format error (sclassid: %"PRIu16" ; create_mode: %"PRIu8" ; create_labelscnt: %"PRIu8" ; keep_labelscnt: %"PRIu8" ; arch_labelscnt: %"PRIu8" ; arch_delay: %"PRIu16")",sclassid,create_mode,create_labelscnt,keep_labelscnt,arch_labelscnt,arch_delay);
			free(databuff);
			databuff = NULL;
			return -1;
		}
		if (sclassid==0 || sclassid>=MAXSCLASS || nleng==0) {
			if (ignoreflag) {
				mfs_arg_syslog(LOG_ERR,"loading storage class data: bad sclassid (%"PRIu16") - ignore",sclassid);
				if (mver>0x14) {
					bio_skip(fd,(create_labelscnt+keep_labelscnt+arch_labelscnt)*4*orgroup);
				} else if (mver>0x13) {
					bio_skip(fd,(create_labelscnt+keep_labelscnt)*4*orgroup);
				} else {
					bio_skip(fd,(create_labelscnt)*4*orgroup);
				}
				continue;
			} else {
				mfs_arg_syslog(LOG_ERR,"loading storage class data: bad sclassid (%"PRIu16")",sclassid);
				free(databuff);
				databuff=NULL;
				return -1;
			}
		}
		if (mver>0x14) {
			if (bio_read(fd,databuff,(create_labelscnt+keep_labelscnt+arch_labelscnt)*4*orgroup)!=(create_labelscnt+keep_labelscnt+arch_labelscnt)*4*orgroup) {
				int err = errno;
				fputc('\n',stderr);
				errno = err;
				mfs_errlog(LOG_ERR,"loading storage class data: read error");
				free(databuff);
				databuff=NULL;
				return -1;
			}
		} else if (mver>0x13) {
			if (bio_read(fd,databuff,(create_labelscnt+keep_labelscnt)*4*orgroup)!=(create_labelscnt+keep_labelscnt)*4*orgroup) {
				int err = errno;
				fputc('\n',stderr);
				errno = err;
				mfs_errlog(LOG_ERR,"loading storage class data: read error");
				free(databuff);
				databuff=NULL;
				return -1;
			}
		} else {
			if (bio_read(fd,databuff,create_labelscnt*4*orgroup)!=create_labelscnt*4*orgroup) {
				int err = errno;
				fputc('\n',stderr);
				errno = err;
				mfs_errlog(LOG_ERR,"loading storage class data: read error");
				free(databuff);
				databuff=NULL;
				return -1;
			}
		}
		if (sclassid>=FIRSTSCLASSID && sclasstab[sclassid].nleng>0) {
			if (ignoreflag) {
				mfs_syslog(LOG_ERR,"loading storage class data: repeated sclassid - ignore");
				if (chunkcount>0) {
					bio_skip(fd,chunkcount*8);
				}
				continue;
			} else {
				mfs_syslog(LOG_ERR,"loading storage class data: repeated sclassid");
				free(databuff);
				databuff=NULL;
				return -1;
			}
		}

		ptr = databuff;
		for (i=0 ; i<create_labelscnt ; i++) {
			for (j=0 ; j<MASKORGROUP ; j++) {
				if (j<orgroup) {
					labelmask = get32bit(&ptr);
				} else {
					labelmask = 0;
				}
				sclasstab[sclassid].create_labelmasks[i][j] = labelmask;
			}
		}
		for (i=0 ; i<keep_labelscnt ; i++) {
			for (j=0 ; j<MASKORGROUP ; j++) {
				if (mver>0x13) {
					if (j<orgroup) {
						labelmask = get32bit(&ptr);
					} else {
						labelmask = 0;
					}
				} else {
					labelmask = sclasstab[sclassid].create_labelmasks[i][j];
				}
				sclasstab[sclassid].keep_labelmasks[i][j] = labelmask;
			}
		}
		for (i=0 ; i<arch_labelscnt ; i++) {
			for (j=0 ; j<MASKORGROUP ; j++) {
				if (mver>0x14) {
					if (j<orgroup) {
						labelmask = get32bit(&ptr);
					} else {
						labelmask = 0;
					}
				} else {
					labelmask = sclasstab[sclassid].keep_labelmasks[i][j];
				}
				sclasstab[sclassid].arch_labelmasks[i][j] = labelmask;
			}
		}
		sclasstab[sclassid].create_mode = create_mode;
		sclasstab[sclassid].arch_delay = arch_delay;
		sclasstab[sclassid].create_labelscnt = create_labelscnt;
		sclasstab[sclassid].keep_labelscnt = keep_labelscnt;
		sclasstab[sclassid].arch_labelscnt = arch_labelscnt;
		sclasstab[sclassid].admin_only = admin_only;
		sclasstab[sclassid].nleng = nleng;
		memcpy(sclasstab[sclassid].name,name,nleng);
		sclasstab[sclassid].files = 0;
		sclasstab[sclassid].directories = 0;
		sclass_fix_has_labels_fields(sclassid);
		if (sclassid>=firstneverused) {
			firstneverused = sclassid+1;
		}
		if (chunkcount>0) {
			bio_skip(fd,chunkcount*8);
		}
	}
	free(databuff);
	databuff=NULL;
	return 1;
}
Esempio n. 7
0
void charts_load(void) {
	int fd;
	uint32_t i,j,k,fleng,fcharts;
	uint64_t *tab;
#ifdef USE_NET_ORDER
	uint32_t l;
	const uint8_t *ptr;
	uint8_t hdr[16];
	uint8_t data[8*LENG];
#else
	uint32_t hdr[3];
#endif
	char namehdr[101];

	fd = open(statsfilename,O_RDONLY);
	if (fd<0) {
		if (errno!=ENOENT) {
			mfs_errlog(LOG_WARNING,"error reading charts data file");
		} else {
			mfs_syslog(LOG_NOTICE,"no charts data file - initializing empty charts");
		}
		return;
	}
#ifdef USE_NET_ORDER
	if (read(fd,(void*)hdr,16)!=16) {
		mfs_errlog(LOG_WARNING,"error reading charts data file");
		close(fd);
		return;
	}
	ptr = hdr;
	i = get32bit(&ptr);
	if (i!=CHARTS_FILE_VERSION) {
		lseek(fd,4,SEEK_SET);
		memcpy((void*)&j,hdr,4);	// get first 4 bytes of hdr as a 32-bit number in "natural" order
		if (j==4) {
			if (charts_import_from_old_4ranges_format(fd)<0) {
				mfs_syslog(LOG_WARNING,"error importing charts data from 4-ranges format");
			}
		} else if (j==3) {
			if (charts_import_from_old_3ranges_format(fd)<0) {
				mfs_syslog(LOG_WARNING,"error importing charts data from 3-ranges format");
			}
		} else {
			mfs_syslog(LOG_WARNING,"unrecognized charts data file format - initializing empty charts");
		}
		close(fd);
		return;
	}
	fleng = get32bit(&ptr);
	fcharts = get32bit(&ptr);
	i = get32bit(&ptr);
	timepoint[SHORTRANGE]=i;
//	timepoint[MEDIUMRANGE]=i/6;
//	timepoint[LONGRANGE]=i/30;
//	timepoint[VERYLONGRANGE]=i/(24*60);
#else
	if (read(fd,(void*)hdr,sizeof(uint32_t))!=sizeof(uint32_t)) {
		mfs_errlog(LOG_WARNING,"error reading charts data file");
		close(fd);
		return;
	}
	if (hdr[0]!=CHARTS_FILE_VERSION) {
		if (hdr[0]==4) {
			if (charts_import_from_old_4ranges_format(fd)<0) {
				mfs_syslog(LOG_WARNING,"error importing charts data from 4-ranges format");
			}
		} else if (hdr[0]==3) {
			if (charts_import_from_old_3ranges_format(fd)<0) {
				mfs_syslog(LOG_WARNING,"error importing charts data from 3-ranges format");
			}
		} else {
			mfs_syslog(LOG_WARNING,"unrecognized charts data file format - initializing empty charts");
		}
		close(fd);
		return;
	}
	if (read(fd,(void*)hdr,sizeof(uint32_t)*3)!=sizeof(uint32_t)*3) {
		mfs_errlog(LOG_WARNING,"error reading charts data file");
		close(fd);
		return;
	}
	fleng = hdr[0];
	fcharts = hdr[1];
	timepoint[SHORTRANGE]=hdr[2];
//	timepoint[MEDIUMRANGE]=hdr[2]/6;
//	timepoint[LONGRANGE]=hdr[2]/30;
//	timepoint[VERYLONGRANGE]=hdr[2]/(24*60);
#endif
	pointers[SHORTRANGE]=LENG-1;
	pointers[MEDIUMRANGE]=LENG-1;
	pointers[LONGRANGE]=LENG-1;
	pointers[VERYLONGRANGE]=LENG-1;
	for (i=0 ; i<fcharts ; i++) {
		if (read(fd,namehdr,100)!=100) {
			mfs_errlog(LOG_WARNING,"error reading charts data file");
			close(fd);
			return;
		}
		namehdr[100]=0;
		for (j=0 ; j<statdefscount && strcmp(statdefs[j].name,namehdr)!=0 ; j++) {}
		if (j>=statdefscount) {
			lseek(fd,RANGES*fleng*8,SEEK_CUR);
			// ignore data
		} else {
			for (k=0 ; k<RANGES ; k++) {
				tab = series[j][k];
				if (fleng>LENG) {
					lseek(fd,(fleng-LENG)*sizeof(uint64_t),SEEK_CUR);
				}
#ifdef USE_NET_ORDER
				if (fleng<LENG) {
					if (read(fd,(void*)data,8*fleng)!=(ssize_t)(8*fleng)) {
						mfs_errlog(LOG_WARNING,"error reading charts data file");
						close(fd);
						return;
					}
					ptr = data;
					for (l=LENG-fleng ; l<LENG ; l++) {
						tab[l] = get64bit(&ptr);
					}
				} else {
					if (read(fd,(void*)data,8*LENG)!=(ssize_t)(8*LENG)) {
						mfs_errlog(LOG_WARNING,"error reading charts data file");
						close(fd);
						return;
					}
					ptr = data;
					for (l=0 ; l<LENG ; l++) {
						tab[l] = get64bit(&ptr);
					}
				}
#else
				if (fleng<LENG) {
					if (read(fd,(void*)(tab+(LENG-fleng)),sizeof(uint64_t)*fleng)!=(ssize_t)(sizeof(uint64_t)*fleng)) {
						mfs_errlog(LOG_WARNING,"error reading charts data file");
						close(fd);
						return;
					}
				} else {
					if (read(fd,(void*)tab,sizeof(uint64_t)*LENG)!=(ssize_t)(sizeof(uint64_t)*LENG)) {
						mfs_errlog(LOG_WARNING,"error reading charts data file");
						close(fd);
						return;
					}
				}
#endif
			}
		}
	}
	close(fd);
	mfs_syslog(LOG_NOTICE,"stats file has been loaded");
	return;
}