~upstart-devel/upstart/trunk

« back to all changes in this revision

Viewing changes to init/log.c

mergeĀ lp:~vorlon/upstart/upstart-fix-racy-tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
 *
43
43
 * TRUE if log_clear_unflushed() has been called successfully.
44
44
 **/
45
 
static int log_flushed = 0;
 
45
int log_flushed = 0;
46
46
 
47
47
/**
48
48
 * log_unflushed_files:
758
758
 
759
759
        log_unflushed_init ();
760
760
 
761
 
        /* re-parent */
762
 
        nih_ref (log, log_unflushed_files);
763
 
        nih_unref (log, parent);
764
 
 
765
 
        elem = nih_list_entry_new (log);
 
761
        elem = nih_list_entry_new (log_unflushed_files);
766
762
        if (! elem) {
767
763
                /* If memory is low, we discard the unflushed
768
764
                 * data buffer too.
769
765
                 */
770
 
                nih_unref (log, log_unflushed_files);
 
766
                nih_unref (log, parent);
771
767
                return -1;
772
768
        }
773
769
 
 
770
        /* re-parent */
 
771
        nih_ref (log, elem);
 
772
        nih_unref (log, parent);
 
773
 
774
774
        /* Indicate separation from parent */
775
775
        log->detached = 1;
776
776
 
777
 
        elem->data = log;    
 
777
        elem->data = log;
778
778
        nih_list_add_after (log_unflushed_files, &elem->entry);
779
779
 
780
780
        return 0;
800
800
                elem = (NihListEntry *)iter;
801
801
                log = elem->data;
802
802
 
 
803
                nih_assert (log);
 
804
 
 
805
                if (! log->unflushed->len) {
 
806
                        /* The job that originally owned this log has
 
807
                         * exited, but it spawned one or more other
 
808
                         * processes which still live on. If those
 
809
                         * processes produce output, the NihIo will get
 
810
                         * called automatically to flush the data.
 
811
                         * However, that happens asynchronously to
 
812
                         * clearing the unflushed list.
 
813
                         *
 
814
                         * Hence, if no unflushed data remains, remove
 
815
                         * the entry from the list.
 
816
                         *
 
817
                         * Note that doing so frees the log and stops
 
818
                         * any further output being logged from the
 
819
                         * out-of-job process(es). This isn't ideal
 
820
                         * however:
 
821
                         *
 
822
                         * - Better this than having the Log objects remain indefinitely.
 
823
                         *
 
824
                         * - The problem is ultimately caused by a rogue
 
825
                         *   job which should be fixed (see the warning
 
826
                         *   that is produced for such jobs in
 
827
                         *   log_read_watch()).
 
828
                         *
 
829
                         * - Even if the Log was retained, it would not
 
830
                         *   be flushed unless multiple calls to this
 
831
                         *   function are made (which is not going to
 
832
                         *   occur by default).
 
833
                         *
 
834
                         * Note that it is necessary to set the io to
 
835
                         * NULL since it has already been freed by
 
836
                         * nih_io_closed(), but the io is not set to
 
837
                         * NULL. If we don't do this, when log_destroy()
 
838
                         * is called, it will attempt to dereference the 
 
839
                         * already freed log->io.
 
840
                         */
 
841
                        log->io = NULL;
 
842
                        nih_free (elem);
 
843
 
 
844
                        continue;
 
845
                }
 
846
 
803
847
                /* To be added to this list, log should have been
804
848
                 * detached from its parent job.
805
849
                 */
833
877
                        return -1;
834
878
 
835
879
                /* This will handle any remaining unflushed log data */
836
 
                nih_free (log);
 
880
                nih_free (elem);
837
881
        }
838
882
 
839
883
        log_flushed = 1;