~jamesodhunt/upstart/async-spawn.20140310

« back to all changes in this revision

Viewing changes to init/job.c

  • Committer: James Hunt
  • Date: 2014-03-10 15:13:52 UTC
  • Revision ID: james.hunt@ubuntu.com-20140310151352-06mj0njwsgvi0fa0
temporary commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 *
3
3
 * job.c - core state machine of tasks and services
4
4
 *
5
 
 * Copyright © 2010,2011 Canonical Ltd.
 
5
 * Copyright  2010,2011 Canonical Ltd.
6
6
 * Author: Scott James Remnant <scott@netsplit.com>.
7
7
 *
8
8
 * This program is free software; you can redistribute it and/or modify
70
70
        __attribute__ ((warn_unused_result));
71
71
 
72
72
static const char *
73
 
job_state_enum_to_str (JobState state)
74
 
        __attribute__ ((warn_unused_result));
75
 
 
76
 
static JobState
77
 
job_state_str_to_enum (const char *state)
78
 
        __attribute__ ((warn_unused_result));
79
 
 
80
 
static const char *
81
73
job_trace_state_enum_to_str (TraceState state)
82
74
        __attribute__ ((warn_unused_result));
83
75
 
93
85
job_deserialise_kill_timer (json_object *json)
94
86
        __attribute__ ((warn_unused_result));
95
87
 
96
 
static void
97
 
job_error_handler (Job *job, JobState state, ProcessType process);
98
 
 
 
88
#if 1
 
89
int
 
90
job_destroy (Job *job)
 
91
{
 
92
        nih_assert (job);
 
93
 
 
94
        nih_message ("XXX:%s:%d: job: name=%s, state=%s, goal=%s",
 
95
                        __func__, __LINE__,
 
96
                        job_name (job), job_state_name (job->state), job_goal_name (job->goal));
 
97
 
 
98
        nih_list_destroy (&job->entry);
 
99
 
 
100
        return 0;
 
101
}
 
102
 
 
103
#endif
99
104
/**
100
105
 * job_new:
101
106
 * @class: class of job,
128
133
 
129
134
        nih_list_init (&job->entry);
130
135
 
131
 
        nih_alloc_set_destructor (job, nih_list_destroy);
 
136
        //nih_alloc_set_destructor (job, nih_list_destroy);
 
137
        nih_alloc_set_destructor (job, job_destroy);
132
138
 
133
139
        job->name = nih_strdup (job, name);
134
140
        if (! job->name)
210
216
                job_register (job, conn, TRUE);
211
217
        }
212
218
 
 
219
        job->handler_data = NULL;
 
220
 
 
221
        nih_message ("XXX:%s:%d: job: name=%s, state=%s, goal=%s",
 
222
                        __func__, __LINE__,
 
223
                        job_name (job), job_state_name (job->state), job_goal_name (job->goal));
 
224
 
213
225
        return job;
214
226
 
215
227
error:
277
289
{
278
290
        nih_assert (job != NULL);
279
291
 
 
292
/* FIXME */
 
293
#if 1
 
294
        nih_message ("XXX:%s:%d: goal=%s, job: %s, state=%s, goal=%s",
 
295
                        __func__, __LINE__,
 
296
                        job_goal_name (goal),
 
297
                        job_name (job), job_state_name (job->state), job_goal_name (goal));
 
298
#endif
 
299
 
280
300
        if (job->goal == goal)
281
301
                return;
282
302
 
345
365
{
346
366
        nih_assert (job != NULL);
347
367
 
 
368
/* FIXME */
 
369
#if 1
 
370
#include "event.h"
 
371
        nih_message ("XXX:%s:%d: state=%s, job: %s, goal=%s, state=%s",
 
372
                        __func__, __LINE__,
 
373
                        job_state_name (state),
 
374
                        job_name (job),
 
375
                        job_goal_name (job->goal),
 
376
                        job_state_name (job->state));
 
377
 
 
378
#endif
 
379
 
348
380
        while (job->state != state) {
349
 
                JobState old_state;
 
381
                //JobState old_state;
350
382
                int      unused;
351
383
 
 
384
                nih_assert (! job->blocker);
 
385
 
 
386
                /* Allow the event to be emitted before proceeding */
 
387
                if (job->blocker)
 
388
                        return;
 
389
 
 
390
                /* FIXME */
 
391
#if 1
 
392
        nih_message ("XXX:%s:%d: while: state=%s, job: %s, goal=%s, state=%s",
 
393
                        __func__, __LINE__,
 
394
                        job_state_name (state),
 
395
                        job_name (job),
 
396
                        job_goal_name (job->goal),
 
397
                        job_state_name (job->state));
 
398
#endif
 
399
                /* FIXME */
 
400
#if 1
 
401
                if (job->blocker) {
 
402
                        Event *event = (Event *)job->blocker;
 
403
 
 
404
                        nih_message ("XXX:%s:%d: job->blocker event=%s (env='%s', progress=%d)",
 
405
                                        __func__, __LINE__,
 
406
                                        event->name,
 
407
                                        event->env ? event->env[0] : "",
 
408
                                        event->progress);
 
409
                }
 
410
#endif
 
411
                /* FIXME: cannot assert this since there will be a
 
412
                 * blocked event.
 
413
                 */
352
414
                nih_assert (job->blocker == NULL);
353
415
 
 
416
                /* FIXME: only true if not calling job_process_start()!!! */
354
417
                nih_info (_("%s state changed from %s to %s"), job_name (job),
355
418
                          job_state_name (job->state), job_state_name (state));
356
419
 
357
 
                old_state = job->state;
358
 
                job->state = state;
359
 
 
 
420
                /* FIXME */
 
421
#if 1
 
422
        nih_message ("XXX:%s:%d: while: state=%s, job: %s, goal=%s, state=%s",
 
423
                        __func__, __LINE__,
 
424
                        job_state_name (state),
 
425
                        job_name (job),
 
426
                        job_goal_name (job->goal),
 
427
                        job_state_name (job->state));
 
428
#endif
 
429
 
 
430
 
 
431
                /* FIXME: should not change state until job known to
 
432
                 * have started successfully!
 
433
                 */
 
434
                //old_state = job->state;
 
435
                //job->state = state;
 
436
 
 
437
                /* FIXME: emitted too early !! */
 
438
#if 0
360
439
                NIH_LIST_FOREACH (control_conns, iter) {
361
440
                        NihListEntry   *entry = (NihListEntry *)iter;
362
441
                        DBusConnection *conn = (DBusConnection *)entry->data;
365
444
                                        conn, job->path,
366
445
                                        job_state_name (job->state)));
367
446
                }
 
447
#endif
368
448
 
369
449
                /* Perform whatever action is necessary to enter the new
370
450
                 * state, such as executing a process or emitting an event.
371
451
                 */
372
 
                switch (job->state) {
 
452
                switch (state) {
373
453
                case JOB_STARTING:
374
454
                        nih_assert (job->goal == JOB_START);
375
 
                        nih_assert ((old_state == JOB_WAITING)
376
 
                                    || (old_state == JOB_POST_STOP));
 
455
                        nih_assert ((job->state == JOB_WAITING)
 
456
                                    || (job->state == JOB_POST_STOP));
377
457
 
378
458
                        /* Throw away our old environment and use the newly
379
459
                         * set environment from now on; unless that's NULL
398
478
                        job->failed_process = PROCESS_INVALID;
399
479
                        job->exit_status = 0;
400
480
 
401
 
                        job->blocker = job_emit_event (job);
 
481
                /* FIXME */
 
482
#if 1
 
483
        nih_message ("XXX:%s:%d: while: state=%s, job: %s, goal=%s, state=%s: @@@@@ setting job->blocker",
 
484
                        __func__, __LINE__,
 
485
                        job_state_name (state),
 
486
                        job_name (job),
 
487
                        job_goal_name (job->goal),
 
488
                        job_state_name (job->state));
 
489
#endif
 
490
                        job->blocker = job_emit_event (job, state);
 
491
 
 
492
                        job->state = state;
402
493
 
403
494
                        break;
404
495
                case JOB_SECURITY:
405
496
                        nih_assert (job->goal == JOB_START);
406
 
                        nih_assert (old_state == JOB_STARTING);
 
497
                        nih_assert (job->state == JOB_STARTING);
407
498
 
408
499
                        if (job->class->process[PROCESS_SECURITY]
409
500
                            && apparmor_available()) {
410
 
                                job_process_start (job, state,
411
 
                                                PROCESS_SECURITY,
412
 
                                                job_error_handler);
 
501
                                job_process_start (job, state, PROCESS_SECURITY);
 
502
                                return;
413
503
                        } else {
 
504
                                job->state = state;
414
505
                                state = job_next_state (job);
415
506
                        }
416
507
 
417
508
                        break;
 
509
                case JOB_PRE_STARTING:
 
510
                        nih_assert (job->goal == JOB_START);
 
511
                        nih_assert (job->state == JOB_SECURITY);
 
512
                        job->state = state;
 
513
                        state = job_next_state (job);
 
514
                        break;
418
515
                case JOB_PRE_START:
419
516
                        nih_assert (job->goal == JOB_START);
420
 
                        nih_assert (old_state == JOB_SECURITY);
 
517
                        nih_assert (job->state == JOB_PRE_STARTING);
421
518
 
422
519
                        if (job->class->process[PROCESS_PRE_START]) {
423
 
                                job_process_start (job,state,
424
 
                                                PROCESS_PRE_START, job_error_handler);
 
520
                                job_process_start (job, state, PROCESS_PRE_START);
 
521
                                return;
425
522
                        } else {
 
523
                                job->state = state;
426
524
                                state = job_next_state (job);
427
525
                        }
428
526
 
429
527
                        break;
 
528
                case JOB_SPAWNING:
 
529
                        nih_assert (job->goal == JOB_START);
 
530
                        nih_assert (job->state == JOB_PRE_START);
 
531
 
 
532
                        job->state = state;
 
533
                        state = job_next_state (job);
 
534
                        break;
430
535
                case JOB_SPAWNED:
431
536
                        nih_assert (job->goal == JOB_START);
432
 
                        nih_assert (old_state == JOB_PRE_START);
 
537
                        nih_assert (job->state == JOB_SPAWNING);
433
538
 
434
539
                        if (job->class->process[PROCESS_MAIN]) {
435
 
                                job_process_start (job, state,
436
 
                                                PROCESS_MAIN, job_error_handler);
 
540
                                job_process_start (job, state, PROCESS_MAIN);
437
541
                                if (job->class->expect == EXPECT_NONE) {
 
542
                                        job->state = state;
438
543
                                        state = job_next_state (job);
 
544
                                } else {
 
545
                                        return;
439
546
                                }
440
547
                        } else {
 
548
                                job->state = state;
441
549
                                state = job_next_state (job);
442
550
                        }
443
551
 
444
552
                        break;
 
553
                case JOB_POST_STARTING:
 
554
                        nih_assert (job->goal == JOB_START);
 
555
                        nih_assert (job->state == JOB_SPAWNED);
 
556
                        job->state = state;
 
557
                        state = job_next_state (job);
 
558
                        break;
445
559
                case JOB_POST_START:
446
560
                        nih_assert (job->goal == JOB_START);
447
 
                        nih_assert (old_state == JOB_SPAWNED);
 
561
                        nih_assert (job->state == JOB_POST_STARTING);
448
562
 
449
563
                        if (job->class->process[PROCESS_POST_START]) {
450
 
                                job_process_start (job, state,
451
 
                                                PROCESS_POST_START, job_error_handler);
 
564
                                job_process_start (job, state, PROCESS_POST_START);
 
565
                                return;
452
566
                        } else {
 
567
                                job->state = state;
453
568
                                state = job_next_state (job);
454
569
                        }
455
570
 
456
571
                        break;
457
572
                case JOB_RUNNING:
458
573
                        nih_assert (job->goal == JOB_START);
459
 
                        nih_assert ((old_state == JOB_POST_START)
460
 
                                    || (old_state == JOB_PRE_STOP));
 
574
                        nih_assert ((job->state == JOB_POST_START)
 
575
                                    || (job->state == JOB_PRE_STOP));
461
576
 
462
 
                        if (old_state == JOB_PRE_STOP) {
 
577
                        if (job->state == JOB_PRE_STOP) {
463
578
                                /* Throw away the stop environment */
464
579
                                if (job->stop_env) {
465
580
                                        nih_unref (job->stop_env, job);
469
584
                                /* Cancel the stop attempt */
470
585
                                job_finished (job, FALSE);
471
586
                        } else {
472
 
                                job_emit_event (job);
 
587
                /* FIXME */
 
588
#if 1
 
589
        nih_message ("XXX:%s:%d: while: state=%s, job: %s, goal=%s, state=%s: @@@@@ setting job->blocker",
 
590
                        __func__, __LINE__,
 
591
                        job_state_name (state),
 
592
                        job_name (job),
 
593
                        job_goal_name (job->goal),
 
594
                        job_state_name (job->state));
 
595
#endif
 
596
                                job_emit_event (job, state);
473
597
 
474
598
                                /* If we're not a task, our goal is to be
475
599
                                 * running.
478
602
                                        job_finished (job, FALSE);
479
603
                        }
480
604
 
 
605
                        job->state = state;
 
606
 
 
607
                        break;
 
608
                case JOB_PRE_STOPPING:
 
609
                        nih_assert (job->goal == JOB_STOP);
 
610
                        nih_assert (job->state == JOB_RUNNING);
 
611
                        job->state = state;
 
612
                        state = job_next_state (job);
481
613
                        break;
482
614
                case JOB_PRE_STOP:
483
615
                        nih_assert (job->goal == JOB_STOP);
484
 
                        nih_assert (old_state == JOB_RUNNING);
 
616
                        nih_assert (job->state == JOB_PRE_STOPPING);
485
617
 
486
618
                        if (job->class->process[PROCESS_PRE_STOP]) {
487
 
                                job_process_start (job, state,
488
 
                                                PROCESS_PRE_STOP, job_error_handler);
 
619
                                job_process_start (job, state, PROCESS_PRE_STOP);
 
620
                                return;
489
621
                        } else {
 
622
                                job->state = state;
490
623
                                state = job_next_state (job);
491
624
                        }
492
625
 
493
626
                        break;
494
627
                case JOB_STOPPING:
495
 
                        nih_assert ((old_state == JOB_STARTING)
496
 
                                    || (old_state == JOB_PRE_START)
497
 
                                    || (old_state == JOB_SECURITY)
498
 
                                    || (old_state == JOB_SPAWNED)
499
 
                                    || (old_state == JOB_POST_START)
500
 
                                    || (old_state == JOB_RUNNING)
501
 
                                    || (old_state == JOB_PRE_STOP));
502
 
 
503
 
                        job->blocker = job_emit_event (job);
 
628
                        nih_assert ((job->state == JOB_STARTING)
 
629
                                    || (job->state == JOB_PRE_START)
 
630
                                    || (job->state == JOB_SECURITY)
 
631
                                    || (job->state == JOB_SPAWNED)
 
632
                                    || (job->state == JOB_POST_START)
 
633
                                    || (job->state == JOB_RUNNING)
 
634
                                    || (job->state == JOB_PRE_STOP));
 
635
 
 
636
                /* FIXME */
 
637
#if 1
 
638
        nih_message ("XXX:%s:%d: while: state=%s, job: %s, goal=%s, state=%s: @@@@@ setting job->blocker",
 
639
                        __func__, __LINE__,
 
640
                        job_state_name (state),
 
641
                        job_name (job),
 
642
                        job_goal_name (job->goal),
 
643
                        job_state_name (job->state));
 
644
#endif
 
645
                        job->blocker = job_emit_event (job, state);
 
646
 
 
647
                        job->state = state;
504
648
 
505
649
                        break;
506
650
                case JOB_KILLED:
507
 
                        nih_assert (old_state == JOB_STOPPING);
 
651
                        nih_assert (job->state == JOB_STOPPING);
508
652
 
509
653
                        if (job->class->process[PROCESS_MAIN]
510
654
                            && (job->pid[PROCESS_MAIN] > 0)) {
511
655
                                job_process_kill (job, PROCESS_MAIN);
512
656
                        } else {
 
657
                                job->state = state;
513
658
                                state = job_next_state (job);
514
659
                        }
515
660
 
516
661
                        break;
 
662
                case JOB_POST_STOPPING:
 
663
                        nih_assert (job->state == JOB_KILLED);
 
664
                        job->state = state;
 
665
                        state = job_next_state (job);
 
666
                        break;
517
667
                case JOB_POST_STOP:
518
 
                        nih_assert (old_state == JOB_KILLED);
 
668
                        nih_assert (job->state == JOB_POST_STOPPING);
519
669
 
520
670
                        if (job->class->process[PROCESS_POST_STOP]) {
521
 
                                job_process_start (job, state,
522
 
                                                PROCESS_POST_STOP, job_error_handler);
 
671
                                job_process_start (job, state, PROCESS_POST_STOP);
 
672
                                return;
523
673
                        } else {
 
674
                                job->state = state;
524
675
                                state = job_next_state (job);
525
676
                        }
526
677
 
527
678
                        break;
528
679
                case JOB_WAITING:
529
680
                        nih_assert (job->goal == JOB_STOP);
530
 
                        nih_assert ((old_state == JOB_POST_STOP)
531
 
                                    || (old_state == JOB_STARTING));
 
681
                        nih_assert ((job->state == JOB_POST_STOP)
 
682
                                    || (job->state == JOB_STARTING));
532
683
 
533
 
                        job_emit_event (job);
 
684
                /* FIXME */
 
685
#if 1
 
686
        nih_message ("XXX:%s:%d: while: state=%s, job: %s, goal=%s, state=%s: @@@@@ setting job->blocker",
 
687
                        __func__, __LINE__,
 
688
                        job_state_name (state),
 
689
                        job_name (job),
 
690
                        job_goal_name (job->goal),
 
691
                        job_state_name (job->state));
 
692
#endif
 
693
                        job_emit_event (job, state);
534
694
 
535
695
                        job_finished (job, FALSE);
536
696
 
568
728
                                nih_free (job);
569
729
                        }
570
730
 
 
731
                        /* FIXME */
 
732
#if 1
 
733
                        nih_message ("XXX:%s:%d: LEAVING WHILE: state=%s, job: %s, goal=%s, state=%s: @@@@@ setting job->blocker",
 
734
                        __func__, __LINE__,
 
735
                        job_state_name (state),
 
736
                        job_name (job),
 
737
                        job_goal_name (job->goal),
 
738
                        job_state_name (job->state));
 
739
#endif
571
740
                        return;
572
741
                }
 
742
 
 
743
#if 0
 
744
                /* Set state for those transitions that do not invoke
 
745
                 * job_process_start().
 
746
                 */
 
747
                job->state = state;
 
748
#endif
573
749
        }
574
750
}
575
751
 
594
770
JobState
595
771
job_next_state (Job *job)
596
772
{
 
773
 
 
774
/* FIXME */
 
775
#if 1
 
776
        nih_message ("XXX:%s:%d: job: %s, state=%s, goal=%s",
 
777
                        __func__, __LINE__,
 
778
                        job_name (job), job_state_name (job->state), job_goal_name (job->goal));
 
779
#endif
597
780
        nih_assert (job != NULL);
598
781
 
599
782
        switch (job->state) {
620
803
                case JOB_STOP:
621
804
                        return JOB_STOPPING;
622
805
                case JOB_START:
 
806
                        return JOB_PRE_STARTING;
 
807
                default:
 
808
                        nih_assert_not_reached ();
 
809
                }
 
810
        case JOB_PRE_STARTING:
 
811
                switch (job->goal) {
 
812
                case JOB_STOP:
 
813
                        return JOB_STOPPING;
 
814
                case JOB_START:
623
815
                        return JOB_PRE_START;
624
816
                default:
625
817
                        nih_assert_not_reached ();
626
818
                }
 
819
                break;
627
820
        case JOB_PRE_START:
628
821
                switch (job->goal) {
629
822
                case JOB_STOP:
630
823
                        return JOB_STOPPING;
631
824
                case JOB_START:
 
825
                        return JOB_SPAWNING;
 
826
                default:
 
827
                        nih_assert_not_reached ();
 
828
                }
 
829
        case JOB_SPAWNING:
 
830
                switch (job->goal) {
 
831
                case JOB_STOP:
 
832
                        return JOB_STOPPING;
 
833
                case JOB_START:
632
834
                        return JOB_SPAWNED;
633
835
                default:
634
836
                        nih_assert_not_reached ();
635
837
                }
 
838
                break;
636
839
        case JOB_SPAWNED:
637
840
                switch (job->goal) {
638
841
                case JOB_STOP:
639
842
                        return JOB_STOPPING;
640
843
                case JOB_START:
 
844
                        return JOB_POST_STARTING;
 
845
                default:
 
846
                        nih_assert_not_reached ();
 
847
                }
 
848
        case JOB_POST_STARTING:
 
849
                switch (job->goal) {
 
850
                case JOB_STOP:
 
851
                        return JOB_STOPPING;
 
852
                case JOB_START:
641
853
                        return JOB_POST_START;
642
854
                default:
643
855
                        nih_assert_not_reached ();
644
856
                }
 
857
                break;
645
858
        case JOB_POST_START:
646
859
                switch (job->goal) {
647
860
                case JOB_STOP:
668
881
                default:
669
882
                        nih_assert_not_reached ();
670
883
                }
 
884
        case JOB_PRE_STOPPING:
 
885
                switch (job->goal) {
 
886
                case JOB_STOP:
 
887
                        return JOB_STOPPING;
 
888
                case JOB_START:
 
889
                        return JOB_PRE_STOP;
 
890
                default:
 
891
                        nih_assert_not_reached ();
 
892
                }
 
893
                break;
671
894
        case JOB_PRE_STOP:
672
895
                switch (job->goal) {
673
896
                case JOB_STOP:
692
915
        case JOB_KILLED:
693
916
                switch (job->goal) {
694
917
                case JOB_STOP:
695
 
                        return JOB_POST_STOP;
696
 
                case JOB_START:
697
 
                        return JOB_POST_STOP;
698
 
                default:
699
 
                        nih_assert_not_reached ();
700
 
                }
 
918
                        return JOB_POST_STOPPING;
 
919
                case JOB_START:
 
920
                        return JOB_POST_STOPPING;
 
921
                default:
 
922
                        nih_assert_not_reached ();
 
923
                }
 
924
        case JOB_POST_STOPPING:
 
925
                switch (job->goal) {
 
926
                case JOB_STOP:
 
927
                        return JOB_POST_STOP;
 
928
                case JOB_START:
 
929
                        return JOB_POST_STOP;
 
930
                default:
 
931
                        nih_assert_not_reached ();
 
932
                }
 
933
                break;
701
934
        case JOB_POST_STOP:
702
935
                switch (job->goal) {
703
936
                case JOB_STOP:
865
1098
 
866
1099
/**
867
1100
 * job_emit_event:
868
 
 * @job: job generating the event.
 
1101
 * @job: job generating the event,
 
1102
 * @state: state job is moving to.
869
1103
 *
870
1104
 * Called from a state change because it believes an event should be
871
1105
 * emitted.  Constructs the event with the right arguments and environment
883
1117
 * Returns: new Event in the queue.
884
1118
 **/
885
1119
Event *
886
 
job_emit_event (Job *job)
 
1120
job_emit_event (Job *job, JobState state)
887
1121
{
888
1122
        Event           *event;
889
1123
        const char      *name;
894
1128
 
895
1129
        nih_assert (job != NULL);
896
1130
 
897
 
        switch (job->state) {
 
1131
        switch (state) {
898
1132
        case JOB_STARTING:
899
1133
                name = JOB_STARTING_EVENT;
900
1134
                block = TRUE;
1096
1330
                return N_("starting");
1097
1331
        case JOB_SECURITY:
1098
1332
                return N_("security");
 
1333
        case JOB_PRE_STARTING:
 
1334
                return N_("pre-starting");
1099
1335
        case JOB_PRE_START:
1100
1336
                return N_("pre-start");
 
1337
        case JOB_SPAWNING:
 
1338
                return N_("spawning");
1101
1339
        case JOB_SPAWNED:
1102
1340
                return N_("spawned");
 
1341
        case JOB_POST_STARTING:
 
1342
                return N_("post-starting");
1103
1343
        case JOB_POST_START:
1104
1344
                return N_("post-start");
1105
1345
        case JOB_RUNNING:
1106
1346
                return N_("running");
 
1347
        case JOB_PRE_STOPPING:
 
1348
                return N_("pre-stopping");
1107
1349
        case JOB_PRE_STOP:
1108
1350
                return N_("pre-stop");
1109
1351
        case JOB_STOPPING:
1110
1352
                return N_("stopping");
1111
1353
        case JOB_KILLED:
1112
1354
                return N_("killed");
 
1355
        case JOB_POST_STOPPING:
 
1356
                return N_("post-stopping");
1113
1357
        case JOB_POST_STOP:
1114
1358
                return N_("post-stop");
1115
1359
        default:
1136
1380
                return JOB_STARTING;
1137
1381
        } else if (! strcmp (state, "security")) {
1138
1382
                return JOB_SECURITY;
 
1383
        } else if (! strcmp (state, "pre-starting")) {
 
1384
                return JOB_PRE_STARTING;
1139
1385
        } else if (! strcmp (state, "pre-start")) {
1140
1386
                return JOB_PRE_START;
 
1387
        } else if (! strcmp (state, "spawning")) {
 
1388
                return JOB_SPAWNING;
1141
1389
        } else if (! strcmp (state, "spawned")) {
1142
1390
                return JOB_SPAWNED;
 
1391
        } else if (! strcmp (state, "post-starting")) {
 
1392
                return JOB_POST_STARTING;
1143
1393
        } else if (! strcmp (state, "post-start")) {
1144
1394
                return JOB_POST_START;
1145
1395
        } else if (! strcmp (state, "running")) {
1146
1396
                return JOB_RUNNING;
 
1397
        } else if (! strcmp (state, "pre-stopping")) {
 
1398
                return JOB_PRE_STOPPING;
1147
1399
        } else if (! strcmp (state, "pre-stop")) {
1148
1400
                return JOB_PRE_STOP;
1149
1401
        } else if (! strcmp (state, "stopping")) {
1150
1402
                return JOB_STOPPING;
1151
1403
        } else if (! strcmp (state, "killed")) {
1152
1404
                return JOB_KILLED;
 
1405
        } else if (! strcmp (state, "post-stopping")) {
 
1406
                return JOB_POST_STOPPING;
1153
1407
        } else if (! strcmp (state, "post-stop")) {
1154
1408
                return JOB_POST_STOP;
1155
1409
        } else {
1620
1874
        json_object      *json_pid;
1621
1875
        json_object      *json_fds;
1622
1876
        json_object      *json_logs;
 
1877
        json_object      *json_handler_data;
1623
1878
 
1624
1879
        nih_assert (job);
1625
1880
 
1765
2020
 
1766
2021
        json_object_object_add (json, "log", json_logs);
1767
2022
 
 
2023
        nih_debug ("XXX:%s:%d:", __func__, __LINE__);
 
2024
        json_handler_data = job_process_data_serialise (job, job->handler_data);
 
2025
        if (! json_handler_data)
 
2026
                goto error;
 
2027
        nih_debug ("XXX:%s:%d:", __func__, __LINE__);
 
2028
 
 
2029
        json_object_object_add (json, "handler_data", json_handler_data);
 
2030
 
 
2031
        nih_debug ("XXX:%s:%d:", __func__, __LINE__);
1768
2032
        return json;
1769
2033
 
1770
2034
error:
2064
2328
                }
2065
2329
        }
2066
2330
 
 
2331
        if (json_object_object_get (json, "process_data")) {
 
2332
                JobProcessData *handler_data;
 
2333
 
 
2334
                handler_data = job_process_data_deserialise (job, job, json);
 
2335
                if (! handler_data)
 
2336
                        goto error;
 
2337
                
 
2338
                /* Recreate watch */
 
2339
                job_register_child_handler (job, handler_data->job_process_fd, handler_data);
 
2340
        }
 
2341
 
2067
2342
        return job;
2068
2343
 
2069
2344
error:
2167
2442
 *
2168
2443
 * Returns: string representation of @state, or NULL if not known.
2169
2444
 **/
2170
 
static const char *
 
2445
const char *
2171
2446
job_state_enum_to_str (JobState state)
2172
2447
{
2173
2448
        state_enum_to_str (JOB_WAITING, state);
2174
2449
        state_enum_to_str (JOB_STARTING, state);
2175
2450
        state_enum_to_str (JOB_SECURITY, state);
 
2451
        state_enum_to_str (JOB_PRE_STARTING, state);
2176
2452
        state_enum_to_str (JOB_PRE_START, state);
 
2453
        state_enum_to_str (JOB_SPAWNING, state);
2177
2454
        state_enum_to_str (JOB_SPAWNED, state);
 
2455
        state_enum_to_str (JOB_POST_STARTING, state);
2178
2456
        state_enum_to_str (JOB_POST_START, state);
2179
2457
        state_enum_to_str (JOB_RUNNING, state);
 
2458
        state_enum_to_str (JOB_PRE_STOPPING, state);
2180
2459
        state_enum_to_str (JOB_PRE_STOP, state);
2181
2460
        state_enum_to_str (JOB_STOPPING, state);
2182
2461
        state_enum_to_str (JOB_KILLED, state);
 
2462
        state_enum_to_str (JOB_POST_STOPPING, state);
2183
2463
        state_enum_to_str (JOB_POST_STOP, state);
2184
2464
 
2185
2465
        return NULL;
2194
2474
 *
2195
2475
 * Returns: JobState representation of @state, or -1 if not known.
2196
2476
 **/
2197
 
static JobState
 
2477
JobState
2198
2478
job_state_str_to_enum (const char *state)
2199
2479
{
2200
2480
        state_str_to_enum (JOB_WAITING, state);
2201
2481
        state_str_to_enum (JOB_STARTING, state);
2202
2482
        state_str_to_enum (JOB_SECURITY, state);
 
2483
        state_str_to_enum (JOB_PRE_STARTING, state);
2203
2484
        state_str_to_enum (JOB_PRE_START, state);
 
2485
        state_str_to_enum (JOB_SPAWNING, state);
2204
2486
        state_str_to_enum (JOB_SPAWNED, state);
 
2487
        state_str_to_enum (JOB_POST_STARTING, state);
2205
2488
        state_str_to_enum (JOB_POST_START, state);
2206
2489
        state_str_to_enum (JOB_RUNNING, state);
 
2490
        state_str_to_enum (JOB_PRE_STOPPING, state);
2207
2491
        state_str_to_enum (JOB_PRE_STOP, state);
2208
2492
        state_str_to_enum (JOB_STOPPING, state);
2209
2493
        state_str_to_enum (JOB_KILLED, state);
 
2494
        state_str_to_enum (JOB_POST_STOPPING, state);
2210
2495
        state_str_to_enum (JOB_POST_STOP, state);
2211
2496
 
2212
2497
        return -1;
2361
2646
}
2362
2647
 
2363
2648
/**
2364
 
 * job_error_handler:
 
2649
 * job_child_error_handler:
2365
2650
 *
2366
2651
 * @job: job,
2367
 
 * @state: state job was trying to move to,
2368
2652
 * @process: process that failed to start.
2369
2653
 *
2370
2654
 * JobProcessErrorHandler that deals with errors resulting from
2371
2655
 * a failure to start a job process.
2372
2656
 **/
2373
 
static void
2374
 
job_error_handler (Job *job, JobState state, ProcessType process)
 
2657
void
 
2658
job_child_error_handler (Job *job, ProcessType process)
2375
2659
{
2376
2660
        nih_assert (job);
2377
2661
        nih_assert (process > PROCESS_INVALID);
2378
2662
        nih_assert (process < PROCESS_LAST);
2379
2663
        
 
2664
        /* FIXME */
 
2665
#if 1
 
2666
        nih_message ("XXX:%s:%d: job: %s, state=%s, goal=%s, process=%s",
 
2667
                        __func__, __LINE__,
 
2668
                        job_name (job),
 
2669
                        job_state_name (job->state),
 
2670
                        job_goal_name (job->goal),
 
2671
                        process_name (process));
 
2672
#endif
 
2673
 
2380
2674
        switch (process) {
2381
2675
        case PROCESS_SECURITY:
2382
2676
                job_failed (job, PROCESS_SECURITY, -1);