libarmutils  1.4
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups
dsenv.c
Go to the documentation of this file.
1 /*******************************************************************************
2 *
3 * COPYRIGHT (C) 2010 Battelle Memorial Institute. All Rights Reserved.
4 *
5 ********************************************************************************
6 *
7 * Author:
8 * name: Brian Ermold
9 * phone: (509) 375-2277
10 * email: brian.ermold@pnl.gov
11 *
12 ********************************************************************************
13 *
14 * REPOSITORY INFORMATION:
15 * $Revision: 55592 $
16 * $Author: ermold $
17 * $Date: 2014-07-19 23:21:50 +0000 (Sat, 19 Jul 2014) $
18 *
19 ********************************************************************************
20 *
21 * NOTE: DOXYGEN is used to generate documentation for this file.
22 *
23 *******************************************************************************/
24 
25 /** @file dsenv.c
26  * DataSystem Environment Functions.
27  */
28 
29 #include "armutils.h"
30 
31 /*******************************************************************************
32  * Private Functions
33  */
34 /** @privatesection */
35 
36 /**
37  * Create the full path to a datastream directory.
38  *
39  * This function will return a dynamically allocated string of the form:
40  *
41  * $(root_dir)/$(site)/$(site)$(name)$(facility).$(level)
42  *
43  * The calling process is responsible for freeing this value when it is
44  * no longer needed.
45  *
46  * Error messages from this function are sent to the message handler
47  * (see msngr_init_log() and msngr_init_mail()).
48  *
49  * @param root_dir - the root directory
50  * @param site - site name
51  * @param facility - facility name
52  * @param name - datastream class name or process name
53  * @param level - datastream class level, or NULL to exclude the level
54  * from the datastream directory name.
55  *
56  * @return
57  * - full path to the datastream directory
58  * - 0 if a memory allocation error occurred
59  */
60 static char *dsenv_create_full_path(
61  const char *root_dir,
62  const char *site,
63  const char *facility,
64  const char *name,
65  const char *level)
66 {
67  char *path;
68 
69  if (level) {
70 
71  path = msngr_create_string("%s/%s/%s%s%s.%s",
72  root_dir, site, site, name, facility, level);
73  }
74  else {
75 
76  path = msngr_create_string("%s/%s/%s%s%s",
77  root_dir, site, site, name, facility);
78  }
79 
80  if (!path) {
81 
82  if (level) {
83 
85  "Could not get datastream path for: %s, %s%s%s.%s\n"
86  " -> memory allocation error\n",
87  root_dir, site, name, facility, level);
88  }
89  else {
90 
92  "Could not get datastream path for: %s, %s%s%s\n"
93  " -> memory allocation error\n",
94  root_dir, site, name, facility);
95  }
96 
97  return((char *)NULL);
98  }
99 
100  return(path);
101 }
102 
103 /*******************************************************************************
104  * Public Functions
105  */
106 /** @publicsection */
107 
108 /**
109  * Get hostname.
110  *
111  * The returned value is statically defined in this function and must
112  * *not* be freed by the calling process.
113  *
114  * Error messages from this function are sent to the message handler
115  * (see msngr_init_log() and msngr_init_mail()).
116  *
117  * @return
118  * - pointer to the hostname
119  * - NULL if an error occurred
120  */
122 {
123  static char hostname[256];
124 
125  if (!hostname[0]) {
126  if (gethostname(hostname, 255) == -1) {
127 
129  "Could not get hostname: %s\n", strerror(errno));
130 
131  return((char *)NULL);
132  }
133  }
134 
135  return(hostname);
136 }
137 
138 /**
139  * Get environment variable.
140  *
141  * The output value is dynamically allocated and must be freed by the
142  * calling process.
143  *
144  * Error messages from this function are sent to the message handler
145  * (see msngr_init_log() and msngr_init_mail()).
146  *
147  * @param name - name of the environment variable
148  * @param value - output: pointer to the value of the environment variable
149  *
150  * @return
151  * - 1 if the environment variable was found
152  * - 0 if the environment variable was not found
153  * - -1 if a memory allocation error occurred
154  */
155 int dsenv_getenv(const char *name, char **value)
156 {
157  const char *env_value = getenv(name);
158 
159  if (!env_value) {
160  *value = (char *)NULL;
161  return(0);
162  }
163 
164  *value = strdup(env_value);
165  if (!(*value)) {
166 
168  "Could not get environment variable: %s\n"
169  " -> memory allocation error\n", name);
170 
171  return(-1);
172  }
173 
174  return(1);
175 }
176 
177 /**
178  * Set environment variable.
179  *
180  * Error messages from this function are sent to the message handler
181  * (see msngr_init_log() and msngr_init_mail()).
182  *
183  * @param name - name of the environment variable
184  * @param format - format string (see printf)
185  * @param ... - arguments for the format string
186  *
187  * @return
188  * - 1 if successful
189  * - 0 if an error occurred
190  */
191 int dsenv_setenv(const char *name, const char *format, ...)
192 {
193  va_list args;
194  char *value;
195  int status;
196 
197  va_start(args, format);
198 
199  value = msngr_format_va_list(format, args);
200 
201  if (!value) {
202 
204  "Could not set environment variable: %s\n"
205  " -> memory allocation error\n", name);
206 
207  return(0);
208  }
209 
210  va_end(args);
211 
212  status = setenv(name, value, 1);
213  free(value);
214 
215  if (status < 0) {
216 
218  "Could not set environment variable: %s\n"
219  " -> %s\n", name, strerror(errno));
220 
221  return(0);
222  }
223 
224  return(1);
225 }
226 
227 /**
228  * Get the root path of the apps conf directory.
229  *
230  * This function will return a dynamically allocated string of the form:
231  *
232  * $(PROC_TYPE_HOME)/conf/$(proc_type)/$(proc_name)
233  *
234  * The calling process is responsible for freeing this string when it is
235  * no longer needed.
236  *
237  * Error messages from this function are sent to the message handler
238  * (see msngr_init_log() and msngr_init_mail()).
239  *
240  * @param proc_name - the name of the process
241  * @param proc_type - the process type (i.e. Ingest, VAP)
242  * @param path - output: pointer to the root path of the
243  * apps conf directory
244  *
245  * @return
246  * - 1 if successful
247  * - 0 if the PROC_TYPE_HOME environment variable was not found
248  * - -1 if a memory allocation error occurred
249  */
251  const char *proc_name,
252  const char *proc_type,
253  char **path)
254 {
255  char *env_var = (char *)NULL;
256  char *lc_type = (char *)NULL;
257  char *root_dir;
258  int typelen;
259  int i;
260 
261  typelen = strlen(proc_type);
262 
263  if (!(env_var = malloc((typelen + 6) * sizeof(char))) ||
264  !(lc_type = malloc((typelen + 1) * sizeof(char)))) {
265 
267  "Could not get apps conf path for: %s, %s\n"
268  " -> memory allocation error\n",
269  proc_name, proc_type);
270 
271  if (env_var) free(env_var);
272 
273  return(-1);
274  }
275 
276  for (i = 0; i < typelen; ++i) {
277  env_var[i] = toupper(proc_type[i]);
278  lc_type[i] = tolower(proc_type[i]);
279  }
280 
281  lc_type[i] = '\0';
282  strcpy(&(env_var[i]), "_HOME");
283 
284  root_dir = getenv(env_var);
285  free(env_var);
286 
287  if (!root_dir) {
288  *path = (char *)NULL;
289  free(lc_type);
290  return(0);
291  }
292 
293  *path = msngr_create_string("%s/conf/%s/%s",
294  root_dir, lc_type, proc_name);
295 
296  free(lc_type);
297 
298  if (!*path) {
299 
301  "Could not get apps conf path for: %s, %s\n"
302  " -> memory allocation error\n",
303  proc_name, proc_type);
304 
305  return(-1);
306  }
307 
308  return(1);
309 }
310 
311 /**
312  * Get the apps conf directory for a datastream.
313  *
314  * This function will return a dynamically allocated string of the form:
315  *
316  * $(PROC_TYPE_HOME)/conf/$(proc_type)/$(proc_name)/$(site)/$(site)$(name)$(facility).$(level)
317  *
318  * The calling process is responsible for freeing this string when it is
319  * no longer needed.
320  *
321  * Error messages from this function are sent to the message handler
322  * (see msngr_init_log() and msngr_init_mail()).
323  *
324  * @param proc_name - the name of the process
325  * @param proc_type - the process type (i.e. Ingest, VAP)
326  * @param site - site name
327  * @param facility - facility name
328  * @param name - datastream class name
329  * @param level - datastream class level, or NULL to exclude the level
330  * from the datastream directory name.
331  * @param path - output: pointer to the path of the
332  * apps conf directory
333  *
334  * @return
335  * - 1 if successful
336  * - 0 if the PROC_TYPE_HOME environment variable was not found
337  * - -1 if a memory allocation error occurred
338  */
340  const char *proc_name,
341  const char *proc_type,
342  const char *site,
343  const char *facility,
344  const char *name,
345  const char *level,
346  char **path)
347 {
348  char *root_dir;
349  int status;
350 
351  status = dsenv_get_apps_conf_root(proc_name, proc_type, &root_dir);
352 
353  if (status <= 0) {
354  *path = (char *)NULL;
355  return(0);
356  }
357 
358  *path = dsenv_create_full_path(
359  root_dir, site, facility, name, level);
360 
361  free(root_dir);
362 
363  if (!*path) {
364  return(-1);
365  }
366 
367  return(1);
368 }
369 
370 /**
371  * Get the root path of the data collection directory.
372  *
373  * This function returns a dynamically allocated copy of the COLLECTION_DATA
374  * environment variable. The calling process is responsible for freeing this
375  * string when it is no longer needed.
376  *
377  * Error messages from this function are sent to the message handler
378  * (see msngr_init_log() and msngr_init_mail()).
379  *
380  * @param path - output: pointer to the root path of the
381  * data collection directory
382  *
383  * @return
384  * - 1 if successful
385  * - 0 if the COLLECTION_DATA environment variable was not found
386  * - -1 if a memory allocation error occurred
387  */
389 {
390  return(dsenv_getenv("COLLECTION_DATA", path));
391 }
392 
393 /**
394  * Get the data collection directory for a datastream.
395  *
396  * This function will return a dynamically allocated string of the form:
397  *
398  * $(COLLECTION_DATA)/$(site)/$(site)$(name)$(facility).$(level)
399  *
400  * The calling process is responsible for freeing this string when it is
401  * no longer needed.
402  *
403  * Error messages from this function are sent to the message handler
404  * (see msngr_init_log() and msngr_init_mail()).
405  *
406  * @param site - site name
407  * @param facility - facility name
408  * @param name - datastream class name
409  * @param level - datastream class level
410  * @param path - output: pointer to the data collection directory
411  *
412  * @return
413  * - 1 if successful
414  * - 0 if the COLLECTION_DATA environment variable was not found
415  * - -1 if a memory allocation error occurred
416  */
418  const char *site,
419  const char *facility,
420  const char *name,
421  const char *level,
422  char **path)
423 {
424  const char *root_dir = getenv("COLLECTION_DATA");
425 
426  if (!root_dir) {
427  *path = (char *)NULL;
428  return(0);
429  }
430 
431  *path = dsenv_create_full_path(
432  root_dir, site, facility, name, level);
433 
434  if (!*path) {
435  return(-1);
436  }
437 
438  return(1);
439 }
440 
441 /**
442  * Get the root path of the data conf directory.
443  *
444  * This function returns a dynamically allocated copy of the CONF_DATA
445  * environment variable. The calling process is responsible for freeing this
446  * string when it is no longer needed.
447  *
448  * Error messages from this function are sent to the message handler
449  * (see msngr_init_log() and msngr_init_mail()).
450  *
451  * @param path - output: pointer to the root path of the
452  * data conf directory
453  *
454  * @return
455  * - 1 if successful
456  * - 0 if the CONF_DATA environment variable was not found
457  * - -1 if a memory allocation error occurred
458  */
459 int dsenv_get_data_conf_root(char **path)
460 {
461  return(dsenv_getenv("CONF_DATA", path));
462 }
463 
464 /**
465  * Get the data conf directory for a datastream.
466  *
467  * This function will return a dynamically allocated string of the form:
468  *
469  * $(CONF_DATA)/$(site)/$(site)$(name)$(facility).$(level)
470  *
471  * The calling process is responsible for freeing this string when it is
472  * no longer needed.
473  *
474  * Error messages from this function are sent to the message handler
475  * (see msngr_init_log() and msngr_init_mail()).
476  *
477  * @param site - site name
478  * @param facility - facility name
479  * @param name - datastream class name or process name
480  * @param level - datastream class level, or NULL to exclude the level
481  * from the datastream directory name.
482  * @param path - output: pointer to the data conf directory
483  *
484  * @return
485  * - 1 if successful
486  * - 0 if the CONF_DATA environment variable was not found
487  * - -1 if a memory allocation error occurred
488  */
490  const char *site,
491  const char *facility,
492  const char *name,
493  const char *level,
494  char **path)
495 {
496  const char *root_dir = getenv("CONF_DATA");
497 
498  if (!root_dir) {
499  *path = (char *)NULL;
500  return(0);
501  }
502 
503  *path = dsenv_create_full_path(
504  root_dir, site, facility, name, level);
505 
506  if (!*path) {
507  return(-1);
508  }
509 
510  return(1);
511 }
512 
513 /**
514  * Get the root path of the datastream directory.
515  *
516  * This function returns a dynamically allocated copy of the DATASTREAM_DATA
517  * environment variable. The calling process is responsible for freeing this
518  * string when it is no longer needed.
519  *
520  * Error messages from this function are sent to the message handler
521  * (see msngr_init_log() and msngr_init_mail()).
522  *
523  * @param path - output: pointer to the root path of the
524  * datastream directory
525  *
526  * @return
527  * - 1 if successful
528  * - 0 if the DATASTREAM_DATA environment variable was not found
529  * - -1 if a memory allocation error occurred
530  */
532 {
533  return(dsenv_getenv("DATASTREAM_DATA", path));
534 }
535 
536 /**
537  * Get the datastream directory for a datastream.
538  *
539  * This function will return a dynamically allocated string of the form:
540  *
541  * $(DATASTREAM_DATA)/$(site)/$(site)$(name)$(facility).$(level)
542  *
543  * The calling process is responsible for freeing this string when it is
544  * no longer needed.
545  *
546  * Error messages from this function are sent to the message handler
547  * (see msngr_init_log() and msngr_init_mail()).
548  *
549  * @param site - site name
550  * @param facility - facility name
551  * @param name - datastream class name
552  * @param level - datastream class level
553  * @param path - output: pointer to the datastream directory
554  *
555  * @return
556  * - 1 if successful
557  * - 0 if the DATASTREAM_DATA environment variable was not found
558  * - -1 if a memory allocation error occurred
559  */
561  const char *site,
562  const char *facility,
563  const char *name,
564  const char *level,
565  char **path)
566 {
567  const char *root_dir = getenv("DATASTREAM_DATA");
568 
569  if (!root_dir) {
570  *path = (char *)NULL;
571  return(0);
572  }
573 
574  *path = dsenv_create_full_path(
575  root_dir, site, facility, name, level);
576 
577  if (!*path) {
578  return(-1);
579  }
580 
581  return(1);
582 }
583 
584 /**
585  * Get the root path of the input datastream directory.
586  *
587  * This function returns a dynamically allocated copy of the first enviroment
588  * variable found in the following search order:
589  *
590  * - DATASTREAM_DATA_IN
591  * - DATASTREAM_DATA
592  *
593  * The calling process is responsible for freeing the returned string when
594  * it is no longer needed.
595  *
596  * Error messages from this function are sent to the message handler
597  * (see msngr_init_log() and msngr_init_mail()).
598  *
599  * @param path - output: pointer to the root path of the
600  * datastream directory
601  *
602  * @return
603  * - 1 if successful
604  * - 0 none of the environment variables were found
605  * - -1 if a memory allocation error occurred
606  */
608 {
609  int status = dsenv_getenv("DATASTREAM_DATA_IN", path);
610 
611  if (status == 0) {
612  status = dsenv_getenv("DATASTREAM_DATA", path);
613  }
614 
615  return(status);
616 }
617 
618 /**
619  * Get the input datastream directory.
620  *
621  * This function will return a dynamically allocated string of the form:
622  *
623  * root_dir/$(site)/$(site)$(name)$(facility).$(level)
624  *
625  * where root_dir is the value of the first enviroment variable found in
626  * the following search order:
627  *
628  * - DATASTREAM_DATA_IN
629  * - DATASTREAM_DATA
630  *
631  * The calling process is responsible for freeing this string when it is
632  * no longer needed.
633  *
634  * Error messages from this function are sent to the message handler
635  * (see msngr_init_log() and msngr_init_mail()).
636  *
637  * @param site - site name
638  * @param facility - facility name
639  * @param name - datastream class name
640  * @param level - datastream class level
641  * @param path - output: pointer to the datastream directory
642  *
643  * @return
644  * - 1 if successful
645  * - 0 none of the environment variables were found
646  * - -1 if a memory allocation error occurred
647  */
649  const char *site,
650  const char *facility,
651  const char *name,
652  const char *level,
653  char **path)
654 {
655  const char *root_dir = getenv("DATASTREAM_DATA_IN");
656 
657  if (!root_dir) {
658  root_dir = getenv("DATASTREAM_DATA");
659  if (!root_dir) {
660  *path = (char *)NULL;
661  return(0);
662  }
663  }
664 
665  *path = dsenv_create_full_path(
666  root_dir, site, facility, name, level);
667 
668  if (!*path) {
669  return(-1);
670  }
671 
672  return(1);
673 }
674 
675 /**
676  * Get the root path of the output datastream directory.
677  *
678  * This function returns a dynamically allocated copy of the first enviroment
679  * variable found in the following search order:
680  *
681  * - DATASTREAM_DATA_OUT
682  * - DATASTREAM_DATA
683  *
684  * The calling process is responsible for freeing the returned string when
685  * it is no longer needed.
686  *
687  * Error messages from this function are sent to the message handler
688  * (see msngr_init_log() and msngr_init_mail()).
689  *
690  * @param path - output: pointer to the root path of the
691  * datastream directory
692  *
693  * @return
694  * - 1 if successful
695  * - 0 none of the environment variables were found
696  * - -1 if a memory allocation error occurred
697  */
699 {
700  int status = dsenv_getenv("DATASTREAM_DATA_OUT", path);
701 
702  if (status == 0) {
703  status = dsenv_getenv("DATASTREAM_DATA", path);
704  }
705 
706  return(status);
707 }
708 
709 /**
710  * Get the output datastream directory.
711  *
712  * This function will return a dynamically allocated string of the form:
713  *
714  * root_dir/$(site)/$(site)$(name)$(facility).$(level)
715  *
716  * where root_dir is the value of the first enviroment variable found in
717  * the following search order:
718  *
719  * - DATASTREAM_DATA_OUT
720  * - DATASTREAM_DATA
721  *
722  * The calling process is responsible for freeing this string when it is
723  * no longer needed.
724  *
725  * Error messages from this function are sent to the message handler
726  * (see msngr_init_log() and msngr_init_mail()).
727  *
728  * @param site - site name
729  * @param facility - facility name
730  * @param name - datastream class name
731  * @param level - datastream class level
732  * @param path - output: pointer to the datastream directory
733  *
734  * @return
735  * - 1 if successful
736  * - 0 none of the environment variables were found
737  * - -1 if a memory allocation error occurred
738  */
740  const char *site,
741  const char *facility,
742  const char *name,
743  const char *level,
744  char **path)
745 {
746  const char *root_dir = getenv("DATASTREAM_DATA_OUT");
747 
748  if (!root_dir) {
749  root_dir = getenv("DATASTREAM_DATA");
750  if (!root_dir) {
751  *path = (char *)NULL;
752  return(0);
753  }
754  }
755 
756  *path = dsenv_create_full_path(
757  root_dir, site, facility, name, level);
758 
759  if (!*path) {
760  return(-1);
761  }
762 
763  return(1);
764 }
765 
766 /**
767  * Get the root path of the data tmp directory.
768  *
769  * This function returns a dynamically allocated copy of the TMP_DATA
770  * environment variable. The calling process is responsible for freeing this
771  * string when it is no longer needed.
772  *
773  * Error messages from this function are sent to the message handler
774  * (see msngr_init_log() and msngr_init_mail()).
775  *
776  * @param path - output: pointer to the root path of the
777  * data tmp directory
778  *
779  * @return
780  * - 1 if successful
781  * - 0 if the TMP_DATA environment variable was not found
782  * - -1 if a memory allocation error occurred
783  */
784 int dsenv_get_tmp_root(char **path)
785 {
786  return(dsenv_getenv("TMP_DATA", path));
787 }
788 
789 /**
790  * Get the root path of the data logs directory.
791  *
792  * This function returns a dynamically allocated copy of the LOGS_DATA
793  * environment variable. The calling process is responsible for freeing this
794  * string when it is no longer needed.
795  *
796  * Error messages from this function are sent to the message handler
797  * (see msngr_init_log() and msngr_init_mail()).
798  *
799  * @param path - output: pointer to the root path of the
800  * data logs directory
801  *
802  * @return
803  * - 1 if successful
804  * - 0 if the LOGS_DATA environment variable was not found
805  * - -1 if a memory allocation error occurred
806  */
807 int dsenv_get_logs_root(char **path)
808 {
809  return(dsenv_getenv("LOGS_DATA", path));
810 }
811 
812 /**
813  * Get the root path of the quicklook directory.
814  *
815  * This function returns a dynamically allocated copy of the QUICKLOOK_DATA
816  * environment variable. The calling process is responsible for freeing this
817  * string when it is no longer needed.
818  *
819  * Error messages from this function are sent to the message handler
820  * (see msngr_init_log() and msngr_init_mail()).
821  *
822  * @param path - output: pointer to the root path of the
823  * quicklook directory
824  *
825  * @return
826  * - 1 if successful
827  * - 0 if the QUICKLOOK_DATA environment variable was not found
828  * - -1 if a memory allocation error occurred
829  */
830 int dsenv_get_quicklook_root(char **path)
831 {
832  return(dsenv_getenv("QUICKLOOK_DATA", path));
833 }