00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00034 #ifndef LCAS_SETUP_C
00035 #define LCAS_SETUP_C
00036
00037
00038
00039
00040
00041 #include "lcas_config.h"
00042 #include <syslog.h>
00043 #include <errno.h>
00044 #include <ctype.h>
00045 #include <stdio.h>
00046 #include <stdlib.h>
00047 #include <string.h>
00048 #include <stdarg.h>
00049
00050 #include <sys/types.h>
00051 #include <sys/stat.h>
00052 #include <fcntl.h>
00053 #include "lcas_setup.h"
00054 #include "_lcas_utils.h"
00055 #include "_lcas_log.h"
00056
00057
00058
00059
00060
00061
00062 #define LCAS_MAXARGS 52
00063 #define LCAS_BUFSIZ 8192
00064
00065
00066 #ifndef LCAS_LOCATION
00067 #define LCAS_LOCATION "/opt/glite/"
00068 #endif
00069
00070
00071 #ifndef LCAS_ETC_DIR
00072 #define LCAS_ETC_DIR "etc/lcas"
00073 #endif
00074
00075
00076 #ifndef LCAS_MOD_DIR
00077 #define LCAS_MOD_DIR "lib"
00078 #endif
00079
00080
00081 #ifndef LCAS_LOG_FILE
00082 #define LCAS_LOG_FILE "-"
00083 #endif
00084
00085
00086 #ifndef LCAS_DB_FILE
00087 #define LCAS_DB_FILE "lcas.db"
00088 #endif
00089
00090
00091
00092
00093
00094
00095 static void print_settings(void);
00096
00097
00098
00099
00100
00101 static char * lcas_log_file = NULL;
00102 int lcas_use = 1;
00103 static char * lcas_debug_level = NULL;
00104 static char * lcas_db_file = NULL;
00105 static char * lcas_etc_dir = NULL;
00106 static char * lcas_mod_dir = NULL;
00107 static char * tmpname=NULL;
00108 static int do_log = 1;
00109 static int do_syslog = 1;
00110 static unsigned short log_flag = 0;
00111 static char * lcas_log_string = NULL;
00112
00113
00114
00115
00116
00117
00118
00119 void print_settings(void)
00120 {
00121 fprintf(stderr, "LCAS settings:\n");
00122 fprintf(stderr, " use logfile = %s\n", do_log?"yes":"no");
00123 fprintf(stderr, " logfile = %s\n", lcas_log_file);
00124 fprintf(stderr, " use LCAS: %s\n", lcas_use?"yes":"no");
00125 fprintf(stderr, " lcas_debug_level = %s\n", lcas_debug_level?lcas_debug_level:"0 (default)");
00126 fprintf(stderr, " lcas_db_file = %s%s\n", lcas_db_file?lcas_db_file:LCAS_DB_FILE, lcas_db_file?"":" (default)");
00127 fprintf(stderr, " lcas_etc_dir = %s\n", lcas_etc_dir?lcas_etc_dir:"(undefined)");
00128 fprintf(stderr, " lcas_mod_dir = %s%s\n",
00129 lcas_mod_dir?lcas_mod_dir:LCAS_MOD_DIR, lcas_mod_dir?"":" (default)");
00130
00131 }
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 int
00155 lcas_setup(const char * config_file)
00156 {
00157 char ** arguments = NULL;
00158 char argbuf[LCAS_BUFSIZ];
00159 int nread = 0;
00160 int nargs = LCAS_MAXARGS;
00161 int config_fd = -1;
00162 int i = 0;
00163 int retval;
00164 char * glite_location = getenv("GLITE_LOCATION");
00165 char * lcas_location = (glite_location ? glite_location : LCAS_LOCATION);
00166
00167
00168
00169
00170
00171 if (config_file)
00172 {
00173
00174 arguments = (char**) malloc(nargs * sizeof(char *));
00175 if ( (config_fd = open(config_file, O_RDONLY)) < 0 )
00176 {
00177 fprintf(stderr, "Error getting file descriptor from stream of %s: %s\n",
00178 config_file, strerror(errno));
00179 return LCAS_SETUP_ERROR_OPEN;
00180 }
00181 if ( (nread = read(config_fd, argbuf, LCAS_BUFSIZ)) < 0)
00182 {
00183 fprintf(stderr, "Error during read of lcas config file %s: %s\n",
00184 config_file, strerror(errno));
00185 return LCAS_SETUP_ERROR_READ;
00186 }
00187 else if (nread >= LCAS_BUFSIZ)
00188 {
00189 fprintf(stderr, "Size of lcas config file should be less than %d bytes !\n", LCAS_BUFSIZ);
00190 return LCAS_SETUP_ERROR_READ_SIZE;
00191 }
00192 argbuf[nread] = '\0';
00193 if (close(config_fd))
00194 {
00195 fprintf(stderr, "Unable to close file descriptor %d: %s\n", config_fd, strerror(errno));
00196 return LCAS_SETUP_ERROR_CLOSE;
00197 }
00198
00199
00200 if ( (retval = lcas_tokenize(argbuf, arguments, &nargs, " \t\n")) != 0)
00201 {
00202 fprintf(stderr, "Error tokenizing lcas config file (retval = %d) from \"%s\".\n",
00203 retval, arguments[i + 1]);
00204 switch (retval)
00205 {
00206 case -1: fprintf(stderr, " malloc error\n"); break;
00207 case -2: fprintf(stderr, " too many arguments, max = %d\n", LCAS_MAXARGS); break;
00208 case -3: fprintf(stderr, " cannot match quotes\n"); break;
00209 }
00210 return LCAS_SETUP_ERROR_PARSE;
00211 }
00212 for (i = 0; i < nargs; i++)
00213 {
00214 if ( (strcmp(arguments[i], "-lcas_db_file") == 0)
00215 && (i + 1 < nargs) )
00216 {
00217 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00218 {
00219 lcas_db_file = arguments[i + 1];
00220 }
00221 else
00222 {
00223 fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00224 return 1;
00225 return LCAS_SETUP_ERROR_ARGUMENT;
00226 }
00227 i++;
00228 }
00229 else if ( ( ((strcmp(arguments[i], "-lcas_etc_dir") == 0) || (strcmp(arguments[i], "-lcas_dir") == 0)) )
00230 && (i + 1 < nargs) )
00231 {
00232 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00233 {
00234 lcas_etc_dir = arguments[i + 1];
00235 }
00236 else
00237 {
00238 fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00239 return LCAS_SETUP_ERROR_ARGUMENT;
00240 }
00241 i++;
00242 }
00243 else if ( ( ((strcmp(arguments[i], "-lcas_mod_dir") == 0) || (strcmp(arguments[i], "-lcasmod_dir") == 0)) )
00244 && (i + 1 < nargs) )
00245 {
00246 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00247 {
00248 lcas_mod_dir = arguments[i + 1];
00249 }
00250 else
00251 {
00252 fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00253 return LCAS_SETUP_ERROR_ARGUMENT;
00254 }
00255 i++;
00256 }
00257 else if ( (strcmp(arguments[i], "-lcas_debug_level") == 0)
00258 && (i + 1 < nargs) )
00259 {
00260 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00261 {
00262 lcas_debug_level = arguments[i + 1];
00263 }
00264 else
00265 {
00266 fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00267 return LCAS_SETUP_ERROR_ARGUMENT;
00268 }
00269 i++;
00270 }
00271 else if ( (strcmp(arguments[i], "-lcas_log_file") == 0)
00272 && (i + 1 < nargs) )
00273 {
00274 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00275 {
00276 lcas_log_file = arguments[i + 1];
00277 }
00278 else
00279 {
00280 fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00281 return LCAS_SETUP_ERROR_ARGUMENT;
00282 }
00283 i++;
00284 }
00285 else if ( (strcmp(arguments[i], "-lcas_log_string") == 0)
00286 && (i + 1 < nargs) )
00287 {
00288 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00289 {
00290 lcas_log_string = arguments[i + 1];
00291 }
00292 else
00293 {
00294 fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00295 return LCAS_SETUP_ERROR_ARGUMENT;
00296 }
00297 i++;
00298 }
00299 else if ( (strcmp(arguments[i], "-lcas_do_log") == 0)
00300 && (i + 1 < nargs) )
00301 {
00302 if ((arguments[i + 1] != NULL) && (strlen(arguments[i + 1]) > 0))
00303 {
00304 if ( (strcmp(arguments[i+1],"yes") == 0) || (strcmp(arguments[i+1],"YES") == 0) ||
00305 (strcmp(arguments[i+1],"y") == 0) || (strcmp(arguments[i+1],"Y") ==0) )
00306 {
00307 do_log = 1;
00308 }
00309 else if ( (strcmp(arguments[i+1],"no") == 0) || (strcmp(arguments[i+1],"NO") == 0) ||
00310 (strcmp(arguments[i+1],"n") == 0) || (strcmp(arguments[i+1],"N") ==0) )
00311 {
00312 do_log = 0;
00313 }
00314 else
00315 {
00316 fprintf(stderr, "use \"yes\" or \"no\" for option %s\n", arguments[i]);
00317 return LCAS_SETUP_ERROR_ARGUMENT;
00318 }
00319
00320 }
00321 else
00322 {
00323 fprintf(stderr, "Error, no argument found for %s (failure)\n", arguments[i]);
00324 return LCAS_SETUP_ERROR_ARGUMENT;
00325 }
00326 i++;
00327 }
00328
00329
00330
00331
00332 else if ((strncmp(arguments[i], "-", 1) == 0) && (strncmp(arguments[i], "-lcmaps", 7) != 0))
00333 {
00334 fprintf(stderr, "Error: \"%s\" no such option\n", arguments[i]);
00335 return LCAS_SETUP_ERROR_ARGUMENT;
00336 }
00337 }
00338 }
00339 else
00340 {
00341
00342
00343 }
00344
00345 if (lcas_debug_level)
00346 {
00347 setenv("LCAS_DEBUG_LEVEL", lcas_debug_level, 1);
00348 }
00349 else if (getenv("LCAS_DEBUG_LEVEL") == NULL)
00350 {
00351 setenv("LCAS_DEBUG_LEVEL", 0, 1);
00352 }
00353
00354 if (lcas_etc_dir)
00355 {
00356 tmpname=lcas_genfilename(lcas_location, lcas_etc_dir, NULL);
00357 setenv("LCAS_DIR", tmpname, 1);
00358 setenv("LCAS_ETC_DIR", tmpname, 1);
00359 if (tmpname) { free(tmpname); tmpname=NULL; }
00360 }
00361 else if ((getenv("LCAS_DIR") == NULL) && (getenv("LCAS_ETC_DIR") == NULL))
00362 {
00363 tmpname=lcas_genfilename(lcas_location, LCAS_ETC_DIR, NULL);
00364 setenv("LCAS_DIR", tmpname, 1);
00365 setenv("LCAS_ETC_DIR", tmpname, 1);
00366 if (tmpname) { free(tmpname); tmpname=NULL; }
00367 }
00368
00369 if (lcas_db_file)
00370 {
00371 tmpname=lcas_genfilename(lcas_etc_dir,lcas_db_file,NULL);
00372 setenv("LCAS_DB_FILE", tmpname, 1);
00373 if (tmpname) { free(tmpname); tmpname=NULL; }
00374 }
00375 else if (getenv("LCAS_DB_FILE") == NULL)
00376 {
00377 tmpname=lcas_genfilename(lcas_etc_dir,LCAS_DB_FILE,NULL);
00378 setenv("LCAS_DB_FILE", tmpname, 1);
00379 if (tmpname) { free(tmpname); tmpname=NULL; }
00380 }
00381
00382 if (lcas_mod_dir)
00383 {
00384 tmpname=lcas_genfilename(lcas_location, lcas_mod_dir, NULL);
00385 setenv("LCAS_MOD_DIR", tmpname, 1);
00386 if (tmpname) { free(tmpname); tmpname=NULL; }
00387 }
00388 else if (getenv("LCAS_MOD_DIR") == NULL)
00389 {
00390 tmpname=lcas_genfilename(lcas_location, LCAS_MOD_DIR, NULL);
00391 setenv("LCAS_MOD_DIR", tmpname, 1);
00392 if (tmpname) { free(tmpname); tmpname=NULL; }
00393 }
00394
00395 if (do_log)
00396 {
00397 if (lcas_log_file)
00398 {
00399 tmpname=lcas_genfilename(lcas_location, lcas_log_file, NULL);
00400 setenv("LCAS_LOG_FILE", tmpname, 1);
00401 if (tmpname) { free(tmpname); tmpname=NULL; }
00402 }
00403 else if (getenv("LCAS_LOG_FILE") == NULL)
00404 {
00405 tmpname=lcas_genfilename(NULL, LCAS_LOG_FILE, NULL);
00406 setenv("LCAS_LOG_FILE", tmpname, 1);
00407 if (tmpname) { free(tmpname); tmpname=NULL; }
00408 }
00409 }
00410 log_flag = (unsigned short) 0;
00411 if (do_log)
00412 log_flag |= DO_USRLOG;
00413 if (do_syslog)
00414 log_flag |= DO_SYSLOG;
00415 if (getenv("LCAS_LOG_TYPE") == NULL)
00416 {
00417 char tmpstr[6];
00418 snprintf(tmpstr, 6, "%u", log_flag);
00419 setenv("LCAS_LOG_FILE", tmpstr, 1);
00420 }
00421
00422
00423
00424
00425
00426
00427 if (lcas_log_string)
00428 {
00429 setenv("LCAS_LOG_STRING", lcas_log_string, 0);
00430 }
00431
00432 print_settings();
00433
00434 return 0;
00435 }
00436
00437 #endif
00438
00439
00440
00441
00442
00443
00444
00445