~vorlon/ubuntu/raring/upstart/lp.1199778

« back to all changes in this revision

Viewing changes to init/tests/test_job_process.c

* init/job_process.c (job_process_terminated): On termination of
a job, update the utmp file replacing any existing entry for that
pid with a DEAD_PROCESS entry; likewise append an entry to wtmp.
* init/tests/test_job_process.c (test_utmp): Test utmp handling.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
#include <stdlib.h>
37
37
#include <string.h>
38
38
#include <unistd.h>
 
39
#include <utmp.h>
 
40
#include <utmpx.h>
39
41
 
40
42
#include <nih/macros.h>
41
43
#include <nih/string.h>
4501
4503
}
4502
4504
 
4503
4505
 
 
4506
void
 
4507
test_utmp (void)
 
4508
{
 
4509
        JobClass *      class;
 
4510
        Job *           job = NULL;
 
4511
        Blocked *       blocked = NULL;
 
4512
        Event *         event;
 
4513
        FILE *          output;
 
4514
        char            utmpname[PATH_MAX];
 
4515
        struct utmpx    utmp, *utmptr;
 
4516
        struct timeval  tv;
 
4517
 
 
4518
        TEST_FUNCTION ("job_process_handler");
 
4519
        program_name = "test";
 
4520
 
 
4521
        class = job_class_new (NULL, "test");
 
4522
        class->process[PROCESS_MAIN] = process_new (class);
 
4523
        class->process[PROCESS_MAIN]->command = "echo";
 
4524
 
 
4525
        class->start_on = event_operator_new (class, EVENT_MATCH,
 
4526
                                               "foo", NULL);
 
4527
        class->stop_on = event_operator_new (class, EVENT_MATCH,
 
4528
                                              "foo", NULL);
 
4529
        nih_hash_add (job_classes, &class->entry);
 
4530
 
 
4531
        event = event_new (NULL, "foo", NULL);
 
4532
 
 
4533
        TEST_FILENAME(utmpname);
 
4534
 
 
4535
        /* Check that utmp record for the running task of the job terminating
 
4536
         * is properly changed to DEAD_PROCESS
 
4537
         */
 
4538
        TEST_FEATURE ("with LOGIN_PROCESS utmp entry");
 
4539
        TEST_ALLOC_FAIL {
 
4540
                TEST_ALLOC_SAFE {
 
4541
                        job = job_new (class, "");
 
4542
 
 
4543
                        blocked = blocked_new (job, BLOCKED_EVENT, event);
 
4544
                        event_block (event);
 
4545
                        nih_list_add (&job->blocking, &blocked->entry);
 
4546
                }
 
4547
 
 
4548
                job->goal = JOB_START;
 
4549
                job->state = JOB_RUNNING;
 
4550
                job->pid[PROCESS_MAIN] = 1;
 
4551
 
 
4552
                TEST_FREE_TAG (blocked);
 
4553
 
 
4554
                job->blocker = NULL;
 
4555
                event->failed = FALSE;
 
4556
 
 
4557
                job->failed = FALSE;
 
4558
                job->failed_process = -1;
 
4559
                job->exit_status = 0;
 
4560
 
 
4561
                output = fopen (utmpname, "w");
 
4562
                fclose (output);
 
4563
 
 
4564
                /* set utmp file */
 
4565
                utmpxname(utmpname);
 
4566
 
 
4567
                /* set up utmp entries */
 
4568
                memset (&utmp, 0, sizeof utmp);
 
4569
 
 
4570
                strcpy(utmp.ut_id, "2");
 
4571
                utmp.ut_type = LOGIN_PROCESS;
 
4572
                utmp.ut_pid = 2;
 
4573
 
 
4574
                gettimeofday(&tv, NULL);
 
4575
                utmp.ut_tv.tv_sec = tv.tv_sec;
 
4576
                utmp.ut_tv.tv_usec = tv.tv_usec;
 
4577
 
 
4578
                setutxent();
 
4579
                pututxline(&utmp);
 
4580
 
 
4581
                strcpy(utmp.ut_id, "1");
 
4582
                utmp.ut_pid = 1;
 
4583
                pututxline(&utmp);
 
4584
 
 
4585
                endutxent();
 
4586
 
 
4587
                job_process_handler (NULL, 1, NIH_CHILD_EXITED, 0);
 
4588
 
 
4589
                setutxent();
 
4590
 
 
4591
                utmptr = getutxent();
 
4592
                TEST_NE_P(utmptr, NULL);
 
4593
                TEST_EQ(utmptr->ut_pid, 2);
 
4594
                TEST_EQ(utmptr->ut_type, LOGIN_PROCESS);
 
4595
 
 
4596
                utmptr = getutxent();
 
4597
                TEST_NE_P(utmptr, NULL);
 
4598
                TEST_EQ(utmptr->ut_pid, 1);
 
4599
                TEST_EQ(utmptr->ut_type, DEAD_PROCESS);
 
4600
 
 
4601
                nih_free (job);
 
4602
        }
 
4603
        TEST_FEATURE ("with USER_PROCESS utmp entry");
 
4604
        TEST_ALLOC_FAIL {
 
4605
                TEST_ALLOC_SAFE {
 
4606
                        job = job_new (class, "");
 
4607
 
 
4608
                        blocked = blocked_new (job, BLOCKED_EVENT, event);
 
4609
                        event_block (event);
 
4610
                        nih_list_add (&job->blocking, &blocked->entry);
 
4611
                }
 
4612
 
 
4613
                job->goal = JOB_START;
 
4614
                job->state = JOB_RUNNING;
 
4615
                job->pid[PROCESS_MAIN] = 1;
 
4616
 
 
4617
                TEST_FREE_TAG (blocked);
 
4618
 
 
4619
                job->blocker = NULL;
 
4620
                event->failed = FALSE;
 
4621
 
 
4622
                job->failed = FALSE;
 
4623
                job->failed_process = -1;
 
4624
                job->exit_status = 0;
 
4625
 
 
4626
                output = fopen (utmpname, "w");
 
4627
                fclose (output);
 
4628
 
 
4629
                /* set utmp file */
 
4630
                utmpxname(utmpname);
 
4631
 
 
4632
                /* set up utmp entries */
 
4633
                memset (&utmp, 0, sizeof utmp);
 
4634
 
 
4635
                strcpy(utmp.ut_id, "2");
 
4636
                utmp.ut_type = USER_PROCESS;
 
4637
                utmp.ut_pid = 2;
 
4638
 
 
4639
                gettimeofday(&tv, NULL);
 
4640
                utmp.ut_tv.tv_sec = tv.tv_sec;
 
4641
                utmp.ut_tv.tv_usec = tv.tv_usec;
 
4642
 
 
4643
                setutxent();
 
4644
                pututxline(&utmp);
 
4645
 
 
4646
                strcpy(utmp.ut_id, "1");
 
4647
                utmp.ut_pid = 1;
 
4648
                pututxline(&utmp);
 
4649
 
 
4650
                endutxent();
 
4651
 
 
4652
                job_process_handler (NULL, 1, NIH_CHILD_EXITED, 0);
 
4653
 
 
4654
                setutxent();
 
4655
 
 
4656
                utmptr = getutxent();
 
4657
                TEST_NE_P(utmptr, NULL);
 
4658
                TEST_EQ(utmptr->ut_pid, 2);
 
4659
                TEST_EQ(utmptr->ut_type, USER_PROCESS);
 
4660
 
 
4661
                utmptr = getutxent();
 
4662
                TEST_NE_P(utmptr, NULL);
 
4663
                TEST_EQ(utmptr->ut_pid, 1);
 
4664
                TEST_EQ(utmptr->ut_type, DEAD_PROCESS);
 
4665
 
 
4666
                nih_free (job);
 
4667
        }
 
4668
}
 
4669
 
 
4670
 
4504
4671
int
4505
4672
main (int   argc,
4506
4673
      char *argv[])
4532
4699
        test_spawn ();
4533
4700
        test_kill ();
4534
4701
        test_handler ();
 
4702
        test_utmp ();
4535
4703
 
4536
4704
        test_find ();
4537
4705