Example #1
0
/** Free some global variables (except for the country variable). */
void
free_variables(void)
{
#ifdef DEBUG
    printf("free_variables\n");
#endif

    free_option_list(&options, FALSE);
    free_option_list(&settings, FALSE);
    free_option_list(&constants, FALSE);
    free_option_list(&constants_app, FALSE);
    free_option_list(&tokens, FALSE);

    free_gchar_ptr(save_file);

    g_rand_free(rand_generator);

    free_season_stats(FALSE);
}
/* [DEM monitoring] gets adapter request URL */
char* dem_get_adapter_request(context_t* context, char* name, char* args, const char* type, int nrpe)
{
	char		buffer[MAXBUFLEN];
	char*		result = NULL;
	option_list_t	opts   = NULL;

	/* Take adapter query fields from plugin arguments, distinguishing
	   between local executions (host_addr as identifier) and NRPE plugin
	   executions (-H plugin argument as identifier) */
	if (!nrpe) {
		char* addr = host_addr;
		snprintf(buffer, sizeof(buffer)-1, DEM_ADAPTER_REQUEST_FORMAT,
		         adapter_url, name, region_id, addr, type);
		buffer[sizeof(buffer)-1] = '\0';
		result = strdup(buffer);
	} else if ((opts = parse_args(args, ":H:nup:t:c:a:")) == NULL) {
		logging(LOG_WARN, context, "Cannot get NRPE plugin options");
		result = ADAPTER_REQUEST_INVALID;
	} else {
		char        addr[INET_ADDRSTRLEN];
		const char* host = NULL;
		size_t      i;

		for (i = 0; opts[i].opt != -1; i++) {
			switch(opts[i].opt) {
				case 'H': {
					host = opts[i].val;
					break;
				}
			}
		}
		if (host == NULL) {
			logging(LOG_WARN, context, "Missing NRPE plugin options");
			result = ADAPTER_REQUEST_INVALID;
		} else if (resolve_address(host, addr, INET_ADDRSTRLEN) == NEB_ERROR) {
			logging(LOG_WARN, context, "Cannot resolve remote address for %s", host);
			result = ADAPTER_REQUEST_INVALID;
		} else {
			snprintf(buffer, sizeof(buffer)-1, DEM_ADAPTER_REQUEST_FORMAT,
			         adapter_url, name, region_id, addr, type);
			buffer[sizeof(buffer)-1] = '\0';
			result = strdup(buffer);
		}
	}

	free_option_list(opts);
	opts = NULL;
	return result;
}
/* [NPM monitoring] gets adapter request URL */
char* npm_get_adapter_request(context_t* context, char* name, char* args, const char* type)
{
	char*		result = NULL;
	option_list_t	opts   = NULL;

	/* Take adapter query fields from plugin arguments */
	if ((opts = parse_args(args, ":H:C:o:m:")) == NULL) {
		logging(LOG_WARN, context, "Cannot get plugin options");
		result = ADAPTER_REQUEST_INVALID;
	} else {
		char*	host = NULL;
		int	port = -1;
		size_t	i;

		/* For interface 10.11.100.80/20, check_snmp plugin options should
		   look like "-H 10.11.100.80 -o .1.3.6.1.2.1.2.2.1.8.21", where
		   last number of -o option is port number + 1 */
		for (i = 0; opts[i].opt != -1; i++) {
			switch(opts[i].opt) {
				case 'H': {
					host = (char*) opts[i].val;
					break;
				}
				case 'o': {
					char* ptr = strrchr(opts[i].val, '.');
					port = (ptr) ? (atoi(++ptr) - 1) : -1;
					break;
				}
			}
		}
		if ((host == NULL) || (port == -1)) {
			logging(LOG_WARN, context, "Missing plugin options");
			result = ADAPTER_REQUEST_INVALID;
		} else {
			char buffer[MAXBUFLEN];
			snprintf(buffer, sizeof(buffer)-1, NPM_ADAPTER_REQUEST_FORMAT,
			         adapter_url, name, region_id, host, port, type);
			buffer[sizeof(buffer)-1] = '\0';
			result = strdup(buffer);
		}
	}

	free_option_list(opts);
	opts = NULL;
	return result;
}
Example #4
0
/** Free the memory the user occupies.
    @param user The user we free. */
void
free_user(User *user)
{
#ifdef DEBUG
    printf("free_user\n");
#endif

    gint i, j;

    free_gchar_ptr(user->name);
    free_g_string(&user->sponsor.name);
    free_gchar_ptr(user->mmatches_file);
    free_live_game(&user->live_game);
    free_option_list(&user->options, FALSE);

    for(i=0; i<user->events->len; i++)
        free_event(&g_array_index(user->events, Event, i));
    free_g_array(&user->events);

    for(i=0; i<user->history->len; i++)
    {
        free_gchar_ptr(g_array_index(user->history,
                                     UserHistory, i).team_name);
        for(j=0; j<3; j++)
            free_gchar_ptr(g_array_index(user->history,
                                         UserHistory, i).string[j]);
    }

    free_g_array(&user->history);

    free_mmatches(&user->mmatches, FALSE);

    free_player_array(&user->youth_academy.players);

    free_g_array(&user->bets[0]);
    free_g_array(&user->bets[1]);
    g_array_free(user->default_team, TRUE);
}
Example #5
0
static struct cfg_option *
do_read_config_file(const char *path, struct cfg_option *other) {
	FILE				*fd;
	char				buf[1024];
	char				*p;
	char				*p2;
	char				*option;
	int				lineno = 0;
	int				i;
	struct cfg_option		*newcfg;
	struct cfg_option		*tmpcfg;
	struct cfg_option		*ret = NULL;
	struct cfg_option		*ret_tail = NULL;
	static struct cfg_option	nullcfg;

	if ((fd = fopen(path, "r")) == NULL) {
		return NULL;
	}

	while (fgets(buf, sizeof buf, fd) != NULL) {
		if ((p = strchr(buf, '\n')) != NULL) {
			*p = 0;
		} else {
			int	ch;

			(void) fprintf(stderr,
				"%s:%d: Line too long - truncating\n",
				path, lineno);
			do {
				ch = fgetc(fd);
			} while (ch != '\n' && ch != EOF);	
		}
		++lineno;
		p = buf;
		while (isspace((unsigned char)*p)) {
			++p;
		}	
		if (*p == '#' || *p == 0) {
			/* Comment or empty line */
			continue;
		}
		option = p;
		while (isalpha((unsigned char)*p)) {
			++p;
		}
		if (!*p) {
			(void) fprintf(stderr, "%s:%d: Malformed "
				"line `%s'\n",
				path, lineno, buf);
			continue;
		}
		if (strncmp(option, "undef", 5) == 0
			&& !isalpha((unsigned char)option[5])) {
			/* Undefining existing option */
			*p++ = 0;
			if ((p2 = next_opt_str(&p)) != NULL) {
				if ((newcfg = lookup_option(other, p2)) ||
					(newcfg = lookup_option(ret, p2))) {
					free(newcfg->name);
					newcfg->name = NULL;
				}
			}
		} else {
			char	*startp = p;
			int	exists = 0;

			/* Defining new option */
			newcfg = n_xmalloc(sizeof *newcfg);
			*newcfg = nullcfg;

			while (isspace((unsigned char)*p)) {
				++p;
			}
			if (*p++ != '=') {
				/* Incomplete or malformed line - ignore */
				(void) fprintf(stderr, "%s:%d: Malformed "
					"line `%s'\n",
					path, lineno, buf);
				continue;
			}
			*startp = 0;
			if (other == NULL ||
				((tmpcfg=lookup_option(other,option)) == NULL
				&& (tmpcfg = lookup_option(ret,option))
				==NULL)) {
				/*
				 * Doesn't already exist
				 */
				newcfg->name = n_xstrdup(option);
			} else {
				/*
				 * We're overriding an existing definition
				 * from a previous configuration file -
				 * Are we dealing with an option that can
				 * be merged, or one that only allows a
				 * single definition?
				 */
				newcfg->name = tmpcfg->name;
				tmpcfg->name = NULL;
				if (tmpcfg->info->mergable) {
					exists = 1;
					newcfg->argv = tmpcfg->argv;
					newcfg->argc = tmpcfg->argc;
					tmpcfg->argv = NULL;
				}	
			}

			while ((p2 = next_opt_str(&p)) != NULL) {
				int	save_opt = 1;

				if (exists) {
					for (i = 0; i < newcfg->argc; ++i) {
						if (strcmp(newcfg->argv[i],
							p2) == 0) {
							/* Already in list */
							save_opt = 0;
							break;
						}
					}
				}
				if (save_opt) {
					++newcfg->argc;
					newcfg->argv = n_xrealloc(newcfg->argv,
						newcfg->argc *
						sizeof *newcfg->argv);
					newcfg->argv[newcfg->argc - 1] =
						n_xstrdup(p2);
				}
			}
			
			/*
			 * Now establish the option's identity and save it
			 * in the list
			 */
			for (i = 0; known_options[i].name != NULL; ++i) {
				if (strcmp(newcfg->name,
					known_options[i].name) == 0) {
					break;
				}
			}
			if (known_options[i].name == NULL) {
				/* Unknown option */
				(void) fprintf(stderr, "%s:%d: Unknown "
					"option `%s'\n",
					path, lineno, newcfg->name);
				free_option(newcfg);
				continue;
			}
			newcfg->info = &known_options[i];
			if (newcfg->info->maxargs != 0
				&& newcfg->argc > newcfg->info->maxargs) {
				(void) fprintf(stderr, "%s:%d: Too many "
					"arguments for `%s'\n",
					path, lineno, newcfg->name);
				free_option(newcfg);
				continue;
			} else if (newcfg->info->minargs != 0
				&& newcfg->argc < newcfg->info->minargs) {
				(void) fprintf(stderr, "%s:%d: Not "
					"enough arguments for `%s'\n",
					path, lineno, newcfg->name);
				free_option(newcfg);
				continue;
			}

			if (ret == NULL) {
				ret = ret_tail = newcfg;
			} else {
				ret_tail->next = newcfg;
				ret_tail = newcfg;
			}
		}
	}
	(void) fclose(fd);
	if (ret != NULL && other) {
		free_option_list(other);
	}

	return ret;
}