Changeset - 45521249c7e5
[Not reviewed]
default
0 2 0
Nathan Brink (binki) - 16 years ago 2010-02-17 01:12:30
ohnobinki@ohnopublishing.net
make config file includes relative by chdir()ing to sysconfdir
2 files changed with 32 insertions and 22 deletions:
0 comments (0 inline, 0 general)
Makefile.am
Show inline comments
 
@@ -7,34 +7,31 @@ AM_CPPFLAGS = -DSYSCONFDIR='"$(sysconfdi
 
AM_CFLAGS = $(DISTLIBS_CFLAGS)
 
LIBS = $(DISTLIBS_LDADD) $(DISTLIBS_LDFLAGS)
 
LDADD = libdistrencommon.la
 

	
 
bin_PROGRAMS = 
 
if ENABLE_SERVER
 
bin_PROGRAMS += distrend distrenslave
 
endif
 

	
 
pkglib_LTLIBRARIES = libdistrencommon.la
 

	
 
# libdistrencommon.la:
 
libdistrencommon_la_SOURCES = src/common/options.c \
 
	src/common/options.h \
 
libdistrencommon_la_SOURCES = src/common/asprintf.c src/common/asprintf.h \
 
	src/common/execio.c src/common/execio.h \
 
	src/common/misc.c src/common/misc.h \
 
	src/common/options.c src/common/options.h \
 
	src/common/protocol.h \
 
	src/common/execio.h \
 
	src/common/execio.c \
 
	src/common/remoteio.h \
 
	src/common/libremoteio.h \
 
	src/common/remoteio.c \
 
	src/common/asprintf.h \
 
	src/common/asprintf.c
 
	src/common/remoteio.c src/common/libremoteio.h
 
#see http://sources.redhat.com/autobook/autobook/autobook_91.html
 
# either increase the revision number or the interface number each release!
 
libdistrencommon_la_LDFLAGS = $(AM_LDFLAGS) -version-info 0:0:0
 

	
 
# shared server sources:
 
SERVER_SOURCES = src/server/slavefuncs.c \
 
	src/server/slavefuncs.h \
 
	src/server/distrenjob.c \
 
	src/server/distrenjob.h
 
# distrend:
 
distrend_CFLAGS = $(AM_CFLAGS) $(MYSQL_CFLAGS)
 
distrend_LDFLAGS = $(AM_LDFLAGS) $(MYSQL_LDFLAGS)
src/common/options.c
Show inline comments
 
@@ -8,31 +8,30 @@
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  DistRen is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU Affero General Public License for more details.
 

	
 
  You should have received a copy of the GNU Affero General Public License
 
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
/* The purpose of this file is to... */
 

	
 
#include "options.h"
 

	
 
/* privat-ish files for components we need to config */
 
#include "libremoteio.h"
 
#include "protocol.h" /*< for DISTREN_SERVERTYPE_* */
 
#include "common/asprintf.h"
 
#include "common/misc.h"
 
#include "common/libremoteio.h"
 
#include "common/protocol.h"
 

	
 
#include <confuse.h>
 
#include <string.h>
 
#include <stdio.h>
 
#include <stdlib.h>
 
#include <unistd.h>
 

	
 
struct options_common_data
 
{
 
  cfg_t *cfg;
 
};
 

	
 
@@ -42,24 +41,27 @@ struct options_common_data
 
   @todo replace abort()s with something useful
 
 */
 
int options_init(int argc, char *argv[], cfg_t **mycfg, cfg_opt_t *myopts, char *myname, struct options_common **allopts)
 
{
 
  char *configfileprefix;
 
  char *configfile;
 

	
 
  size_t i;
 

	
 
  char *optstring = "hc:";
 
  char curopt;
 

	
 
  char *workingdir;
 
  char *tmp;
 

	
 
  configfileprefix = NULL;
 
  while((curopt = getopt(argc, argv, optstring)) != (char)-1)
 
    switch(curopt)
 
      {
 
      case 'h':
 
	fprintf(stderr, "libdistren common options\n\
 
\n\
 
\t-h\tShow this help.\n\
 
\t-c <path>\tBasename for configuration files. For instance, if distrencommon.conf is located at /etc/distren/distrencommon.conf, set this option /etc/distren/distren . Default value: %s\n\
 
\n", SYSCONFDIR "/distren");
 
	return 2;
 
	break;
 
@@ -81,68 +83,76 @@ int options_init(int argc, char *argv[],
 
    {
 
      configfileprefix = strdup(configfileprefix);
 
      if(!configfileprefix)
 
	{
 
	  fprintf(stderr, "OOM\n");
 
	  return 1;
 
	}
 
    }
 
  /* For those of you who don't know, the following is an example of concatenation of constant strings by the C compiler. Now, that doesn't mean you can do run-time string concatenation ;-)
 
     strdup is because someday configfile will be customizable via argv[]
 
  */
 
  if(!configfileprefix)
 
      configfileprefix = strdup(SYSCONFDIR "/distren");
 
      configfileprefix = strdup(SYSCONFDIR);
 
  if(!configfileprefix)
 
    {
 
      fprintf(stderr, "OOM\n");
 
      return 1;
 
    }
 
  
 
  i = strlen(configfileprefix) + strlen(myname) + strlen(".conf") + 1;
 
  configfile = malloc(i);
 

	
 
  workingdir = distren_getcwd();
 
  if(!workingdir)
 
    fprintf(stderr, "Error finding working directory, I will not be able to return to it after reading the configuration files\n");
 
  if(chdir(configfileprefix))
 
    fprintf(stderr, "Unable to chdir(\"%s\") where I expected to find config files, expect failure\n", configfileprefix);
 

	
 
  _distren_asprintf(&configfile, "%s%s.conf", PACKAGE, myname);
 
  if(!configfile)
 
    {
 
      perror("malloc");
 
      perror("blah");
 
      free(workingdir);
 
      return 1;
 
    }
 
  strncpy(configfile, configfileprefix, strlen(configfileprefix) + 1);
 
  strcat(configfile, myname);
 
  strcat(configfile, ".conf");
 
  fprintf(stderr, "using configuration file: %s\n", configfile);
 

	
 
  tmp = distren_getcwd();
 
  fprintf(stderr, "using configuration file: ``%s'' (CWD=``%s'')\n", configfile, tmp ? tmp : "");
 
  free(tmp);
 

	
 
  /* initialize structs */
 
  *allopts = malloc(sizeof(struct options_common));
 
  if(!*allopts)
 
    {
 
      perror("malloc");
 
      free(workingdir);
 
      free(configfile);
 
      return 1;
 
    }
 
  memset(*allopts, '\0', sizeof(struct options_common));
 

	
 
  (*allopts)->data = malloc(sizeof(struct options_common_data));
 
  if(!(*allopts)->data)
 
    {
 
      perror("malloc");
 
      free(workingdir);
 
      free(configfile);
 
      free(*allopts);
 
      return 1;
 
    }
 
  memset((*allopts)->data, '\0', sizeof(struct options_common_data));
 

	
 
  (*allopts)->remoteio = malloc(sizeof(struct remoteio_opts));
 
  if(!(*allopts)->data)
 
    {
 
      perror("malloc");
 
      free(workingdir);
 
      free(configfile);
 
      free((*allopts)->data);
 
      free(*allopts);
 
      return 1;
 
    }
 
  memset((*allopts)->remoteio, '\0', sizeof(struct remoteio_opts));
 
  (*allopts)->remoteio->ssh_command = strdup("ssh");
 
  /** @TODO: check for NULL return above */
 

	
 

	
 
  cfg_opt_t common_opts[] =
 
    {
 
@@ -199,24 +209,27 @@ int options_init(int argc, char *argv[],
 
      free(configfile);
 

	
 
      return 1;
 

	
 
    case CFG_SUCCESS:
 
      break;
 
    }
 

	
 
  free(configfile);
 

	
 
  *mycfg = cfg_getsec((*allopts)->data->cfg, myname);
 

	
 
  if(chdir(workingdir))
 
    fprintf(stderr, "Unable to return to ``%s'', my old working directory. If you passed relative pathnames on the commandline, those pathnames will likely be disrespected\n", workingdir);
 
  free(workingdir);
 

	
 
  /*
 
   * libdistrencommon's config options:
 
   */
 

	
 
  /* remoteio -- iterate through servers */
 
  i = remoteio_config((*allopts)->data->cfg, (*allopts)->remoteio);
 
  if(i)
 
    {
 
      fprintf(stderr, "cleanup\n");
 
      abort();
 
      return 1;
0 comments (0 inline, 0 general)