78
* This hash table holds the list of known sources of configurations,
79
* indexed by their path. Each entry is a ConfSource structure, multiple
80
* entries for the same path may not exist.
78
* This list holds the list of known sources of configuration; each item
79
* is a ConfSource structure. The order of this list dictates the priority
80
* of the sources, with the first one having the highest priority.
82
NihHash *conf_sources = NULL;
82
NihList *conf_sources = NULL;
88
* Initialise the conf_sources hash table.
88
* Initialise the conf_sources list.
93
93
if (! conf_sources)
94
NIH_MUST (conf_sources = nih_hash_new (NULL, 0,
95
(NihKeyFunction)nih_hash_string_key));
94
NIH_MUST (conf_sources = nih_list_new (NULL));
100
99
* conf_source_new:
101
100
* @parent: parent of new block,
102
101
* @path: path to source,
103
* @type: type of source,
104
* @priority: priority for selection.
102
* @type: type of source.
106
104
* Allocates and returns a new ConfSource structure for the given @path;
107
105
* @type indicates whether this @path is a file or directory and what type
108
* of files are within the directory; @priority is used when multiple items
109
* have the same name.
106
* of files are within the directory.
111
* The returned structure is automatically placed in the conf_sources hash
112
* table, indexed by @path.
108
* The returned structure is automatically added to the conf_sources list.
114
110
* Configuration is not parsed immediately, instead you must call
115
111
* conf_source_reload() on this source to set up any watches and load the
175
168
* Allocates and returns a new ConfFile structure for the given @source,
176
169
* with @path indicating which file it is.
178
* The returned structure is automatically placed in the @source files hash
171
* The returned structure is automatically placed in the @source's files hash
179
172
* and the flag of the returned ConfFile will be set to that of the @source.
181
174
* Returns: newly allocated ConfFile structure or NULL if insufficient memory.
196
189
nih_list_init (&file->entry);
198
file->source = source;
200
191
file->path = nih_strdup (file, path);
201
192
if (! file->path) {
206
nih_list_init (&file->items);
197
file->source = source;
198
file->flag = source->flag;
208
nih_alloc_set_destructor (file, (NihDestructor)nih_list_destroy);
201
nih_alloc_set_destructor (file, (NihDestructor)conf_file_destroy);
210
203
nih_hash_add (source->files, &file->entry);
212
file->flag = source->flag;
219
* @source: configuration source,
220
* @path: path to file.
222
* Looks up the ConfFile entry in @source for @path, or allocates a new
223
* structure and places it in the files hash table before returning it.
225
* The flag of the returned ConfFile will be set to that of the @source.
227
* Returns: existing or newly allocated ConfFile structure,
228
* or NULL if insufficient memory.
231
conf_file_get (ConfSource *source,
236
nih_assert (source != NULL);
237
nih_assert (path != NULL);
239
file = (ConfFile *)nih_hash_lookup (source->files, path);
241
file->flag = source->flag;
243
file = conf_file_new (source, path);
251
* @file: file to attach to,
252
* @type: type of item.
254
* Allocates and returns a new ConfItem structure for the given @file,
255
* with @type indicating what kind of data will be attached
256
* to this item. Setting the data pointer is the job of the caller.
258
* The returned structure is automatically placed in the @file items list.
260
* Returns: newly allocated ConfItem structure or NULL if insufficient memory.
263
conf_item_new (ConfFile *file,
268
nih_assert (file != NULL);
270
item = nih_new (file, ConfItem);
274
nih_list_init (&item->entry);
280
nih_alloc_set_destructor (item, (NihDestructor)conf_item_destroy);
282
nih_list_add (&file->items, &item->entry);
389
310
* and parsed. It is the parent directory because we need to watch out for
390
311
* editors that rename over the top, etc.
392
* We then parse the current state of the file, propogating the value of
393
* the flag member to all items that we find so that deletions can be
394
* detected by the calling function.
396
313
* Returns: zero on success, negative value on raised error.
470
387
* Otherwise we walk the tree ourselves and parse all files that we find,
471
* propogating the value of the flag member to all files and items so that
472
* deletion can be detected by the calling function.
388
* propogating the value of the flag member to all files so that deletion
389
* can be detected by the calling function.
474
391
* Returns: zero on success, negative value on raised error.
616
533
* filtered to only return the path that we're interested in.
618
535
* We lookup the file in our hash table, and if we can find it, perform
619
* the usual deletion on all of its items and the file itself.
536
* the usual deletion of it.
622
539
conf_delete_handler (ConfSource *source,
697
614
* @path: path of file to be reloaded.
699
616
* This function is used to parse the file at @path in the context of the
700
* given configuration @source. Necessary ConfFile and ConfItem structures
701
* are allocated and attached to the @source as appropriate.
703
* Depending on the type of the @source, this can parse jobs directly (in
704
* which case they are named after the relative part of the path) or mixed
705
* configuration files which define the names specifically.
707
* If the file has been parsed before, then any existing items are deleted
708
* and freed if the file fails to load, or after the new items have been
709
* parsed. Items are not reused for the same apparent item between reloads,
710
* since there's no real meaning to it.
617
* given configuration @source. Necessary ConfFile structures are allocated
618
* and attached to @source as appropriate. CONF_FILE sources always have
619
* a single ConfFile when the file exists.
621
* If the file has been parsed before, then the existing item is deleted and
622
* freed if the file fails to load, or after the new item has been parsed.
623
* Items are not reused between reloads.
712
625
* Physical errors are returned, parse errors are not.
727
638
nih_assert (source != NULL);
728
639
nih_assert (path != NULL);
730
NIH_MUST (file = conf_file_get (source, path));
732
/* Map the file into memory for parsing. If this fails, then we
733
* delete all the current items since there's clearly a problem.
735
buf = nih_file_map (file->path, O_RDONLY | O_NOCTTY, &len);
737
NIH_LIST_FOREACH_SAFE (&file->items, iter) {
738
ConfItem *item = (ConfItem *)iter;
641
/* Look up the old file in memory, and then free it. In cases
642
* of failure, we discard it anyway, so there's no particular reason
643
* to keep it around anymore.
645
file = (ConfFile *)nih_hash_lookup (source->files, path);
649
/* Map the file into memory for parsing, if this fails we don't
650
* bother creating a new ConfFile structure for it and bail out
653
buf = nih_file_map (path, O_RDONLY | O_NOCTTY, &len);
746
/* If we've parsed this file before, we'll have a list of old items
747
* that once existed and need to be cleaned up once we've parsed
748
* the new items. The easiest way to identify them is to put
749
* them into a different list for safe-keeping.
751
nih_list_init (&old_items);
752
nih_list_add (&file->items, &old_items);
753
nih_list_remove (&file->items);
755
/* Parse the file buffer, registering items found against the
756
* ConfFile; the existing items are removed later.
657
/* Parse the file, storing the item in a new ConfFile structure. */
658
NIH_MUST (file = conf_file_new (source, path));
761
663
switch (source->type) {
764
/* Parse the file, this deals with item creation itself
765
* since only it knows the item types and names.
666
/* Simple file of options; usually no item attached to it. */
767
667
nih_debug ("Loading configuration from %s", path);
768
668
if (parse_conf (file, buf, len, &pos, &lineno) < 0)
769
669
err = nih_error_get ();
783
683
/* Create a new job item and parse the buffer to produce
784
* the job definition. Discard the item if this fails.
684
* the job definition.
786
686
nih_debug ("Loading %s from %s", name, path);
787
NIH_MUST (item = conf_item_new (file, CONF_JOB));
788
item->job = parse_job (NULL, name, buf, len, &pos, &lineno);
687
file->job = parse_job (NULL, name, buf, len, &pos, &lineno);
790
689
err = nih_error_get ();
797
693
nih_assert_not_reached ();
827
/* Delete the old items now we've parsed in the list of new ones.
829
NIH_LIST_FOREACH_SAFE (&old_items, iter) {
830
ConfItem *item = (ConfItem *)iter;
835
723
/* Unmap the file again; in theory this shouldn't fail, but if
836
724
* it does, return an error condition even though we've actually
837
725
* loaded some of the new things.
858
* conf_item_destructor:
859
* @item: configuration item to be destroyed.
747
* @file: configuration file to be destroyed.
861
* Handles the replacement and deletion of the item itself.
749
* Handles the replacement and deletion of a configuration file, ensuring
750
* that @file is removed from the containing linked list and that the item
751
* attached to it is destroyed if not currently in use.
863
753
* Normally used or called from an nih_alloc() destructor so that the list
864
754
* item is automatically removed from its containing list when freed.
869
conf_item_destroy (ConfItem *item)
759
conf_file_destroy (ConfFile *file)
871
nih_assert (item != NULL);
761
nih_assert (file != NULL);
873
switch (item->type) {
763
switch (file->source->type) {
878
771
/* If the item being deleted hasn't already been marked for
879
772
* replacement, mark it for deletion.
881
if (! item->job->replacement)
882
item->job->replacement = (void *)-1;
774
if (! file->job->replacement)
775
file->job->replacement = (void *)-1;
884
777
/* If the item being deleted is the replacement for another,
885
778
* cut out the middle man and make that item's replacement
886
779
* the same as our own replacement (ie. probably deletion)
888
if ((item->job->replacement_for != NULL)
889
&& (item->job->replacement_for->replacement == item->job))
890
item->job->replacement_for->replacement \
891
= item->job->replacement;
781
if ((file->job->replacement_for != NULL)
782
&& (file->job->replacement_for->replacement == file->job))
783
file->job->replacement_for->replacement \
784
= file->job->replacement;
893
786
/* Delete the job if we can */
894
if (job_config_should_replace (item->job)) {
787
if (job_config_should_replace (file->job)) {
895
788
/* If the item being deleted has a replacement, ensure
896
789
* that the replacement doesn't reference it anymore.
898
if ((item->job->replacement != NULL)
899
&& (item->job->replacement != (void *)-1)
900
&& (item->job->replacement->replacement_for == item->job))
901
item->job->replacement->replacement_for = NULL;
791
if ((file->job->replacement != NULL)
792
&& (file->job->replacement != (void *)-1)
793
&& (file->job->replacement->replacement_for == file->job))
794
file->job->replacement->replacement_for = NULL;
903
nih_free (item->job);
796
nih_free (file->job);
801
nih_assert_not_reached ();
909
nih_list_destroy (&item->entry);
804
nih_list_destroy (&file->entry);