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