int main(int argc, char **argv) { SetDefaultLogging("TEST"); SetNamePgm("verif_syntax"); char *errtxt; char *fichier; config_file_t config; if ((argc > 1) && (argv[1])) { fichier = argv[1]; } else { LogTest("Usage %s <config_file>", argv[0]); exit(EINVAL); } /* test de la syntaxe du fichier */ config = config_ParseFile(fichier); if (config == NULL) { LogTest("Error parsing %s", argv[1]); exit(EINVAL); } else { LogTest("The syntax of the file %s is correct!", argv[1]); exit(0); } config_Free(config); return 0; }
/* Skips deleting first entry of export list. */ int rebuild_export_list() { #ifndef FIXME_EXPORT_RELOAD return 0; #else int status = 0; config_file_t config_struct; /* If no configuration file is given, then the caller must want to reparse the * configuration file from startup. */ if(config_path[0] == '\0') { LogCrit(COMPONENT_CONFIG, "Error: No configuration file was specified for reloading exports."); return 0; } /* Attempt to parse the new configuration file */ config_struct = config_ParseFile(config_path); if(!config_struct) { LogCrit(COMPONENT_CONFIG, "rebuild_export_list: Error while parsing new configuration file %s: %s", config_path, config_GetErrorMsg()); return 0; } /* Create the new exports list */ status = ReadExports(config_struct, &temp_exportlist); if(status < 0) { LogCrit(COMPONENT_CONFIG, "rebuild_export_list: Error while parsing export entries"); return status; } else if(status == 0) { LogWarn(COMPONENT_CONFIG, "rebuild_export_list: No export entries found in configuration file !!!"); return 0; } /* At least one worker thread should exist. Each worker thread has a pointer to * the same hash table. */ if(nfs_export_create_root_entry(&temp_exportlist) != TRUE) { LogCrit(COMPONENT_MAIN, "replace_exports: Error initializing Cache Inode root entries"); return 0; } return 1; #endif }
/* Skips deleting first entry of export list. */ int rebuild_export_list(void) { #if 0 int status = 0; config_file_t config_struct; /* If no configuration file is given, then the caller must want to reparse the * configuration file from startup. */ if(config_path == NULL) { LogCrit(COMPONENT_CONFIG, "Error: No configuration file was specified for reloading exports."); return 0; } /* Attempt to parse the new configuration file */ config_struct = config_ParseFile(config_path); if(!config_struct) { LogCrit(COMPONENT_CONFIG, "rebuild_export_list: Error while parsing new configuration file %s: %s", config_path, config_GetErrorMsg()); return 0; } /* Create the new exports list */ status = ReadExports(config_struct, &temp_exportlist); if(status < 0) { LogCrit(COMPONENT_CONFIG, "rebuild_export_list: Error while parsing export entries"); return status; } else if(status == 0) { LogWarn(COMPONENT_CONFIG, "rebuild_export_list: No export entries found in configuration file !!!"); return 0; } return 1; #else return 0; #endif }
int nfs_get_fsalpathlib_conf(char *configPath, path_str_t * PathLib, unsigned int *plen) { int var_max; int var_index; int err; char *key_name; char *key_value; config_item_t block; unsigned int found = FALSE; config_file_t config_struct; unsigned int index=0 ; /* Is the config tree initialized ? */ if(configPath == NULL || PathLib == NULL) LogFatal(COMPONENT_CONFIG, "nfs_get_fsalpathlib_conf configPath=%p PathLib=%p", configPath, PathLib); config_struct = config_ParseFile(configPath); if(!config_struct) LogFatal(COMPONENT_CONFIG, "Error while parsing %s: %s", configPath, config_GetErrorMsg()); /* Get the config BLOCK */ if((block = config_FindItemByName(config_struct, CONF_LABEL_NFS_CORE)) == NULL) { LogFatal(COMPONENT_CONFIG, "Cannot read item \"%s\" from configuration file", CONF_LABEL_NFS_CORE); } else if(config_ItemType(block) != CONFIG_ITEM_BLOCK) { /* Expected to be a block */ LogFatal(COMPONENT_CONFIG, "Item \"%s\" is expected to be a block", CONF_LABEL_NFS_CORE); } var_max = config_GetNbItems(block); for(var_index = 0; var_index < var_max; var_index++) { config_item_t item; item = config_GetItemByIndex(block, var_index); /* Get key's name */ if((err = config_GetKeyValue(item, &key_name, &key_value)) != 0) { LogFatal(COMPONENT_CONFIG, "Error reading key[%d] from section \"%s\" of configuration file.", var_index, CONF_LABEL_NFS_CORE); } if(!strcasecmp(key_name, "FSAL_Shared_Library")) { strncpy(PathLib[index], key_value, MAXPATHLEN); index += 1 ; found = TRUE; /* Do not exceed array size */ if( index == *plen ) break ; } } if(!found) { LogFatal(COMPONENT_CONFIG, "FSAL_Shared_Library not found"); return 1; } *plen = index ; return 0; } /* nfs_get_fsalpathlib_conf */
int main(int argc, char *argv[]) { char *tempo_exec_name = NULL; char localmachine[MAXHOSTNAMELEN + 1]; int c; int pidfile; #ifndef HAVE_DAEMON pid_t son_pid; #endif sigset_t signals_to_block; /* Set the server's boot time and epoch */ now(&ServerBootTime); ServerEpoch = (time_t) ServerBootTime.tv_sec; tempo_exec_name = strrchr(argv[0], '/'); if (tempo_exec_name != NULL) { exec_name = gsh_strdup(tempo_exec_name + 1); if (!exec_name) { fprintf(stderr, "Unable to allocate memory for exec name, exiting...\n"); exit(1); } } if (*exec_name == '\0') exec_name = argv[0]; /* get host name */ if (gethostname(localmachine, sizeof(localmachine)) != 0) { fprintf(stderr, "Could not get local host name, exiting...\n"); exit(1); } else { host_name = gsh_strdup(localmachine); if (!host_name) { fprintf(stderr, "Unable to allocate memory for hostname, exiting...\n"); exit(1); } } /* now parsing options with getopt */ while ((c = getopt(argc, argv, options)) != EOF) { switch (c) { case '@': /* A litlle backdoor to keep track of binary versions */ printf("%s compiled on %s at %s\n", exec_name, __DATE__, __TIME__); printf("Release = %s\n", VERSION); printf("Release comment = %s\n", VERSION_COMMENT); printf("Git HEAD = %s\n", _GIT_HEAD_COMMIT); printf("Git Describe = %s\n", _GIT_DESCRIBE); exit(0); break; case 'L': /* Default Log */ log_path = gsh_strdup(optarg); if (!log_path) { fprintf(stderr, "Unable to allocate memory for log path.\n"); exit(1); } break; case 'N': /* debug level */ debug_level = ReturnLevelAscii(optarg); if (debug_level == -1) { fprintf(stderr, "Invalid value for option 'N': NIV_NULL, NIV_MAJ, NIV_CRIT, NIV_EVENT, NIV_DEBUG, NIV_MID_DEBUG or NIV_FULL_DEBUG expected.\n"); exit(1); } break; case 'f': /* config file */ config_path = gsh_strdup(optarg); if (!config_path) { fprintf(stderr, "Unable to allocate memory for config path.\n"); exit(1); } break; case 'p': /* PID file */ pidfile_path = gsh_strdup(optarg); if (!pidfile_path) { fprintf(stderr, "Path %s too long for option 'f'.\n", optarg); exit(1); } break; case 'd': /* Detach or not detach ? */ detach_flag = true; break; case 'R': /* Shall we manage RPCSEC_GSS ? */ fprintf(stderr, "\n\nThe -R flag is deprecated, use this syntax in the configuration file instead:\n\n"); fprintf(stderr, "NFS_KRB5\n"); fprintf(stderr, "{\n"); fprintf(stderr, "\tPrincipalName = nfs@<your_host> ;\n"); fprintf(stderr, "\tKeytabPath = /etc/krb5.keytab ;\n"); fprintf(stderr, "\tActive_krb5 = true ;\n"); fprintf(stderr, "}\n\n\n"); exit(1); break; case 'T': /* Dump the default configuration on stdout */ my_nfs_start_info.dump_default_config = true; break; case 'E': ServerEpoch = (time_t) atoll(optarg); break; case '?': case 'h': default: /* display the help */ fprintf(stderr, usage, exec_name); exit(0); break; } } /* initialize memory and logging */ nfs_prereq_init(exec_name, host_name, debug_level, log_path); LogEvent(COMPONENT_MAIN, "%s Starting: Version %s, built at %s %s on %s", exec_name, GANESHA_VERSION, __DATE__, __TIME__, BUILD_HOST); /* Start in background, if wanted */ if (detach_flag) { #ifdef HAVE_DAEMON /* daemonize the process (fork, close xterm fds, * detach from parent process) */ if (daemon(0, 0)) LogFatal(COMPONENT_MAIN, "Error detaching process from parent: %s", strerror(errno)); #else /* Step 1: forking a service process */ switch (son_pid = fork()) { case -1: /* Fork failed */ LogFatal(COMPONENT_MAIN, "Could not start nfs daemon (fork error %d (%s)", errno, strerror(errno)); break; case 0: /* This code is within the son (that will actually work) * Let's make it the leader of its group of process */ if (setsid() == -1) { LogFatal(COMPONENT_MAIN, "Could not start nfs daemon (setsid error %d (%s)", errno, strerror(errno)); } break; default: /* This code is within the parent process, * it is useless, it must die */ LogFullDebug(COMPONENT_MAIN, "Starting a child of pid %d", son_pid); exit(0); break; } #endif } /* Make sure Linux file i/o will return with error * if file size is exceeded. */ #ifdef _LINUX signal(SIGXFSZ, SIG_IGN); #endif /* Echo PID into pidfile */ pidfile = open(pidfile_path, O_CREAT | O_RDWR, 0644); if (pidfile == -1) { LogFatal(COMPONENT_MAIN, "Can't open pid file %s for writing", pidfile_path); } else { char linebuf[1024]; struct flock lk; /* Try to obtain a lock on the file */ lk.l_type = F_WRLCK; lk.l_whence = SEEK_SET; lk.l_start = (off_t) 0; lk.l_len = (off_t) 0; if (fcntl(pidfile, F_SETLK, &lk) == -1) LogFatal(COMPONENT_MAIN, "Ganesha already started"); /* Put pid into file, then close it */ (void)snprintf(linebuf, sizeof(linebuf), "%u\n", getpid()); if (write(pidfile, linebuf, strlen(linebuf)) == -1) LogCrit(COMPONENT_MAIN, "Couldn't write pid to file %s", pidfile_path); } /* Set up for the signal handler. * Blocks the signals the signal handler will handle. */ sigemptyset(&signals_to_block); sigaddset(&signals_to_block, SIGTERM); sigaddset(&signals_to_block, SIGHUP); sigaddset(&signals_to_block, SIGPIPE); if (pthread_sigmask(SIG_BLOCK, &signals_to_block, NULL) != 0) LogFatal(COMPONENT_MAIN, "Could not start nfs daemon, pthread_sigmask failed"); /* Parse the configuration file so we all know what is going on. */ if (config_path == NULL) { LogFatal(COMPONENT_INIT, "start_fsals: No configuration file named."); return 1; } config_struct = config_ParseFile(config_path); if (!config_struct) { LogFatal(COMPONENT_INIT, "Error while parsing %s: %s", config_path, config_GetErrorMsg()); } /* We need all the fsal modules loaded so we can have * the list available at exports parsing time. */ start_fsals(config_struct); /* parse configuration file */ if (nfs_set_param_from_conf(config_struct, &my_nfs_start_info)) { LogFatal(COMPONENT_INIT, "Error setting parameters from configuration file."); } if (nfs_check_param_consistency()) { LogFatal(COMPONENT_INIT, "Inconsistent parameters found. Exiting..."); } if (init_fsals(config_struct)) { /* init the FSALs from the config */ LogFatal(COMPONENT_INIT, "FSALs could not initialize. Exiting..."); } /* freeing syntax tree : */ config_Free(config_struct); /* Everything seems to be OK! We can now start service threads */ nfs_start(&my_nfs_start_info); return 0; }
int auks_acl_init_from_config_file(auks_acl_t * p_acl, char *acl_file) { int fstatus = AUKS_ERROR; config_file_t config; int block_nb; char *principal; char *host; char *role_string; enum AUKS_ACL_ROLE role; int i; /* conf parsing */ config = config_ParseFile(acl_file); if (!config) { auks_error("unable to parse configuration file %s : %s", acl_file, extern_errormsg); fstatus = AUKS_ERROR_ACL_PARSING; goto exit; } auks_log("configuration file (%s) successfully parsed", acl_file); /* block nb */ block_nb = config_GetNbBlocks(config); if (block_nb <= 0) { auks_error("unable to get configuration blocks nb from " "config file %s : %s", acl_file, extern_errormsg); fstatus = AUKS_ERROR_ACL_FILE_IS_EMPTY; goto parse_exit; } auks_log2("configuration blocks nb (%d) successfully extracted", block_nb); /* acl init */ if (auks_acl_init(p_acl, block_nb) != 0) { auks_error("unable to init Auks ACL structure"); fstatus = AUKS_ERROR_ACL_INIT; goto parse_exit; } auks_log2("Auks ACL structure successfully initialized"); /* block loop */ fstatus = 0; for (i = 0; i < block_nb; i++) { char *block_name; block_name = config_GetBlockName(config, i); /* keep only rule block */ if (strncmp("rule", block_name, 7) != 0) continue; role_string = config_GetKeyValueByName(config, i, "role"); principal = config_GetKeyValueByName(config, i, "principal"); host = config_GetKeyValueByName(config, i, "host"); if (role_string == NULL) { auks_error("no role defined in rule[%d] of %s", i, acl_file); continue; } if (strncmp("user", role_string, 5) == 0) { role = AUKS_ACL_ROLE_USER; } else if (strncmp("admin", role_string, 6) == 0) { role = AUKS_ACL_ROLE_ADMIN; } else if (strncmp("guest", role_string, 5) == 0) { role = AUKS_ACL_ROLE_GUEST; } else { auks_error("invalid role for rule[%d]", i, acl_file, extern_errormsg); fstatus++; continue; } if (auks_acl_add_rule(p_acl, principal, host, role)) { auks_error("unable to add rule[%d] to auks_acl", i); fstatus++; } else { auks_log("rule[%d] '%s:%s => %s' successfully add", i, principal, host, role_string); } } /* EOF block loop */ /* clean acl if errors occured during creation */ if (fstatus) { auks_acl_free_contents(p_acl); fstatus = AUKS_ERROR_ACL_FILE_IS_INVALID; } parse_exit: /* free config file */ config_Free(config); exit: return fstatus; }
int auksd_engine_init_from_config_file(auksd_engine_t * engine, char *conf_file) { int fstatus = AUKS_ERROR; char* l_conf_file; char* e_conf_file; config_file_t config; int block_nb; char *phost; char *padd; char *pport; char *pprinc; char *pktb; char *shost; char *sadd; char *sport; char *sprinc; char *sktb; char *cdir; char *afile; char *lfile; char *dfile; char *ll_str; char *dl_str; char *tnb_str; char *qs_str; char *rs_str; char *rd_str; char *nat_str; char *krc_str; long int ll, dl, tnb, qs, rs, rd; int nat, krc; int i; int valid_block_nb=0; if ( conf_file != NULL ) l_conf_file = conf_file; else { e_conf_file = getenv("AUKSD_CONF"); if ( e_conf_file != NULL ) l_conf_file = e_conf_file ; else l_conf_file = DEFAULT_AUKSD_CONF ; } /* parse configuration file */ config = config_ParseFile(l_conf_file); if (!config) { auks_error("unable to parse configuration file %s : %s", l_conf_file, extern_errormsg); fstatus = AUKS_ERROR_ENGINE_CONFFILE_PARSING ; goto exit; } /* get conf blocks quantity */ block_nb = config_GetNbBlocks(config); if (block_nb <= 0) { auks_error("unable to get configuration blocks from config " "file %s : %s", l_conf_file, extern_errormsg); fstatus = AUKS_ERROR_ENGINE_CONFFILE_INVALID ; goto parse_exit; } /* look for relevants block and add contents to engine conf */ fstatus = AUKS_ERROR_ENGINE_CONFFILE_INCOMPLETE ; for (i = 0; i < block_nb; i++) { char *block_name; block_name = config_GetBlockName(config, i); /* skip non config blocks */ if (strncmp("common", block_name, 7) != 0) { continue; } auks_log ("initializing engine from 'common' block of file %s", l_conf_file); /* primary server conf value */ phost = config_GetKeyValueByName(config,i,"PrimaryHost"); if (phost == NULL) phost = DEFAULT_AUKSD_PRIMARY_HOST; padd = config_GetKeyValueByName(config,i,"PrimaryAddress"); if (padd == NULL) padd = DEFAULT_AUKSD_PRIMARY_ADDR; pport = config_GetKeyValueByName(config,i,"PrimaryPort"); if (pport == NULL) pport = DEFAULT_AUKSD_PRIMARY_PORT; pprinc = config_GetKeyValueByName(config,i,"PrimaryPrincipal"); if (pprinc == NULL) pprinc = DEFAULT_AUKSD_PRIMARY_PRINC; /* secondary server conf value */ shost = config_GetKeyValueByName(config,i,"SecondaryHost"); if (shost == NULL) shost = DEFAULT_AUKSD_SECONDARY_HOST; sadd = config_GetKeyValueByName(config,i,"SecondaryAddress"); if (sadd == NULL) sadd = DEFAULT_AUKSD_SECONDARY_ADDR; sport = config_GetKeyValueByName(config,i,"SecondaryPort"); if (sport == NULL) sport = DEFAULT_AUKSD_SECONDARY_PORT; sprinc = config_GetKeyValueByName(config,i, "SecondaryPrincipal"); if (sprinc == NULL) sprinc = DEFAULT_AUKSD_SECONDARY_PRINC; /* NAT traversal mode */ nat_str = config_GetKeyValueByName(config, i, "NAT") ; if (nat_str == NULL) nat = DEFAULT_AUKS_NAT_TRAVERSAL ; else if ( strncasecmp(nat_str,"yes",4) ==0 ) nat = 1 ; else nat = 0 ; valid_block_nb++; } /* EOF for */ for (i = 0; i < block_nb; i++) { char *block_name; block_name = config_GetBlockName(config, i); /* skip non config blocks */ if (strncmp("auksd", block_name, 5) != 0) { continue; } auks_log ("initializing engine from 'auksd' block of file %s", l_conf_file); /* primary server conf value */ pktb = config_GetKeyValueByName(config,i,"PrimaryKeytab"); if (pktb == NULL) pktb = DEFAULT_AUKSD_PRIMARY_KEYTAB; /* secondary server conf value */ sktb = config_GetKeyValueByName(config,i,"SecondaryKeytab"); if (sktb == NULL) sktb = DEFAULT_AUKSD_SECONDARY_KEYTAB; /* cache dir value */ cdir = config_GetKeyValueByName(config,i,"CacheDir"); if (cdir == NULL) cdir = DEFAULT_AUKSD_CACHEDIR; /* acl file value */ afile = config_GetKeyValueByName(config,i,"ACLFile"); if (afile == NULL) afile = DEFAULT_AUKSD_ACLFILE; /* read log file value */ lfile = config_GetKeyValueByName(config,i,"LogFile"); if (lfile == NULL) lfile = DEFAULT_AUKSD_LOGFILE; /* read log level value */ ll_str = config_GetKeyValueByName(config,i,"LogLevel"); if (ll_str == NULL) ll = DEFAULT_AUKSD_LOGLEVEL; else ll = strtol(ll_str, NULL, 10); if (ll == LONG_MIN || ll == LONG_MAX) ll = DEFAULT_AUKSD_LOGLEVEL; /* read debug file value */ dfile = config_GetKeyValueByName(config,i,"DebugFile"); if (dfile == NULL) dfile = DEFAULT_AUKSD_DEBUGFILE; /* read debug level value */ dl_str = config_GetKeyValueByName(config,i,"DebugLevel"); if (dl_str == NULL) dl = DEFAULT_AUKSD_DEBUGLEVEL; else dl = strtol(dl_str, NULL, 10); if (dl == LONG_MIN || dl == LONG_MAX) dl = DEFAULT_AUKSD_DEBUGLEVEL; /* read threads nb value */ tnb_str = config_GetKeyValueByName(config,i,"Workers"); if (tnb_str == NULL) tnb = DEFAULT_AUKSD_THREADS_NB; else tnb = strtol(tnb_str, NULL, 10); if (tnb == LONG_MIN || tnb == LONG_MAX) tnb = DEFAULT_AUKSD_THREADS_NB; /* read queue size */ qs_str = config_GetKeyValueByName(config,i,"QueueSize"); if (qs_str == NULL) qs = DEFAULT_AUKSD_QUEUE_SIZE; else qs = strtol(qs_str, NULL, 10); if (qs == LONG_MIN || qs == LONG_MAX) qs = DEFAULT_AUKSD_QUEUE_SIZE; /* read repository size */ rs_str = config_GetKeyValueByName(config,i,"RepoSize"); if (rs_str == NULL) rs = DEFAULT_AUKSD_REPO_SIZE; else rs = strtol(rs_str, NULL, 10); if (rs == LONG_MIN || rs == LONG_MAX) rs = DEFAULT_AUKSD_REPO_SIZE; /* clean delay */ rd_str = config_GetKeyValueByName(config,i,"CleanDelay"); if (rd_str == NULL) rd = DEFAULT_AUKSD_CLEAN_DELAY; else rd = strtol(rd_str, NULL, 10); if (rd == LONG_MIN || rd == LONG_MAX) rd = DEFAULT_AUKSD_CLEAN_DELAY; /* Kerberos Replay cache mode */ krc_str = config_GetKeyValueByName(config, i, "ReplayCache") ; if (krc_str == NULL) krc = DEFAULT_AUKS_REPLAY_CACHE ; else if ( strncasecmp(krc_str,"yes",4) ==0 ) krc = 1 ; else krc = 0 ; valid_block_nb++; } /* EOF for */ if ( valid_block_nb == 2 ) fstatus = AUKS_SUCCESS; if ( fstatus == AUKS_SUCCESS ) { /* init auksd engine */ fstatus = auksd_engine_init(engine, phost,padd, pport, pprinc, pktb, shost,sadd, sport, sprinc, sktb, cdir, afile, lfile, ll, dfile, dl, tnb, qs, rs, rd,nat,krc); /* init ok, break */ if (fstatus == 0) { auks_log("initialization succeed"); } else auks_error("initialization failed"); } parse_exit: /* free config file */ config_Free(config); exit: return fstatus; }
int main(int argc, char *argv[]) { char *tempo_exec_name = NULL; char localmachine[MAXHOSTNAMELEN + 1]; int c; int dsc; int rc; int pidfile; char *log_path = NULL; char *exec_name = "nfs-ganesha"; int debug_level = -1; int detach_flag = true; bool dump_trace = false; #ifndef HAVE_DAEMON int dev_null_fd = 0; pid_t son_pid; #endif sigset_t signals_to_block; struct config_error_type err_type; /* Set the server's boot time and epoch */ now(&nfs_ServerBootTime); nfs_ServerEpoch = (time_t) nfs_ServerBootTime.tv_sec; srand(nfs_ServerEpoch); tempo_exec_name = strrchr(argv[0], '/'); if (tempo_exec_name != NULL) exec_name = main_strdup("exec_name", tempo_exec_name + 1); if (*exec_name == '\0') exec_name = argv[0]; /* get host name */ if (gethostname(localmachine, sizeof(localmachine)) != 0) { fprintf(stderr, "Could not get local host name, exiting...\n"); exit(1); } else { nfs_host_name = main_strdup("host_name", localmachine); } /* now parsing options with getopt */ while ((c = getopt(argc, argv, options)) != EOF) { switch (c) { case 'v': case '@': printf("NFS-Ganesha Release = V%s\n", GANESHA_VERSION); #if !GANESHA_BUILD_RELEASE /* A little backdoor to keep track of binary versions */ printf("%s compiled on %s at %s\n", exec_name, __DATE__, __TIME__); printf("Release comment = %s\n", VERSION_COMMENT); printf("Git HEAD = %s\n", _GIT_HEAD_COMMIT); printf("Git Describe = %s\n", _GIT_DESCRIBE); #endif exit(0); break; case 'L': /* Default Log */ log_path = main_strdup("log_path", optarg); break; case 'N': /* debug level */ debug_level = ReturnLevelAscii(optarg); if (debug_level == -1) { fprintf(stderr, "Invalid value for option 'N': NIV_NULL, NIV_MAJ, NIV_CRIT, NIV_EVENT, NIV_DEBUG, NIV_MID_DEBUG or NIV_FULL_DEBUG expected.\n"); exit(1); } break; case 'f': /* config file */ nfs_config_path = main_strdup("config_path", optarg); break; case 'p': /* PID file */ nfs_pidfile_path = main_strdup("pidfile_path", optarg); break; case 'F': /* Don't detach, foreground mode */ detach_flag = false; break; case 'R': /* Shall we manage RPCSEC_GSS ? */ fprintf(stderr, "\n\nThe -R flag is deprecated, use this syntax in the configuration file instead:\n\n"); fprintf(stderr, "NFS_KRB5\n"); fprintf(stderr, "{\n"); fprintf(stderr, "\tPrincipalName = nfs@<your_host> ;\n"); fprintf(stderr, "\tKeytabPath = /etc/krb5.keytab ;\n"); fprintf(stderr, "\tActive_krb5 = true ;\n"); fprintf(stderr, "}\n\n\n"); exit(1); break; case 'T': /* Dump the default configuration on stdout */ my_nfs_start_info.dump_default_config = true; break; case 'C': dump_trace = true; break; case 'E': nfs_ServerEpoch = (time_t) atoll(optarg); break; case 'h': fprintf(stderr, usage, exec_name); exit(0); default: /* '?' */ fprintf(stderr, "Try '%s -h' for usage\n", exec_name); exit(1); } } /* initialize memory and logging */ nfs_prereq_init(exec_name, nfs_host_name, debug_level, log_path, dump_trace); #if GANESHA_BUILD_RELEASE LogEvent(COMPONENT_MAIN, "%s Starting: Ganesha Version %s", exec_name, GANESHA_VERSION); #else LogEvent(COMPONENT_MAIN, "%s Starting: %s", exec_name, "Ganesha Version " _GIT_DESCRIBE ", built at " __DATE__ " " __TIME__ " on " BUILD_HOST); #endif /* initialize nfs_init */ nfs_init_init(); nfs_check_malloc(); /* Start in background, if wanted */ if (detach_flag) { #ifdef HAVE_DAEMON /* daemonize the process (fork, close xterm fds, * detach from parent process) */ if (daemon(0, 0)) LogFatal(COMPONENT_MAIN, "Error detaching process from parent: %s", strerror(errno)); /* In the child process, change the log header * if not, the header will contain the parent's pid */ set_const_log_str(); #else /* Step 1: forking a service process */ switch (son_pid = fork()) { case -1: /* Fork failed */ LogFatal(COMPONENT_MAIN, "Could not start nfs daemon (fork error %d (%s)", errno, strerror(errno)); break; case 0: /* This code is within the son (that will actually work) * Let's make it the leader of its group of process */ if (setsid() == -1) { LogFatal(COMPONENT_MAIN, "Could not start nfs daemon (setsid error %d (%s)", errno, strerror(errno)); } /* stdin, stdout and stderr should not refer to a tty * I close 0, 1 & 2 and redirect them to /dev/null */ dev_null_fd = open("/dev/null", O_RDWR); if (dev_null_fd < 0) LogFatal(COMPONENT_MAIN, "Could not open /dev/null: %d (%s)", errno, strerror(errno)); if (close(STDIN_FILENO) == -1) LogEvent(COMPONENT_MAIN, "Error while closing stdin: %d (%s)", errno, strerror(errno)); else { LogEvent(COMPONENT_MAIN, "stdin closed"); dup(dev_null_fd); } if (close(STDOUT_FILENO) == -1) LogEvent(COMPONENT_MAIN, "Error while closing stdout: %d (%s)", errno, strerror(errno)); else { LogEvent(COMPONENT_MAIN, "stdout closed"); dup(dev_null_fd); } if (close(STDERR_FILENO) == -1) LogEvent(COMPONENT_MAIN, "Error while closing stderr: %d (%s)", errno, strerror(errno)); else { LogEvent(COMPONENT_MAIN, "stderr closed"); dup(dev_null_fd); } if (close(dev_null_fd) == -1) LogFatal(COMPONENT_MAIN, "Could not close tmp fd to /dev/null: %d (%s)", errno, strerror(errno)); /* In the child process, change the log header * if not, the header will contain the parent's pid */ set_const_log_str(); break; default: /* This code is within the parent process, * it is useless, it must die */ LogFullDebug(COMPONENT_MAIN, "Starting a child of pid %d", son_pid); exit(0); break; } #endif } /* Make sure Linux file i/o will return with error * if file size is exceeded. */ #ifdef _LINUX signal(SIGXFSZ, SIG_IGN); #endif /* Echo PID into pidfile */ pidfile = open(nfs_pidfile_path, O_CREAT | O_RDWR, 0644); if (pidfile == -1) { LogFatal(COMPONENT_MAIN, "Can't open pid file %s for writing", nfs_pidfile_path); } else { char linebuf[1024]; struct flock lk; /* Try to obtain a lock on the file */ lk.l_type = F_WRLCK; lk.l_whence = SEEK_SET; lk.l_start = (off_t) 0; lk.l_len = (off_t) 0; if (fcntl(pidfile, F_SETLK, &lk) == -1) LogFatal(COMPONENT_MAIN, "Ganesha already started"); /* Put pid into file, then close it */ (void)snprintf(linebuf, sizeof(linebuf), "%u\n", getpid()); if (write(pidfile, linebuf, strlen(linebuf)) == -1) LogCrit(COMPONENT_MAIN, "Couldn't write pid to file %s", nfs_pidfile_path); } /* Set up for the signal handler. * Blocks the signals the signal handler will handle. */ sigemptyset(&signals_to_block); sigaddset(&signals_to_block, SIGTERM); sigaddset(&signals_to_block, SIGHUP); sigaddset(&signals_to_block, SIGPIPE); if (pthread_sigmask(SIG_BLOCK, &signals_to_block, NULL) != 0) LogFatal(COMPONENT_MAIN, "Could not start nfs daemon, pthread_sigmask failed"); /* init URL package */ config_url_init(); /* Create a memstream for parser+processing error messages */ if (!init_error_type(&err_type)) goto fatal_die; /* Parse the configuration file so we all know what is going on. */ if (nfs_config_path == NULL || nfs_config_path[0] == '\0') { LogWarn(COMPONENT_INIT, "No configuration file named."); nfs_config_struct = NULL; } else nfs_config_struct = config_ParseFile(nfs_config_path, &err_type); if (!config_error_no_error(&err_type)) { char *errstr = err_type_str(&err_type); if (!config_error_is_harmless(&err_type)) { LogCrit(COMPONENT_INIT, "Error %s while parsing (%s)", errstr != NULL ? errstr : "unknown", nfs_config_path); if (errstr != NULL) gsh_free(errstr); goto fatal_die; } else LogWarn(COMPONENT_INIT, "Error %s while parsing (%s)", errstr != NULL ? errstr : "unknown", nfs_config_path); if (errstr != NULL) gsh_free(errstr); } if (read_log_config(nfs_config_struct, &err_type) < 0) { LogCrit(COMPONENT_INIT, "Error while parsing log configuration"); goto fatal_die; } /* We need all the fsal modules loaded so we can have * the list available at exports parsing time. */ start_fsals(); /* parse configuration file */ if (nfs_set_param_from_conf(nfs_config_struct, &my_nfs_start_info, &err_type)) { LogCrit(COMPONENT_INIT, "Error setting parameters from configuration file."); goto fatal_die; } /* initialize core subsystems and data structures */ if (init_server_pkgs() != 0) { LogCrit(COMPONENT_INIT, "Failed to initialize server packages"); goto fatal_die; } /* Load Data Server entries from parsed file * returns the number of DS entries. */ dsc = ReadDataServers(nfs_config_struct, &err_type); if (dsc < 0) { LogCrit(COMPONENT_INIT, "Error while parsing DS entries"); goto fatal_die; } /* Create stable storage directory, this needs to be done before * starting the recovery thread. */ rc = nfs4_recovery_init(); if (rc) { LogCrit(COMPONENT_INIT, "Recovery backend initialization failed!"); goto fatal_die; } /* Start grace period */ nfs_start_grace(NULL); /* Wait for enforcement to begin */ nfs_wait_for_grace_enforcement(); /* Load export entries from parsed file * returns the number of export entries. */ rc = ReadExports(nfs_config_struct, &err_type); if (rc < 0) { LogCrit(COMPONENT_INIT, "Error while parsing export entries"); goto fatal_die; } if (rc == 0 && dsc == 0) LogWarn(COMPONENT_INIT, "No export entries found in configuration file !!!"); report_config_errors(&err_type, NULL, config_errs_to_log); /* freeing syntax tree : */ config_Free(nfs_config_struct); /* Everything seems to be OK! We can now start service threads */ nfs_start(&my_nfs_start_info); return 0; fatal_die: report_config_errors(&err_type, NULL, config_errs_to_log); LogFatal(COMPONENT_INIT, "Fatal errors. Server exiting..."); /* NOT REACHED */ return 2; }
int nfs_ip_name_populate(char *path) { config_file_t config_file; config_item_t block; int var_max; int var_index; int err; char *key_name; char *key_value; char label[MAXNAMLEN]; sockaddr_t ipaddr; nfs_ip_name_t *nfs_ip_name; sockaddr_t *pipaddr; hash_buffer_t buffkey; hash_buffer_t buffdata; config_file = config_ParseFile(path); if(!config_file) { LogCrit(COMPONENT_CONFIG, "Can't open file %s", path); return IP_NAME_NOT_FOUND; } /* Get the config BLOCK */ if((block = config_FindItemByName(config_file, CONF_LABEL_IP_NAME_HOSTS)) == NULL) { LogCrit(COMPONENT_CONFIG, "Can't get label %s in file %s", CONF_LABEL_IP_NAME_HOSTS, path); return IP_NAME_NOT_FOUND; } else if(config_ItemType(block) != CONFIG_ITEM_BLOCK) { /* Expected to be a block */ return IP_NAME_NOT_FOUND; } var_max = config_GetNbItems(block); for(var_index = 0; var_index < var_max; var_index++) { config_item_t item; item = config_GetItemByIndex(block, var_index); /* Get key's name */ if((err = config_GetKeyValue(item, &key_name, &key_value)) != 0) { LogCrit(COMPONENT_CONFIG, "Error reading key[%d] from section \"%s\" of configuration file.", var_index, label); return IP_NAME_NOT_FOUND; } err = ipstring_to_sockaddr(key_value, &ipaddr); if(err != 0) { LogCrit(COMPONENT_CONFIG, "Error converting %s to an ipaddress %s", key_value, gai_strerror(err)); return IP_NAME_NOT_FOUND; } /* Entry to be cached */ nfs_ip_name = gsh_malloc(sizeof(nfs_ip_name_t)); if(nfs_ip_name == NULL) return IP_NAME_INSERT_MALLOC_ERROR; pipaddr = gsh_malloc(sizeof(sockaddr_t)); if(pipaddr == NULL) { gsh_free(nfs_ip_name); return IP_NAME_INSERT_MALLOC_ERROR; } strncpy(nfs_ip_name->hostname, key_name, MAXHOSTNAMELEN); nfs_ip_name->timestamp = time(NULL); memcpy(pipaddr, &ipaddr, sizeof(sockaddr_t)); buffdata.pdata = (caddr_t) nfs_ip_name; buffdata.len = sizeof(nfs_ip_name_t); buffkey.pdata = (caddr_t) pipaddr; buffkey.len = sizeof(sockaddr_t); if(HashTable_Set(ht_ip_name, &buffkey, &buffdata) != HASHTABLE_SUCCESS) { gsh_free(nfs_ip_name); gsh_free(pipaddr); return IP_NAME_INSERT_MALLOC_ERROR; } } if(isFullDebug(COMPONENT_CONFIG)) HashTable_Log(COMPONENT_CONFIG, ht_ip_name); return IP_NAME_SUCCESS; } /* nfs_ip_name_populate */