tighten formatting rules
[oweals/gnunet.git] / src / fs / gnunet-auto-share.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001--2012 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 /**
21  * @file fs/gnunet-auto-share.c
22  * @brief automatically publish files on GNUnet
23  * @author Christian Grothoff
24  *
25  * TODO:
26  * - support loading meta data / keywords from resource file
27  * - add stability timer (a la buildbot)
28  */
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31
32 #define MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
33
34 #define MIN_DELAY GNUNET_TIME_UNIT_MINUTES
35
36
37 /**
38  * Item in our work queue (or in the set of files/directories
39  * we have successfully published).
40  */
41 struct WorkItem
42 {
43   /**
44    * PENDING Work is kept in a linked list.
45    */
46   struct WorkItem *prev;
47
48   /**
49    * PENDING Work is kept in a linked list.
50    */
51   struct WorkItem *next;
52
53   /**
54    * Filename of the work item.
55    */
56   char *filename;
57
58   /**
59    * Unique identity for this work item (used to detect
60    * if we need to do the work again).
61    */
62   struct GNUNET_HashCode id;
63 };
64
65
66 /**
67  * Global return value from 'main'.
68  */
69 static int ret;
70
71 /**
72  * Are we running 'verbosely'?
73  */
74 static unsigned int verbose;
75
76 /**
77  * Configuration to use.
78  */
79 static const struct GNUNET_CONFIGURATION_Handle *cfg;
80
81 /**
82  * Name of the configuration file.
83  */
84 static char *cfg_filename;
85
86 /**
87  * Disable extractor option to use for publishing.
88  */
89 static int disable_extractor;
90
91 /**
92  * Disable creation time option to use for publishing.
93  */
94 static int do_disable_creation_time;
95
96 /**
97  * Handle for the main task that does scanning and working.
98  */
99 static struct GNUNET_SCHEDULER_Task *run_task;
100
101 /**
102  * Anonymity level option to use for publishing.
103  */
104 static unsigned int anonymity_level = 1;
105
106 /**
107  * Content priority option to use for publishing.
108  */
109 static unsigned int content_priority = 365;
110
111 /**
112  * Replication level option to use for publishing.
113  */
114 static unsigned int replication_level = 1;
115
116 /**
117  * Top-level directory we monitor to auto-publish.
118  */
119 static const char *dir_name;
120
121 /**
122  * Head of linked list of files still to publish.
123  */
124 static struct WorkItem *work_head;
125
126 /**
127  * Tail of linked list of files still to publish.
128  */
129 static struct WorkItem *work_tail;
130
131 /**
132  * Map from the hash of the filename (!) to a `struct WorkItem`
133  * that was finished.
134  */
135 static struct GNUNET_CONTAINER_MultiHashMap *work_finished;
136
137 /**
138  * Set to #GNUNET_YES if we are shutting down.
139  */
140 static int do_shutdown;
141
142 /**
143  * Start time of the current round; used to determine how long
144  * one iteration takes (which influences how fast we schedule
145  * the next one).
146  */
147 static struct GNUNET_TIME_Absolute start_time;
148
149 /**
150  * Pipe used to communicate 'gnunet-publish' completion (SIGCHLD) via signal.
151  */
152 static struct GNUNET_DISK_PipeHandle *sigpipe;
153
154 /**
155  * Handle to the 'gnunet-publish' process that we executed.
156  */
157 static struct GNUNET_OS_Process *publish_proc;
158
159
160 /**
161  * Compute the name of the state database file we will use.
162  */
163 static char *
164 get_state_file ()
165 {
166   char *ret;
167
168   GNUNET_asprintf (&ret,
169                    "%s%s.auto-share",
170                    dir_name,
171                    (DIR_SEPARATOR == dir_name[strlen (dir_name) - 1])
172                    ? ""
173                    : DIR_SEPARATOR_STR);
174   return ret;
175 }
176
177
178 /**
179  * Load the set of #work_finished items from disk.
180  */
181 static void
182 load_state ()
183 {
184   char *fn;
185   struct GNUNET_BIO_ReadHandle *rh;
186   uint32_t n;
187   struct GNUNET_HashCode id;
188   struct WorkItem *wi;
189   char *emsg;
190
191   emsg = NULL;
192   fn = get_state_file ();
193   rh = GNUNET_BIO_read_open (fn);
194   GNUNET_free (fn);
195   if (NULL == rh)
196     return;
197   fn = NULL;
198   if (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &n))
199     goto error;
200   while (n-- > 0)
201   {
202     if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "filename", &fn, 1024)) ||
203         (GNUNET_OK !=
204          GNUNET_BIO_read (rh, "id", &id, sizeof(struct GNUNET_HashCode))))
205       goto error;
206     wi = GNUNET_new (struct WorkItem);
207     wi->id = id;
208     wi->filename = fn;
209     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
210                 "Loaded serialization ID for `%s' is `%s'\n",
211                 wi->filename,
212                 GNUNET_h2s (&id));
213     fn = NULL;
214     GNUNET_CRYPTO_hash (wi->filename, strlen (wi->filename), &id);
215     GNUNET_break (GNUNET_OK ==
216                   GNUNET_CONTAINER_multihashmap_put (
217                     work_finished,
218                     &id,
219                     wi,
220                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
221   }
222   if (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg))
223     return;
224   rh = NULL;
225 error:
226   GNUNET_free_non_null (fn);
227   if (NULL != rh)
228     (void) GNUNET_BIO_read_close (rh, &emsg);
229   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
230               _ ("Failed to load state: %s\n"),
231               emsg);
232   GNUNET_free_non_null (emsg);
233 }
234
235
236 /**
237  * Write work item from the #work_finished map to the given write handle.
238  *
239  * @param cls the `struct GNUNET_BIO_WriteHandle *`
240  * @param key key of the item in the map (unused)
241  * @param value the `struct WorkItem` to write
242  * @return #GNUNET_OK to continue to iterate (if write worked)
243  */
244 static int
245 write_item (void *cls, const struct GNUNET_HashCode *key, void *value)
246 {
247   struct GNUNET_BIO_WriteHandle *wh = cls;
248   struct WorkItem *wi = value;
249
250   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251               "Saving serialization ID of file `%s' with value `%s'\n",
252               wi->filename,
253               GNUNET_h2s (&wi->id));
254   if ((GNUNET_OK != GNUNET_BIO_write_string (wh, wi->filename)) ||
255       (GNUNET_OK !=
256        GNUNET_BIO_write (wh, &wi->id, sizeof(struct GNUNET_HashCode))))
257     return GNUNET_SYSERR; /* write error, abort iteration */
258   return GNUNET_OK;
259 }
260
261
262 /**
263  * Save the set of #work_finished items on disk.
264  */
265 static void
266 save_state ()
267 {
268   uint32_t n;
269   struct GNUNET_BIO_WriteHandle *wh;
270   char *fn;
271
272   n = GNUNET_CONTAINER_multihashmap_size (work_finished);
273   fn = get_state_file ();
274   wh = GNUNET_BIO_write_open (fn);
275   if (NULL == wh)
276   {
277     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
278                 _ ("Failed to save state to file %s\n"),
279                 fn);
280     GNUNET_free (fn);
281     return;
282   }
283   if (GNUNET_OK != GNUNET_BIO_write_int32 (wh, n))
284   {
285     (void) GNUNET_BIO_write_close (wh);
286     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
287                 _ ("Failed to save state to file %s\n"),
288                 fn);
289     GNUNET_free (fn);
290     return;
291   }
292   (void) GNUNET_CONTAINER_multihashmap_iterate (work_finished, &write_item, wh);
293   if (GNUNET_OK != GNUNET_BIO_write_close (wh))
294     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
295                 _ ("Failed to save state to file %s\n"),
296                 fn);
297   GNUNET_free (fn);
298 }
299
300
301 /**
302  * Task run on shutdown.  Serializes our current state to disk.
303  *
304  * @param cls closure, unused
305  */
306 static void
307 do_stop_task (void *cls)
308 {
309   do_shutdown = GNUNET_YES;
310   if (NULL != publish_proc)
311   {
312     GNUNET_OS_process_kill (publish_proc, SIGKILL);
313     return;
314   }
315   if (NULL != run_task)
316   {
317     GNUNET_SCHEDULER_cancel (run_task);
318     run_task = NULL;
319   }
320 }
321
322
323 /**
324  * Decide what the next task is (working or scanning) and schedule it.
325  */
326 static void
327 schedule_next_task (void);
328
329
330 /**
331  * Task triggered whenever we receive a SIGCHLD (child
332  * process died).
333  *
334  * @param cls the `struct WorkItem` we were working on
335  */
336 static void
337 maint_child_death (void *cls)
338 {
339   struct WorkItem *wi = cls;
340   struct GNUNET_HashCode key;
341   enum GNUNET_OS_ProcessStatusType type;
342   unsigned long code;
343   int ret;
344   char c;
345   const struct GNUNET_DISK_FileHandle *pr;
346   const struct GNUNET_SCHEDULER_TaskContext *tc;
347
348   run_task = NULL;
349   pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
350   tc = GNUNET_SCHEDULER_get_task_context ();
351   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
352   {
353     /* shutdown scheduled us, someone else will kill child,
354        we should just try again */
355     run_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
356                                                pr,
357                                                &maint_child_death,
358                                                wi);
359     return;
360   }
361   /* consume the signal */
362   GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof(c)));
363
364   ret = GNUNET_OS_process_status (publish_proc, &type, &code);
365   GNUNET_assert (GNUNET_SYSERR != ret);
366   if (GNUNET_NO == ret)
367   {
368     /* process still running? Then where did the SIGCHLD come from?
369        Well, let's declare it spurious (kernel bug?) and keep rolling.
370      */
371     GNUNET_break (0);
372     run_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
373                                                pr,
374                                                &maint_child_death,
375                                                wi);
376     return;
377   }
378   GNUNET_assert (GNUNET_OK == ret);
379
380   GNUNET_OS_process_destroy (publish_proc);
381   publish_proc = NULL;
382
383   if (GNUNET_YES == do_shutdown)
384   {
385     GNUNET_free (wi->filename);
386     GNUNET_free (wi);
387     return;
388   }
389   if ((GNUNET_OS_PROCESS_EXITED == type) && (0 == code))
390   {
391     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
392                 _ ("Publication of `%s' done\n"),
393                 wi->filename);
394     GNUNET_CRYPTO_hash (wi->filename, strlen (wi->filename), &key);
395     GNUNET_break (GNUNET_OK ==
396                   GNUNET_CONTAINER_multihashmap_put (
397                     work_finished,
398                     &key,
399                     wi,
400                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
401   }
402   else
403   {
404     GNUNET_CONTAINER_DLL_insert_tail (work_head, work_tail, wi);
405   }
406   save_state ();
407   schedule_next_task ();
408 }
409
410
411 /**
412  * Signal handler called for SIGCHLD.  Triggers the
413  * respective handler by writing to the trigger pipe.
414  */
415 static void
416 sighandler_child_death ()
417 {
418   static char c;
419   int old_errno = errno; /* back-up errno */
420
421   GNUNET_break (
422     1 ==
423     GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (sigpipe,
424                                                      GNUNET_DISK_PIPE_END_WRITE),
425                             &c,
426                             sizeof(c)));
427   errno = old_errno; /* restore errno */
428 }
429
430
431 /**
432  * Function called to process work items.
433  *
434  * @param cls closure, NULL
435  */
436 static void
437 work (void *cls)
438 {
439   static char *argv[14];
440   static char anon_level[20];
441   static char content_prio[20];
442   static char repl_level[20];
443   struct WorkItem *wi;
444   const struct GNUNET_DISK_FileHandle *pr;
445   int argc;
446
447   run_task = NULL;
448   wi = work_head;
449   GNUNET_CONTAINER_DLL_remove (work_head, work_tail, wi);
450   argc = 0;
451   argv[argc++] = "gnunet-publish";
452   if (verbose)
453     argv[argc++] = "-V";
454   if (disable_extractor)
455     argv[argc++] = "-D";
456   if (do_disable_creation_time)
457     argv[argc++] = "-d";
458   argv[argc++] = "-c";
459   argv[argc++] = cfg_filename;
460   GNUNET_snprintf (anon_level, sizeof(anon_level), "%u", anonymity_level);
461   argv[argc++] = "-a";
462   argv[argc++] = anon_level;
463   GNUNET_snprintf (content_prio, sizeof(content_prio), "%u", content_priority);
464   argv[argc++] = "-p";
465   argv[argc++] = content_prio;
466   GNUNET_snprintf (repl_level, sizeof(repl_level), "%u", replication_level);
467   argv[argc++] = "-r";
468   argv[argc++] = repl_level;
469   argv[argc++] = wi->filename;
470   argv[argc] = NULL;
471   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Publishing `%s'\n"), wi->filename);
472   GNUNET_assert (NULL == publish_proc);
473   publish_proc = GNUNET_OS_start_process_vap (GNUNET_YES,
474                                               0,
475                                               NULL,
476                                               NULL,
477                                               NULL,
478                                               "gnunet-publish",
479                                               argv);
480   if (NULL == publish_proc)
481   {
482     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
483                 _ ("Failed to run `%s'\n"),
484                 "gnunet-publish");
485     GNUNET_CONTAINER_DLL_insert (work_head, work_tail, wi);
486     run_task =
487       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &work, NULL);
488     return;
489   }
490   pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
491   run_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
492                                              pr,
493                                              &maint_child_death,
494                                              wi);
495 }
496
497
498 /**
499  * Recursively scan the given file/directory structure to determine
500  * a unique ID that represents the current state of the hierarchy.
501  *
502  * @param cls where to store the unique ID we are computing
503  * @param filename file to scan
504  * @return #GNUNET_OK (always)
505  */
506 static int
507 determine_id (void *cls, const char *filename)
508 {
509   struct GNUNET_HashCode *id = cls;
510   struct stat sbuf;
511   struct GNUNET_HashCode fx[2];
512   struct GNUNET_HashCode ft;
513
514   if (0 != stat (filename, &sbuf))
515   {
516     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
517     return GNUNET_OK;
518   }
519   GNUNET_CRYPTO_hash (filename, strlen (filename), &fx[0]);
520   if (! S_ISDIR (sbuf.st_mode))
521   {
522     uint64_t fattr[2];
523
524     fattr[0] = GNUNET_htonll (sbuf.st_size);
525     fattr[0] = GNUNET_htonll (sbuf.st_mtime);
526
527     GNUNET_CRYPTO_hash (fattr, sizeof(fattr), &fx[1]);
528   }
529   else
530   {
531     memset (&fx[1], 1, sizeof(struct GNUNET_HashCode));
532     GNUNET_DISK_directory_scan (filename, &determine_id, &fx[1]);
533   }
534   /* use hash here to make hierarchical structure distinct from
535      all files on the same level */
536   GNUNET_CRYPTO_hash (fx, sizeof(fx), &ft);
537   /* use XOR here so that order of the files in the directory
538      does not matter! */
539   GNUNET_CRYPTO_hash_xor (&ft, id, id);
540   return GNUNET_OK;
541 }
542
543
544 /**
545  * Function called with a filename (or directory name) to publish
546  * (if it has changed since the last time we published it).  This function
547  * is called for the top-level files only.
548  *
549  * @param cls closure, NULL
550  * @param filename complete filename (absolute path)
551  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR during shutdown
552  */
553 static int
554 add_file (void *cls, const char *filename)
555 {
556   struct WorkItem *wi;
557   struct GNUNET_HashCode key;
558   struct GNUNET_HashCode id;
559
560   if (GNUNET_YES == do_shutdown)
561     return GNUNET_SYSERR;
562   if ((NULL != strstr (filename, "/.auto-share")) ||
563       (NULL != strstr (filename, "\\.auto-share")))
564     return GNUNET_OK; /* skip internal file */
565   GNUNET_CRYPTO_hash (filename, strlen (filename), &key);
566   wi = GNUNET_CONTAINER_multihashmap_get (work_finished, &key);
567   memset (&id, 0, sizeof(struct GNUNET_HashCode));
568   determine_id (&id, filename);
569   if (NULL != wi)
570   {
571     if (0 == memcmp (&id, &wi->id, sizeof(struct GNUNET_HashCode)))
572       return GNUNET_OK;   /* skip: we did this one already */
573     /* contents changed, need to re-do the directory... */
574     GNUNET_assert (
575       GNUNET_YES ==
576       GNUNET_CONTAINER_multihashmap_remove (work_finished, &key, wi));
577   }
578   else
579   {
580     wi = GNUNET_new (struct WorkItem);
581     wi->filename = GNUNET_strdup (filename);
582   }
583   wi->id = id;
584   GNUNET_CONTAINER_DLL_insert (work_head, work_tail, wi);
585   if (GNUNET_YES == do_shutdown)
586     return GNUNET_SYSERR;
587   return GNUNET_OK;
588 }
589
590
591 /**
592  * Periodically run task to update our view of the directory to share.
593  *
594  * @param cls NULL
595  */
596 static void
597 scan (void *cls)
598 {
599   run_task = NULL;
600   start_time = GNUNET_TIME_absolute_get ();
601   (void) GNUNET_DISK_directory_scan (dir_name, &add_file, NULL);
602   schedule_next_task ();
603 }
604
605
606 /**
607  * Decide what the next task is (working or scanning) and schedule it.
608  */
609 static void
610 schedule_next_task ()
611 {
612   struct GNUNET_TIME_Relative delay;
613
614   if (GNUNET_YES == do_shutdown)
615     return;
616   GNUNET_assert (NULL == run_task);
617   if (NULL == work_head)
618   {
619     /* delay by at most 4h, at least 1s, and otherwise in between depending
620        on how long it took to scan */
621     delay = GNUNET_TIME_absolute_get_duration (start_time);
622     delay = GNUNET_TIME_relative_saturating_multiply (delay, 100);
623     delay = GNUNET_TIME_relative_min (delay, MAX_DELAY);
624     delay = GNUNET_TIME_relative_max (delay, MIN_DELAY);
625     run_task = GNUNET_SCHEDULER_add_delayed (delay, &scan, NULL);
626   }
627   else
628   {
629     run_task = GNUNET_SCHEDULER_add_now (&work, NULL);
630   }
631 }
632
633
634 /**
635  * Main function that will be run by the scheduler.
636  *
637  * @param cls closure
638  * @param args remaining command-line arguments
639  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
640  * @param c configuration
641  */
642 static void
643 run (void *cls,
644      char *const *args,
645      const char *cfgfile,
646      const struct GNUNET_CONFIGURATION_Handle *c)
647 {
648   /* check arguments */
649   if ((NULL == args[0]) || (NULL != args[1]) ||
650       (GNUNET_YES != GNUNET_DISK_directory_test (args[0], GNUNET_YES)))
651   {
652     printf (_ (
653               "You must specify one and only one directory name for automatic publication.\n"));
654     ret = -1;
655     return;
656   }
657   cfg_filename = GNUNET_strdup (cfgfile);
658   cfg = c;
659   dir_name = args[0];
660   work_finished = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
661   load_state ();
662   run_task = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
663                                                  &scan,
664                                                  NULL);
665   GNUNET_SCHEDULER_add_shutdown (&do_stop_task, NULL);
666 }
667
668
669 /**
670  * Free memory associated with the work item from the work_finished map.
671  *
672  * @param cls NULL (unused)
673  * @param key key of the item in the map (unused)
674  * @param value the `struct WorkItem` to free
675  * @return #GNUNET_OK to continue to iterate
676  */
677 static int
678 free_item (void *cls, const struct GNUNET_HashCode *key, void *value)
679 {
680   struct WorkItem *wi = value;
681
682   GNUNET_free (wi->filename);
683   GNUNET_free (wi);
684   return GNUNET_OK;
685 }
686
687
688 /**
689  * The main function to automatically publish content to GNUnet.
690  *
691  * @param argc number of arguments from the command line
692  * @param argv command line arguments
693  * @return 0 ok, 1 on error
694  */
695 int
696 main (int argc, char *const *argv)
697 {
698   struct GNUNET_GETOPT_CommandLineOption options[] = {
699     GNUNET_GETOPT_option_uint ('a',
700                                "anonymity",
701                                "LEVEL",
702                                gettext_noop (
703                                  "set the desired LEVEL of sender-anonymity"),
704                                &anonymity_level),
705
706     GNUNET_GETOPT_option_flag (
707       'd',
708       "disable-creation-time",
709       gettext_noop (
710         "disable adding the creation time to the metadata of the uploaded file"),
711       &do_disable_creation_time),
712
713     GNUNET_GETOPT_option_flag (
714       'D',
715       "disable-extractor",
716       gettext_noop ("do not use libextractor to add keywords or metadata"),
717       &disable_extractor),
718
719     GNUNET_GETOPT_option_uint ('p',
720                                "priority",
721                                "PRIORITY",
722                                gettext_noop (
723                                  "specify the priority of the content"),
724                                &content_priority),
725
726     GNUNET_GETOPT_option_uint ('r',
727                                "replication",
728                                "LEVEL",
729                                gettext_noop (
730                                  "set the desired replication LEVEL"),
731                                &replication_level),
732
733     GNUNET_GETOPT_option_verbose (&verbose),
734
735     GNUNET_GETOPT_OPTION_END
736   };
737   struct WorkItem *wi;
738   int ok;
739   struct GNUNET_SIGNAL_Context *shc_chld;
740
741   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
742     return 2;
743   sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
744   GNUNET_assert (NULL != sigpipe);
745   shc_chld =
746     GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
747   ok =
748     (GNUNET_OK ==
749      GNUNET_PROGRAM_run (
750        argc,
751        argv,
752        "gnunet-auto-share [OPTIONS] FILENAME",
753        gettext_noop ("Automatically publish files from a directory on GNUnet"),
754        options,
755        &run,
756        NULL))
757     ? ret
758     : 1;
759   if (NULL != work_finished)
760   {
761     (void) GNUNET_CONTAINER_multihashmap_iterate (work_finished,
762                                                   &free_item,
763                                                   NULL);
764     GNUNET_CONTAINER_multihashmap_destroy (work_finished);
765   }
766   while (NULL != (wi = work_head))
767   {
768     GNUNET_CONTAINER_DLL_remove (work_head, work_tail, wi);
769     GNUNET_free (wi->filename);
770     GNUNET_free (wi);
771   }
772   GNUNET_SIGNAL_handler_uninstall (shc_chld);
773   shc_chld = NULL;
774   GNUNET_DISK_pipe_close (sigpipe);
775   sigpipe = NULL;
776   GNUNET_free_non_null (cfg_filename);
777   cfg_filename = NULL;
778   GNUNET_free ((void *) argv);
779   return ok;
780 }
781
782
783 /* end of gnunet-auto-share.c */