~jamesodhunt/ubuntu/vivid/upstart/bug-1447756

« back to all changes in this revision

Viewing changes to init/tests/test_job_process.c

  • Committer: James Hunt
  • Date: 2011-12-14 14:09:46 UTC
  • mfrom: (1185.1.14 upstream)
  • Revision ID: james.hunt@ubuntu.com-20111214140946-omh0ikuf6utjdm1o
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
#include <unistd.h>
39
39
#include <utmp.h>
40
40
#include <utmpx.h>
 
41
#include <dirent.h>
 
42
#include <sys/time.h>
 
43
#include <sys/resource.h>
 
44
#include <pwd.h>
 
45
#include <grp.h>
41
46
 
42
47
#include <nih/macros.h>
43
48
#include <nih/string.h>
54
59
#include "errors.h"
55
60
 
56
61
 
 
62
#define EXPECTED_JOB_LOGDIR       "/var/log/upstart"
 
63
#define TEST_SHELL                "/bin/sh"
 
64
#define TEST_SHELL_ARG            "-e"
 
65
 
 
66
/* Used to generate single- and multi-line output. */
 
67
#define TEST_CMD_ECHO             "/bin/echo"
 
68
 
 
69
/* Used to generate multi-line output out stdout without using shell
 
70
 * meta-characters.
 
71
 */
 
72
#define TEST_CMD_YES              "/usr/bin/yes"
 
73
 
 
74
/* Used to generate multi-line output on stderr without using shell
 
75
 * meta-characters.
 
76
 */
 
77
#define TEST_CMD_DD               "/bin/dd"
 
78
 
 
79
/* Force an inotify watch update */
 
80
#define TEST_FORCE_WATCH_UPDATE()                                    \
 
81
{                                                                    \
 
82
        int         nfds = 0;                                        \
 
83
        int         ret = 0;                                         \
 
84
        fd_set      readfds, writefds, exceptfds;                    \
 
85
                                                                     \
 
86
        FD_ZERO (&readfds);                                          \
 
87
        FD_ZERO (&writefds);                                         \
 
88
        FD_ZERO (&exceptfds);                                        \
 
89
                                                                     \
 
90
        nih_io_select_fds (&nfds, &readfds, &writefds, &exceptfds);  \
 
91
        ret = select (nfds, &readfds, &writefds, &exceptfds, NULL);  \
 
92
        if (ret > 0)                                                 \
 
93
                nih_io_handle_fds (&readfds, &writefds, &exceptfds); \
 
94
}
 
95
 
 
96
/* Force an inotify watch update (allowing a struct timeval
 
97
 * timeout to be specified
 
98
 */
 
99
#define TEST_FORCE_WATCH_UPDATE_TIMEOUT(t)                           \
 
100
{                                                                    \
 
101
        int         nfds = 0;                                        \
 
102
        int         ret = 0;                                         \
 
103
        fd_set      readfds, writefds, exceptfds;                    \
 
104
                                                                     \
 
105
        FD_ZERO (&readfds);                                          \
 
106
        FD_ZERO (&writefds);                                         \
 
107
        FD_ZERO (&exceptfds);                                        \
 
108
                                                                     \
 
109
        nih_io_select_fds (&nfds, &readfds, &writefds, &exceptfds);  \
 
110
        ret = select (nfds, &readfds, &writefds, &exceptfds, &t);    \
 
111
        if (ret > 0)                                                 \
 
112
                nih_io_handle_fds (&readfds, &writefds, &exceptfds); \
 
113
}
 
114
 
 
115
#define ENSURE_DIRECTORY_EMPTY(path)                                 \
 
116
{                                                                    \
 
117
        DIR            *dp = NULL;                                   \
 
118
        struct dirent  *file = NULL;                                 \
 
119
        int             count = 0;                                   \
 
120
                                                                     \
 
121
        dp = opendir (path);                                         \
 
122
        TEST_NE_P (dp, NULL);                                        \
 
123
                                                                     \
 
124
        while((file = readdir (dp))) {                               \
 
125
                if (!strcmp (".", file->d_name) ||                   \
 
126
                                !strcmp ("..", file->d_name))        \
 
127
                        continue;                                    \
 
128
                count++;                                             \
 
129
        }                                                            \
 
130
                                                                     \
 
131
        closedir (dp);                                               \
 
132
                                                                     \
 
133
        TEST_EQ (count, 0);                                          \
 
134
}
 
135
 
57
136
/* Sadly we can't test everything that job_process_spawn() does simply because
58
137
 * a lot of it can only be done by root, or in the case of the console stuff,
59
138
 * kills whatever had /dev/console (usually X).
65
144
        TEST_PIDS,
66
145
        TEST_CONSOLE,
67
146
        TEST_PWD,
68
 
        TEST_ENVIRONMENT
 
147
        TEST_ENVIRONMENT,
 
148
        TEST_OUTPUT,
 
149
        TEST_SIGNALS
69
150
};
70
151
 
71
152
static char *argv0;
72
153
 
 
154
static int get_available_pty_count (void) __attribute__((unused));
 
155
 
73
156
static void
74
157
child (enum child_tests  test,
75
158
       const char       *filename)
76
159
{
77
 
        FILE *out;
78
 
        char  tmpname[PATH_MAX], path[PATH_MAX];
79
 
        int   i;
 
160
        FILE  *out;
 
161
        FILE  *in;
 
162
        char   tmpname[PATH_MAX], path[PATH_MAX];
 
163
        int    i;
 
164
        char   buffer[1024];
 
165
 
80
166
 
81
167
        strcpy (tmpname, filename);
82
168
        strcat (tmpname, ".tmp");
110
196
                for (char **env = environ; *env; env++)
111
197
                        fprintf (out, "%s\n", *env);
112
198
                break;
 
199
        case TEST_OUTPUT:
 
200
                /* Write to stdout and stderr.
 
201
                 *
 
202
                 * Of course, daemon's usually make a point of not writing to
 
203
                 * stdout/stderr...
 
204
                 */
 
205
                fprintf(stdout, "stdout\n");
 
206
                fprintf(stderr, "stderr\n");
 
207
                break;
 
208
        case TEST_SIGNALS:
 
209
                /* Write signal stats for child process to stdout */
 
210
                in = fopen("/proc/self/status", "r");
 
211
                if (! in) {
 
212
                        abort();
 
213
                }
 
214
 
 
215
                while (fgets (buffer, sizeof (buffer), in) != NULL) {
 
216
                        if (strstr (buffer, "SigBlk:") == buffer ||
 
217
                                        strstr (buffer, "SigIgn:") == buffer)
 
218
                                fputs (buffer, out);
 
219
                }
 
220
 
 
221
                fclose(in);
 
222
                break;
113
223
        }
114
224
 
115
225
        fsync (fileno (out));
120
230
        exit (0);
121
231
}
122
232
 
123
 
 
 
233
/* FIXME:
 
234
 *
 
235
 * This is not currently reliable due to a kernel bug that does
 
236
 * not bound 'nr' to the range:
 
237
 *
 
238
 *      0 <= nr <= 'max'
 
239
 */
 
240
 
 
241
/**
 
242
 * get_available_pty_count:
 
243
 *
 
244
 * Return count of available ptys.
 
245
 *
 
246
 **/
 
247
static int
 
248
get_available_pty_count (void)
 
249
{
 
250
        FILE *f;
 
251
        int nr, max;
 
252
        int ret;
 
253
 
 
254
        f = fopen ("/proc/sys/kernel/pty/max", "r");
 
255
        TEST_NE_P (f, NULL);
 
256
 
 
257
        ret = fscanf (f, "%d", &max);
 
258
        TEST_EQ (ret, 1);
 
259
        TEST_GT (max, 0);
 
260
 
 
261
        fclose (f);
 
262
 
 
263
        f = fopen ("/proc/sys/kernel/pty/nr", "r");
 
264
        TEST_NE_P (f, NULL);
 
265
 
 
266
        ret = fscanf (f, "%d", &nr);
 
267
        TEST_EQ (ret, 1);
 
268
        TEST_GE (nr, 0);
 
269
 
 
270
        fclose (f);
 
271
 
 
272
        return max - nr;
 
273
}
 
274
 
 
275
/* XXX: Note that none of these tests attempts to test with a Session
 
276
 * passed to job_class_new() since to do so would modify the home
 
277
 * directory of the user running these tests (BAD!!).
 
278
 *
 
279
 * (Such tests are handled in the bundled test_user_sessions.sh script).
 
280
 */
124
281
void
125
282
test_run (void)
126
283
{
127
 
        JobClass   *class = NULL;
128
 
        Job         *job = NULL;
129
 
        FILE        *output;
130
 
        struct stat  statbuf;
131
 
        char         filename[PATH_MAX], buf[80];
132
 
        int          ret = -1, status, first;
133
 
        siginfo_t    info;
 
284
        char             dirname[PATH_MAX];
 
285
        JobClass        *class = NULL;
 
286
        Job             *job = NULL;
 
287
        FILE            *output;
 
288
        struct stat      statbuf;
 
289
        char             filename[PATH_MAX], buf[80];
 
290
        int              ret = -1, status, first;
 
291
        siginfo_t        info;
 
292
        char             filebuf[1024];
 
293
        struct passwd   *pwd;
 
294
        struct group    *grp;
 
295
        char            *p;
 
296
        int              ok;
 
297
 
134
298
 
135
299
        TEST_FUNCTION ("job_process_run");
136
 
        job_class_init ();
137
 
        nih_error_init ();
138
 
        nih_io_init ();
139
300
 
140
301
        TEST_FILENAME (filename);
141
302
        program_name = "test";
148
309
        TEST_ALLOC_FAIL {
149
310
                TEST_ALLOC_SAFE {
150
311
                        class = job_class_new (NULL, "test", NULL);
 
312
                        class->console = CONSOLE_NONE;
151
313
                        class->process[PROCESS_MAIN] = process_new (class);
152
314
                        class->process[PROCESS_MAIN]->command = nih_sprintf (
153
315
                                class->process[PROCESS_MAIN],
180
342
        TEST_ALLOC_FAIL {
181
343
                TEST_ALLOC_SAFE {
182
344
                        class = job_class_new (NULL, "test", NULL);
 
345
                        class->console = CONSOLE_NONE;
183
346
                        class->process[PROCESS_MAIN] = process_new (class);
184
347
                        class->process[PROCESS_MAIN]->command = nih_sprintf (
185
348
                                class->process[PROCESS_MAIN],
217
380
        TEST_ALLOC_FAIL {
218
381
                TEST_ALLOC_SAFE {
219
382
                        class = job_class_new (NULL, "test", NULL);
 
383
                        class->console = CONSOLE_NONE;
220
384
                        class->process[PROCESS_MAIN] = process_new (class);
221
385
                        class->process[PROCESS_MAIN]->script = TRUE;
222
386
                        class->process[PROCESS_MAIN]->command = nih_sprintf (
254
418
        TEST_ALLOC_FAIL {
255
419
                TEST_ALLOC_SAFE {
256
420
                        class = job_class_new (NULL, "test", NULL);
 
421
                        class->console = CONSOLE_NONE;
257
422
                        class->process[PROCESS_MAIN] = process_new (class);
258
423
                        class->process[PROCESS_MAIN]->script = TRUE;
259
424
                        class->process[PROCESS_MAIN]->command = nih_sprintf (
291
456
        TEST_ALLOC_FAIL {
292
457
                TEST_ALLOC_SAFE {
293
458
                        class = job_class_new (NULL, "test", NULL);
 
459
                        class->console = CONSOLE_NONE;
294
460
                        class->process[PROCESS_MAIN] = process_new (class);
295
461
                        class->process[PROCESS_MAIN]->script = TRUE;
296
462
                        class->process[PROCESS_MAIN]->command = nih_sprintf (
328
494
        TEST_ALLOC_FAIL {
329
495
                TEST_ALLOC_SAFE {
330
496
                        class = job_class_new (NULL, "test", NULL);
 
497
                        class->console = CONSOLE_NONE;
331
498
                        class->process[PROCESS_MAIN] = process_new (class);
332
499
                        class->process[PROCESS_MAIN]->command = nih_sprintf (
333
500
                                class->process[PROCESS_MAIN],
362
529
                TEST_FILE_EQ (output, "BAR=BAZ\n");
363
530
                TEST_FILE_EQ (output, "UPSTART_JOB=test\n");
364
531
                TEST_FILE_EQ (output, "UPSTART_INSTANCE=\n");
 
532
                TEST_FILE_EQ (output, "UPSTART_NO_SESSIONS=1\n");
365
533
                TEST_FILE_END (output);
366
534
                fclose (output);
367
535
                unlink (filename);
377
545
        TEST_ALLOC_FAIL {
378
546
                TEST_ALLOC_SAFE {
379
547
                        class = job_class_new (NULL, "test", NULL);
 
548
                        class->console = CONSOLE_NONE;
380
549
                        class->process[PROCESS_MAIN] = process_new (class);
381
550
                        class->process[PROCESS_MAIN]->command = nih_sprintf (
382
551
                                class->process[PROCESS_MAIN],
411
580
                TEST_FILE_EQ (output, "BAR=BAZ\n");
412
581
                TEST_FILE_EQ (output, "UPSTART_JOB=test\n");
413
582
                TEST_FILE_EQ (output, "UPSTART_INSTANCE=foo\n");
 
583
                TEST_FILE_EQ (output, "UPSTART_NO_SESSIONS=1\n");
414
584
                TEST_FILE_END (output);
415
585
                fclose (output);
416
586
                unlink (filename);
427
597
        TEST_ALLOC_FAIL {
428
598
                TEST_ALLOC_SAFE {
429
599
                        class = job_class_new (NULL, "test", NULL);
 
600
                        class->console = CONSOLE_NONE;
430
601
                        class->process[PROCESS_PRE_STOP] = process_new (class);
431
602
                        class->process[PROCESS_PRE_STOP]->command = nih_sprintf (
432
603
                                class->process[PROCESS_PRE_STOP],
462
633
                TEST_FILE_EQ (output, "CRACKLE=FIZZ\n");
463
634
                TEST_FILE_EQ (output, "UPSTART_JOB=test\n");
464
635
                TEST_FILE_EQ (output, "UPSTART_INSTANCE=\n");
 
636
                TEST_FILE_EQ (output, "UPSTART_NO_SESSIONS=1\n");
465
637
                TEST_FILE_END (output);
466
638
                fclose (output);
467
639
                unlink (filename);
478
650
        TEST_ALLOC_FAIL {
479
651
                TEST_ALLOC_SAFE {
480
652
                        class = job_class_new (NULL, "test", NULL);
 
653
                        class->console = CONSOLE_NONE;
481
654
                        class->process[PROCESS_POST_STOP] = process_new (class);
482
655
                        class->process[PROCESS_POST_STOP]->command = nih_sprintf (
483
656
                                class->process[PROCESS_POST_STOP],
513
686
                TEST_FILE_EQ (output, "CRACKLE=FIZZ\n");
514
687
                TEST_FILE_EQ (output, "UPSTART_JOB=test\n");
515
688
                TEST_FILE_EQ (output, "UPSTART_INSTANCE=\n");
 
689
                TEST_FILE_EQ (output, "UPSTART_NO_SESSIONS=1\n");
516
690
                TEST_FILE_END (output);
517
691
                fclose (output);
518
692
                unlink (filename);
529
703
        TEST_ALLOC_FAIL {
530
704
                TEST_ALLOC_SAFE {
531
705
                        class = job_class_new (NULL, "test", NULL);
 
706
                        class->console = CONSOLE_NONE;
532
707
                        class->process[PROCESS_MAIN] = process_new (class);
533
708
                        class->process[PROCESS_MAIN]->script = TRUE;
534
709
                        class->process[PROCESS_MAIN]->command = nih_alloc (
594
769
        TEST_ALLOC_FAIL {
595
770
                TEST_ALLOC_SAFE {
596
771
                        class = job_class_new (NULL, "test", NULL);
 
772
                        class->console = CONSOLE_NONE;
597
773
                        class->process[PROCESS_MAIN] = process_new (class);
598
774
                        class->process[PROCESS_MAIN]->command = "true";
599
775
 
630
806
        TEST_ALLOC_FAIL {
631
807
                TEST_ALLOC_SAFE {
632
808
                        class = job_class_new (NULL, "test", NULL);
 
809
                        class->console = CONSOLE_NONE;
633
810
                        class->process[PROCESS_PRE_START] = process_new (class);
634
811
                        class->process[PROCESS_PRE_START]->command = "true";
635
812
 
667
844
        TEST_ALLOC_FAIL {
668
845
                TEST_ALLOC_SAFE {
669
846
                        class = job_class_new (NULL, "test", NULL);
 
847
                        class->console = CONSOLE_NONE;
670
848
                        class->expect = EXPECT_DAEMON;
671
849
                        class->process[PROCESS_MAIN] = process_new (class);
672
850
                        class->process[PROCESS_MAIN]->command = "true";
714
892
        TEST_ALLOC_FAIL {
715
893
                TEST_ALLOC_SAFE {
716
894
                        class = job_class_new (NULL, "test", NULL);
 
895
                        class->console = CONSOLE_NONE;
717
896
                        class->expect = EXPECT_FORK;
718
897
                        class->process[PROCESS_MAIN] = process_new (class);
719
898
                        class->process[PROCESS_MAIN]->command = "true";
752
931
                nih_free (class);
753
932
        }
754
933
 
755
 
 
756
934
        /* Check that if we try and run a command that doesn't exist,
757
935
         * job_process_run() raises a ProcessError and the command doesn't
758
936
         * have any stored process id for it.
763
941
        TEST_ALLOC_FAIL {
764
942
                TEST_ALLOC_SAFE {
765
943
                        class = job_class_new (NULL, "test", NULL);
 
944
                        class->console = CONSOLE_NONE;
766
945
                        class->process[PROCESS_MAIN] = process_new (class);
767
946
                        class->process[PROCESS_MAIN]->command = filename;
768
947
 
787
966
 
788
967
                nih_free (class);
789
968
        }
 
969
 
 
970
        TEST_FILENAME (dirname);       
 
971
        TEST_EQ (mkdir (dirname, 0755), 0);
 
972
 
 
973
        /* Override default location to ensure job output goes to a
 
974
         * writeable location
 
975
         */
 
976
        TEST_EQ (setenv ("UPSTART_LOGDIR", dirname, 1), 0);
 
977
 
 
978
        /************************************************************/
 
979
        TEST_FEATURE ("ensure that no log file written for single-line no-output script");
 
980
 
 
981
        class = job_class_new (NULL, "test", NULL);
 
982
        TEST_NE_P (class, NULL);
 
983
        TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
 
984
 
 
985
        class->console = CONSOLE_LOG;
 
986
        class->process[PROCESS_MAIN] = process_new (class);
 
987
        class->process[PROCESS_MAIN]->command = nih_strdup (
 
988
                        class->process[PROCESS_MAIN],
 
989
                        "/bin/true");
 
990
        class->process[PROCESS_MAIN]->script = TRUE;
 
991
 
 
992
        job = job_new (class, "");
 
993
        job->goal = JOB_START;
 
994
        job->state = JOB_SPAWNED;
 
995
 
 
996
        ret = job_process_run (job, PROCESS_MAIN);
 
997
        TEST_EQ (ret, 0);
 
998
 
 
999
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
1000
 
 
1001
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
1002
        TEST_TRUE (WIFEXITED (status));
 
1003
        TEST_EQ (WEXITSTATUS (status), 0);
 
1004
 
 
1005
        TEST_FORCE_WATCH_UPDATE ();
 
1006
 
 
1007
        ENSURE_DIRECTORY_EMPTY (dirname);
 
1008
 
 
1009
        /* Paranoia */
 
1010
        TEST_TRUE (stat (filename, &statbuf) < 0 && errno == ENOENT);
 
1011
 
 
1012
        nih_free (class);
 
1013
 
 
1014
        /************************************************************/
 
1015
        TEST_FEATURE ("ensure that no log file written for single-line no-output command");
 
1016
 
 
1017
        class = job_class_new (NULL, "test", NULL);
 
1018
        TEST_NE_P (class, NULL);
 
1019
        TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
 
1020
 
 
1021
        class->console = CONSOLE_LOG;
 
1022
        class->process[PROCESS_MAIN] = process_new (class);
 
1023
        class->process[PROCESS_MAIN]->command = nih_strdup (
 
1024
                        class->process[PROCESS_MAIN],
 
1025
                        "/bin/true");
 
1026
        class->process[PROCESS_MAIN]->script = FALSE;
 
1027
 
 
1028
        job = job_new (class, "");
 
1029
        job->goal = JOB_START;
 
1030
        job->state = JOB_SPAWNED;
 
1031
 
 
1032
        ret = job_process_run (job, PROCESS_MAIN);
 
1033
        TEST_EQ (ret, 0);
 
1034
 
 
1035
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
1036
 
 
1037
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
1038
        TEST_TRUE (WIFEXITED (status));
 
1039
        TEST_EQ (WEXITSTATUS (status), 0);
 
1040
 
 
1041
        TEST_FORCE_WATCH_UPDATE ();
 
1042
 
 
1043
        ENSURE_DIRECTORY_EMPTY (dirname);
 
1044
 
 
1045
        /* Paranoia */
 
1046
        TEST_TRUE (stat (filename, &statbuf) < 0 && errno == ENOENT);
 
1047
 
 
1048
        nih_free (class);
 
1049
 
 
1050
        /************************************************************/
 
1051
        TEST_FEATURE ("ensure that no log file written for CONSOLE_NONE");
 
1052
 
 
1053
        class = job_class_new (NULL, "test", NULL);
 
1054
        TEST_NE_P (class, NULL);
 
1055
        TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
 
1056
 
 
1057
        class->console = CONSOLE_NONE;
 
1058
        class->process[PROCESS_MAIN] = process_new (class);
 
1059
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
1060
                        class->process[PROCESS_MAIN],
 
1061
                        "%s hello world", TEST_CMD_ECHO);
 
1062
        class->process[PROCESS_MAIN]->script = TRUE;
 
1063
 
 
1064
        job = job_new (class, "");
 
1065
        job->goal = JOB_START;
 
1066
        job->state = JOB_SPAWNED;
 
1067
 
 
1068
        ret = job_process_run (job, PROCESS_MAIN);
 
1069
        TEST_EQ (ret, 0);
 
1070
 
 
1071
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
1072
 
 
1073
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
1074
        TEST_TRUE (WIFEXITED (status));
 
1075
 
 
1076
        TEST_EQ (WEXITSTATUS (status), 0);
 
1077
 
 
1078
        /* If a log is written, select(2) will inform us, but we don't
 
1079
         * expect this, hence specify a timeout.
 
1080
         */
 
1081
        {
 
1082
                struct timeval t;
 
1083
                /* be generous */
 
1084
                t.tv_usec = 500000;
 
1085
                TEST_FORCE_WATCH_UPDATE_TIMEOUT (t);
 
1086
        }
 
1087
 
 
1088
        ENSURE_DIRECTORY_EMPTY (dirname);
 
1089
 
 
1090
        /* Paranoia */
 
1091
        TEST_TRUE (stat (filename, &statbuf) < 0 && errno == ENOENT);
 
1092
 
 
1093
        nih_free (class);
 
1094
 
 
1095
        /************************************************************/
 
1096
        TEST_FEATURE ("ensure that no log file written for multi-line no-output script");
 
1097
 
 
1098
        class = job_class_new (NULL, "test", NULL);
 
1099
        TEST_NE_P (class, NULL);
 
1100
        TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
 
1101
 
 
1102
        class->console = CONSOLE_LOG;
 
1103
        class->process[PROCESS_MAIN] = process_new (class);
 
1104
        class->process[PROCESS_MAIN]->command = nih_strdup (
 
1105
                        class->process[PROCESS_MAIN],
 
1106
                        "/bin/true\n/bin/false");
 
1107
        class->process[PROCESS_MAIN]->script = TRUE;
 
1108
 
 
1109
        job = job_new (class, "");
 
1110
        job->goal = JOB_START;
 
1111
        job->state = JOB_SPAWNED;
 
1112
 
 
1113
        ret = job_process_run (job, PROCESS_MAIN);
 
1114
        TEST_EQ (ret, 0);
 
1115
 
 
1116
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
1117
 
 
1118
        /* XXX: call 1: wait for script write to child shell */
 
1119
        TEST_FORCE_WATCH_UPDATE ();
 
1120
 
 
1121
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
1122
        TEST_TRUE (WIFEXITED (status));
 
1123
 
 
1124
        /* we've just run /bin/false remember? :) */
 
1125
        TEST_EQ (WEXITSTATUS (status), 1);
 
1126
 
 
1127
        /* XXX: call 2: wait for read from pty allowing logger to write to log file */
 
1128
        TEST_FORCE_WATCH_UPDATE ();
 
1129
 
 
1130
        ENSURE_DIRECTORY_EMPTY (dirname);
 
1131
 
 
1132
        /* Paranoia */
 
1133
        TEST_TRUE (stat (filename, &statbuf) < 0 && errno == ENOENT);
 
1134
 
 
1135
        nih_free (class);
 
1136
 
 
1137
        /************************************************************/
 
1138
        TEST_FEATURE ("with single-line script that writes 1 line to stdout");
 
1139
 
 
1140
        /* Note we can't use TEST_ALLOC_FAIL() for this test since on
 
1141
         * the ENOMEM loop all we could do is discard the error and
 
1142
         * continue since job_process_run() calls job_process_spawn()
 
1143
         * repeatedly until it works, but the alloc fails in log_new()
 
1144
         * invoked by job_process_spawn() such that when we've left
 
1145
         * job_process_run(), it's too late.
 
1146
         *
 
1147
         * However, we test this scenario in test_spawn() so all is not
 
1148
         * lost.
 
1149
         */
 
1150
        class = job_class_new (NULL, "test", NULL);
 
1151
        TEST_NE_P (class, NULL);
 
1152
 
 
1153
        TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
 
1154
 
 
1155
        class->console = CONSOLE_LOG;
 
1156
        class->process[PROCESS_MAIN] = process_new (class);
 
1157
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
1158
                        class->process[PROCESS_MAIN],
 
1159
                        "%s hello world", TEST_CMD_ECHO);
 
1160
        class->process[PROCESS_MAIN]->script = TRUE;
 
1161
 
 
1162
        job = job_new (class, "");
 
1163
        job->goal = JOB_START;
 
1164
        job->state = JOB_SPAWNED;
 
1165
 
 
1166
        ret = job_process_run (job, PROCESS_MAIN);
 
1167
        TEST_EQ (ret, 0);
 
1168
 
 
1169
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
1170
 
 
1171
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
1172
        TEST_TRUE (WIFEXITED (status));
 
1173
        TEST_EQ (WEXITSTATUS (status), 0);
 
1174
 
 
1175
        TEST_FORCE_WATCH_UPDATE ();
 
1176
 
 
1177
        TEST_EQ (stat (filename, &statbuf), 0);
 
1178
 
 
1179
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
1180
 
 
1181
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
1182
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
1183
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
1184
 
 
1185
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
1186
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
1187
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
1188
 
 
1189
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
1190
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
1191
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
1192
 
 
1193
        output = fopen (filename, "r");
 
1194
        TEST_NE_P (output, NULL);
 
1195
 
 
1196
        TEST_FILE_EQ (output, "hello world\r\n");
 
1197
        TEST_FILE_END (output);
 
1198
        fclose (output);
 
1199
 
 
1200
        TEST_EQ (unlink (filename), 0);
 
1201
        nih_free (class);
 
1202
 
 
1203
        /************************************************************/
 
1204
        TEST_FEATURE ("with single-line script that is killed");
 
1205
 
 
1206
        class = job_class_new (NULL, "test", NULL);
 
1207
        TEST_NE_P (class, NULL);
 
1208
 
 
1209
        TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
 
1210
 
 
1211
        class->console = CONSOLE_LOG;
 
1212
        class->process[PROCESS_MAIN] = process_new (class);
 
1213
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
1214
                        class->process[PROCESS_MAIN],
 
1215
                        "%s hello world;sleep 999", TEST_CMD_ECHO);
 
1216
        class->process[PROCESS_MAIN]->script = TRUE;
 
1217
 
 
1218
        job = job_new (class, "");
 
1219
        job->goal = JOB_START;
 
1220
        job->state = JOB_SPAWNED;
 
1221
 
 
1222
        ret = job_process_run (job, PROCESS_MAIN);
 
1223
        TEST_EQ (ret, 0);
 
1224
 
 
1225
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
1226
 
 
1227
        /*  wait for read from pty allowing logger to write to log file */
 
1228
        TEST_FORCE_WATCH_UPDATE ();
 
1229
 
 
1230
        TEST_EQ (kill (job->pid[PROCESS_MAIN], SIGKILL), 0);
 
1231
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
1232
        TEST_TRUE (WIFSIGNALED (status));
 
1233
        TEST_EQ (WTERMSIG (status), SIGKILL);
 
1234
 
 
1235
        TEST_EQ (stat (filename, &statbuf), 0);
 
1236
 
 
1237
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
1238
 
 
1239
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
1240
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
1241
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
1242
 
 
1243
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
1244
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
1245
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
1246
 
 
1247
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
1248
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
1249
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
1250
 
 
1251
        output = fopen (filename, "r");
 
1252
        TEST_NE_P (output, NULL);
 
1253
 
 
1254
        TEST_FILE_EQ (output, "hello world\r\n");
 
1255
        TEST_FILE_END (output);
 
1256
        fclose (output);
 
1257
 
 
1258
        TEST_EQ (unlink (filename), 0);
 
1259
        nih_free (class);
 
1260
 
 
1261
        /************************************************************/
 
1262
        TEST_FEATURE ("with single-line command that is killed");
 
1263
 
 
1264
        class = job_class_new (NULL, "test", NULL);
 
1265
        TEST_NE_P (class, NULL);
 
1266
 
 
1267
        TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
 
1268
 
 
1269
        class->console = CONSOLE_LOG;
 
1270
        class->process[PROCESS_MAIN] = process_new (class);
 
1271
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
1272
                        class->process[PROCESS_MAIN],
 
1273
                        "%s", TEST_CMD_YES);
 
1274
        class->process[PROCESS_MAIN]->script = FALSE;
 
1275
 
 
1276
        job = job_new (class, "");
 
1277
        job->goal = JOB_START;
 
1278
        job->state = JOB_SPAWNED;
 
1279
 
 
1280
        ret = job_process_run (job, PROCESS_MAIN);
 
1281
        TEST_EQ (ret, 0);
 
1282
 
 
1283
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
1284
 
 
1285
        /*  wait for read from pty allowing logger to write to log file */
 
1286
        TEST_FORCE_WATCH_UPDATE ();
 
1287
 
 
1288
        TEST_EQ (kill (job->pid[PROCESS_MAIN], SIGKILL), 0);
 
1289
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
1290
        TEST_TRUE (WIFSIGNALED (status));
 
1291
        TEST_EQ (WTERMSIG (status), SIGKILL);
 
1292
 
 
1293
        TEST_EQ (stat (filename, &statbuf), 0);
 
1294
 
 
1295
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
1296
 
 
1297
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
1298
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
1299
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
1300
 
 
1301
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
1302
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
1303
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
1304
 
 
1305
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
1306
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
1307
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
1308
 
 
1309
        output = fopen (filename, "r");
 
1310
        TEST_NE_P (output, NULL);
 
1311
 
 
1312
        /* XXX: this _might_ be a kernel(?) bug - sometimes we don't read
 
1313
         * the final line end character (presumably since the process
 
1314
         * was forcibly killed).
 
1315
         */
 
1316
        while (fgets (filebuf, sizeof(filebuf), output) != NULL) {
 
1317
                if (! strcmp (filebuf, "y\r\n"))
 
1318
                        ok = 1;
 
1319
                else if (! strcmp (filebuf, "y") && feof (output))
 
1320
                        ok = 1;
 
1321
                else
 
1322
                        ok = 0;
 
1323
 
 
1324
                if (! ok)
 
1325
                        break;
 
1326
        }
 
1327
        TEST_EQ (ok, 1);
 
1328
 
 
1329
        TEST_FILE_END (output);
 
1330
        fclose (output);
 
1331
 
 
1332
        TEST_EQ (unlink (filename), 0);
 
1333
        nih_free (class);
 
1334
 
 
1335
        /************************************************************/
 
1336
        TEST_FEATURE ("with multi-line script that is killed");
 
1337
 
 
1338
        /* Note we can't use TEST_ALLOC_FAIL() for this test since on
 
1339
         * the ENOMEM loop all we could do is discard the error and
 
1340
         * continue since job_process_run() calls job_process_spawn()
 
1341
         * repeatedly until it works, but the alloc fails in log_new()
 
1342
         * invoked by job_process_spawn() such that when we've left
 
1343
         * job_process_run(), it's too late.
 
1344
         *
 
1345
         * However, we test this scenario in test_spawn() so all is not
 
1346
         * lost.
 
1347
         */
 
1348
        class = job_class_new (NULL, "test", NULL);
 
1349
        TEST_NE_P (class, NULL);
 
1350
 
 
1351
        TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
 
1352
 
 
1353
        class->console = CONSOLE_LOG;
 
1354
        class->process[PROCESS_MAIN] = process_new (class);
 
1355
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
1356
                        class->process[PROCESS_MAIN],
 
1357
                        "%s hello world\nsleep 999", TEST_CMD_ECHO);
 
1358
        class->process[PROCESS_MAIN]->script = TRUE;
 
1359
 
 
1360
        job = job_new (class, "");
 
1361
        job->goal = JOB_START;
 
1362
        job->state = JOB_SPAWNED;
 
1363
 
 
1364
        ret = job_process_run (job, PROCESS_MAIN);
 
1365
        TEST_EQ (ret, 0);
 
1366
 
 
1367
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
1368
 
 
1369
        /* XXX: call 1: wait for script write to child shell */
 
1370
        TEST_FORCE_WATCH_UPDATE ();
 
1371
 
 
1372
        /* XXX: call 2: wait for read from pty allowing logger to write to log file */
 
1373
        TEST_FORCE_WATCH_UPDATE ();
 
1374
 
 
1375
        TEST_EQ (kill (job->pid[PROCESS_MAIN], SIGKILL), 0);
 
1376
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
1377
        TEST_TRUE (WIFSIGNALED (status));
 
1378
        TEST_EQ (WTERMSIG (status), SIGKILL);
 
1379
 
 
1380
        TEST_EQ (stat (filename, &statbuf), 0);
 
1381
 
 
1382
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
1383
 
 
1384
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
1385
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
1386
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
1387
 
 
1388
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
1389
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
1390
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
1391
 
 
1392
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
1393
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
1394
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
1395
 
 
1396
        output = fopen (filename, "r");
 
1397
        TEST_NE_P (output, NULL);
 
1398
 
 
1399
        TEST_FILE_EQ (output, "hello world\r\n");
 
1400
        TEST_FILE_END (output);
 
1401
        fclose (output);
 
1402
 
 
1403
        TEST_EQ (unlink (filename), 0);
 
1404
        nih_free (class);
 
1405
 
 
1406
        /************************************************************/
 
1407
        TEST_FEATURE ("with single-line script that writes 1 byte and is killed");
 
1408
 
 
1409
        class = job_class_new (NULL, "test", NULL);
 
1410
        TEST_NE_P (class, NULL);
 
1411
 
 
1412
        TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
 
1413
 
 
1414
        class->console = CONSOLE_LOG;
 
1415
        class->process[PROCESS_MAIN] = process_new (class);
 
1416
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
1417
                        class->process[PROCESS_MAIN],
 
1418
                        "%s -ne X;sleep 999", TEST_CMD_ECHO);
 
1419
        class->process[PROCESS_MAIN]->script = TRUE;
 
1420
 
 
1421
        job = job_new (class, "");
 
1422
        job->goal = JOB_START;
 
1423
        job->state = JOB_SPAWNED;
 
1424
 
 
1425
        ret = job_process_run (job, PROCESS_MAIN);
 
1426
        TEST_EQ (ret, 0);
 
1427
 
 
1428
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
1429
 
 
1430
        /*  wait for read from pty allowing logger to write to log file */
 
1431
        TEST_FORCE_WATCH_UPDATE ();
 
1432
 
 
1433
        TEST_EQ (kill (job->pid[PROCESS_MAIN], SIGKILL), 0);
 
1434
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
1435
        TEST_TRUE (WIFSIGNALED (status));
 
1436
        TEST_EQ (WTERMSIG (status), SIGKILL);
 
1437
 
 
1438
        TEST_EQ (stat (filename, &statbuf), 0);
 
1439
 
 
1440
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
1441
 
 
1442
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
1443
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
1444
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
1445
 
 
1446
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
1447
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
1448
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
1449
 
 
1450
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
1451
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
1452
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
1453
 
 
1454
        output = fopen (filename, "r");
 
1455
        TEST_NE_P (output, NULL);
 
1456
 
 
1457
        TEST_FILE_EQ (output, "X");
 
1458
        TEST_FILE_END (output);
 
1459
        fclose (output);
 
1460
 
 
1461
        TEST_EQ (unlink (filename), 0);
 
1462
        nih_free (class);
 
1463
 
 
1464
        /************************************************************/
 
1465
        /* Can't think of a command that would echo 1 byte and then
 
1466
         * either sleep or read a file forever to allow us time to kill
 
1467
         * it *after* it had written the single byte. Answers on a
 
1468
         * postcard please.
 
1469
         *
 
1470
         * TEST_FEATURE ("with single-line command that writes 1 byte and is killed");
 
1471
         */
 
1472
 
 
1473
        /************************************************************/
 
1474
        TEST_FEATURE ("with multi-line script that writes 1 byte and is killed");
 
1475
 
 
1476
        class = job_class_new (NULL, "multiline", NULL);
 
1477
        TEST_NE_P (class, NULL);
 
1478
 
 
1479
        TEST_FILENAME (filename);
 
1480
        TEST_GT (sprintf (filename, "%s/multiline.log", dirname), 0);
 
1481
 
 
1482
        class->console = CONSOLE_LOG;
 
1483
        class->process[PROCESS_MAIN] = process_new (class);
 
1484
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
1485
                        class->process[PROCESS_MAIN],
 
1486
                        "/bin/true\n%s -ne F", TEST_CMD_ECHO);
 
1487
        class->process[PROCESS_MAIN]->script = TRUE;
 
1488
 
 
1489
        job = job_new (class, "");
 
1490
        job->goal = JOB_START;
 
1491
        job->state = JOB_SPAWNED;
 
1492
 
 
1493
        ret = job_process_run (job, PROCESS_MAIN);
 
1494
        TEST_EQ (ret, 0);
 
1495
 
 
1496
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
1497
 
 
1498
        /* XXX: call 1: wait for script write to child shell */
 
1499
        TEST_FORCE_WATCH_UPDATE ();
 
1500
 
 
1501
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
1502
        TEST_TRUE (WIFEXITED (status));
 
1503
        TEST_EQ (WEXITSTATUS (status), 0);
 
1504
 
 
1505
        /* XXX: call 2: wait for read from pty allowing logger to write to log file */
 
1506
        TEST_FORCE_WATCH_UPDATE ();
 
1507
 
 
1508
        TEST_EQ (stat (filename, &statbuf), 0);
 
1509
 
 
1510
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
1511
 
 
1512
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
1513
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
1514
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
1515
 
 
1516
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
1517
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
1518
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
1519
 
 
1520
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
1521
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
1522
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
1523
 
 
1524
        output = fopen (filename, "r");
 
1525
        TEST_NE_P (output, NULL);
 
1526
 
 
1527
        TEST_FILE_EQ (output, "F");
 
1528
        TEST_FILE_END (output);
 
1529
        fclose (output);
 
1530
 
 
1531
        TEST_EQ (unlink (filename), 0);
 
1532
        nih_free (class);
 
1533
 
 
1534
        /************************************************************/
 
1535
        TEST_FEATURE ("with single-line command that writes 1 line to stdout");
 
1536
 
 
1537
        /* Note we can't use TEST_ALLOC_FAIL() for this test since on
 
1538
         * the ENOMEM loop all we could do is discard the error and
 
1539
         * continue since job_process_run() calls job_process_spawn()
 
1540
         * repeatedly until it works, but the alloc fails in log_new()
 
1541
         * invoked by job_process_spawn() such that when we've left
 
1542
         * job_process_run(), it's too late.
 
1543
         *
 
1544
         * However, we test this scenario in test_spawn() so all is not
 
1545
         * lost.
 
1546
         */
 
1547
        class = job_class_new (NULL, "test", NULL);
 
1548
        TEST_NE_P (class, NULL);
 
1549
 
 
1550
        TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
 
1551
 
 
1552
        class->console = CONSOLE_LOG;
 
1553
        class->process[PROCESS_MAIN] = process_new (class);
 
1554
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
1555
                        class->process[PROCESS_MAIN],
 
1556
                        "%s hello world", TEST_CMD_ECHO);
 
1557
        class->process[PROCESS_MAIN]->script = FALSE;
 
1558
 
 
1559
        job = job_new (class, "");
 
1560
        job->goal = JOB_START;
 
1561
        job->state = JOB_SPAWNED;
 
1562
 
 
1563
        ret = job_process_run (job, PROCESS_MAIN);
 
1564
        TEST_EQ (ret, 0);
 
1565
 
 
1566
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
1567
 
 
1568
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
1569
        TEST_TRUE (WIFEXITED (status));
 
1570
        TEST_EQ (WEXITSTATUS (status), 0);
 
1571
 
 
1572
        TEST_FORCE_WATCH_UPDATE ();
 
1573
 
 
1574
        TEST_EQ (stat (filename, &statbuf), 0);
 
1575
 
 
1576
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
1577
 
 
1578
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
1579
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
1580
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
1581
 
 
1582
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
1583
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
1584
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
1585
 
 
1586
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
1587
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
1588
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
1589
 
 
1590
        output = fopen (filename, "r");
 
1591
        TEST_NE_P (output, NULL);
 
1592
 
 
1593
        TEST_FILE_EQ (output, "hello world\r\n");
 
1594
        TEST_FILE_END (output);
 
1595
        fclose (output);
 
1596
 
 
1597
        TEST_EQ (unlink (filename), 0);
 
1598
        nih_free (class);
 
1599
 
 
1600
        /************************************************************/
 
1601
        TEST_FEATURE ("with multi-line script that writes 1 line to stdout");
 
1602
 
 
1603
        /* XXX: Note that all tests which use multi-line scripts (but
 
1604
         * XXX: *NOT* commands!) and produce output must call
 
1605
         * XXX: TEST_FORCE_WATCH_UPDATE() *TWICE* to ensure select(2) is
 
1606
         * XXX: called twice.
 
1607
         *
 
1608
         * This is required since job_process_run() uses an NihIo object
 
1609
         * to squirt the script to the shell sub-process and this
 
1610
         * triggers select to return when the data is written to the shell.
 
1611
         * However, we don't care about that directly - we care more about
 
1612
         * a subsequent fd becoming ready to read data from - the fd
 
1613
         * associated with the pty which will trigger the log file to be
 
1614
         * written.
 
1615
         *
 
1616
         * Note that the 2nd call to TEST_FORCE_WATCH_UPDATE would not be
 
1617
         * required should job_process_run() simple invoke write(2) to
 
1618
         * send the data.
 
1619
         */
 
1620
 
 
1621
        class = job_class_new (NULL, "multiline", NULL);
 
1622
        TEST_NE_P (class, NULL);
 
1623
 
 
1624
        TEST_FILENAME (filename);
 
1625
        TEST_GT (sprintf (filename, "%s/multiline.log", dirname), 0);
 
1626
 
 
1627
        class->console = CONSOLE_LOG;
 
1628
        class->process[PROCESS_MAIN] = process_new (class);
 
1629
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
1630
                        class->process[PROCESS_MAIN],
 
1631
                        "/bin/true\n%s hello world\n\n\n\n\n\n\n\n\n\n", TEST_CMD_ECHO);
 
1632
        class->process[PROCESS_MAIN]->script = TRUE;
 
1633
 
 
1634
        job = job_new (class, "");
 
1635
        job->goal = JOB_START;
 
1636
        job->state = JOB_SPAWNED;
 
1637
 
 
1638
        ret = job_process_run (job, PROCESS_MAIN);
 
1639
        TEST_EQ (ret, 0);
 
1640
 
 
1641
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
1642
 
 
1643
        /* XXX: call 1: wait for script write to child shell */
 
1644
        TEST_FORCE_WATCH_UPDATE ();
 
1645
 
 
1646
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
1647
        TEST_TRUE (WIFEXITED (status));
 
1648
        TEST_EQ (WEXITSTATUS (status), 0);
 
1649
 
 
1650
        /* XXX: call 2: wait for read from pty allowing logger to write to log file */
 
1651
        TEST_FORCE_WATCH_UPDATE ();
 
1652
 
 
1653
        TEST_EQ (stat (filename, &statbuf), 0);
 
1654
 
 
1655
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
1656
 
 
1657
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
1658
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
1659
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
1660
 
 
1661
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
1662
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
1663
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
1664
 
 
1665
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
1666
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
1667
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
1668
 
 
1669
        output = fopen (filename, "r");
 
1670
        TEST_NE_P (output, NULL);
 
1671
 
 
1672
        TEST_FILE_EQ (output, "hello world\r\n");
 
1673
        TEST_FILE_END (output);
 
1674
        fclose (output);
 
1675
 
 
1676
        TEST_EQ (unlink (filename), 0);
 
1677
        nih_free (class);
 
1678
 
 
1679
        /************************************************************/
 
1680
        TEST_FEATURE ("with instance job and single-line script that writes 1 line to stdout");
 
1681
 
 
1682
        class = job_class_new (NULL, "test", NULL);
 
1683
        TEST_NE_P (class, NULL);
 
1684
 
 
1685
        TEST_GT (sprintf (filename, "%s/test-instance.log", dirname), 0);
 
1686
 
 
1687
        class->console = CONSOLE_LOG;
 
1688
        class->process[PROCESS_MAIN] = process_new (class);
 
1689
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
1690
                        class->process[PROCESS_MAIN],
 
1691
                        "%s hello world", TEST_CMD_ECHO);
 
1692
        class->process[PROCESS_MAIN]->script = TRUE;
 
1693
 
 
1694
        job = job_new (class, "instance");
 
1695
        job->goal = JOB_START;
 
1696
        job->state = JOB_SPAWNED;
 
1697
 
 
1698
        ret = job_process_run (job, PROCESS_MAIN);
 
1699
        TEST_EQ (ret, 0);
 
1700
 
 
1701
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
1702
 
 
1703
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
1704
        TEST_TRUE (WIFEXITED (status));
 
1705
        TEST_EQ (WEXITSTATUS (status), 0);
 
1706
 
 
1707
        TEST_FORCE_WATCH_UPDATE ();
 
1708
 
 
1709
        TEST_EQ (stat (filename, &statbuf), 0);
 
1710
 
 
1711
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
1712
 
 
1713
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
1714
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
1715
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
1716
 
 
1717
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
1718
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
1719
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
1720
 
 
1721
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
1722
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
1723
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
1724
 
 
1725
        output = fopen (filename, "r");
 
1726
        TEST_NE_P (output, NULL);
 
1727
 
 
1728
        TEST_FILE_EQ (output, "hello world\r\n");
 
1729
        TEST_FILE_END (output);
 
1730
        fclose (output);
 
1731
 
 
1732
        TEST_EQ (unlink (filename), 0);
 
1733
        nih_free (class);
 
1734
 
 
1735
        /************************************************************/
 
1736
        TEST_FEATURE ("with single-line script that writes >1 lines to stdout");
 
1737
 
 
1738
        class = job_class_new (NULL, "foo", NULL);
 
1739
        TEST_NE_P (class, NULL);
 
1740
 
 
1741
        TEST_FILENAME (filename);
 
1742
        TEST_GT (sprintf (filename, "%s/foo.log", dirname), 0);
 
1743
 
 
1744
        class->console = CONSOLE_LOG;
 
1745
        class->process[PROCESS_MAIN] = process_new (class);
 
1746
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
1747
                        class->process[PROCESS_MAIN],
 
1748
                        /* XXX: note the required quoting */
 
1749
                        "%s -ne \"hello world\\n\\n\\n\"", TEST_CMD_ECHO);
 
1750
        class->process[PROCESS_MAIN]->script = TRUE;
 
1751
 
 
1752
        job = job_new (class, "");
 
1753
        job->goal = JOB_START;
 
1754
        job->state = JOB_SPAWNED;
 
1755
 
 
1756
        ret = job_process_run (job, PROCESS_MAIN);
 
1757
        TEST_EQ (ret, 0);
 
1758
 
 
1759
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
1760
 
 
1761
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
1762
        TEST_TRUE (WIFEXITED (status));
 
1763
        TEST_EQ (WEXITSTATUS (status), 0);
 
1764
 
 
1765
        TEST_FORCE_WATCH_UPDATE ();
 
1766
 
 
1767
        TEST_EQ (stat (filename, &statbuf), 0);
 
1768
 
 
1769
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
1770
 
 
1771
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
1772
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
1773
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
1774
 
 
1775
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
1776
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
1777
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
1778
 
 
1779
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
1780
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
1781
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
1782
 
 
1783
        output = fopen (filename, "r");
 
1784
        TEST_NE_P (output, NULL);
 
1785
 
 
1786
        /* Yup, pseudo-terminals record *everything*,
 
1787
         * even the carriage returns.
 
1788
         */
 
1789
        TEST_FILE_EQ (output, "hello world\r\n");
 
1790
        TEST_FILE_EQ (output, "\r\n");
 
1791
        TEST_FILE_EQ (output, "\r\n");
 
1792
        TEST_FILE_END (output);
 
1793
        fclose (output);
 
1794
 
 
1795
        TEST_EQ (unlink (filename), 0);
 
1796
        nih_free (class);
 
1797
 
 
1798
        /************************************************************/
 
1799
        TEST_FEATURE ("with single-line command that writes >1 lines to stdout");
 
1800
 
 
1801
        class = job_class_new (NULL, "foo", NULL);
 
1802
        TEST_NE_P (class, NULL);
 
1803
 
 
1804
        TEST_FILENAME (filename);
 
1805
        TEST_GT (sprintf (filename, "%s/foo.log", dirname), 0);
 
1806
 
 
1807
        class->console = CONSOLE_LOG;
 
1808
        class->process[PROCESS_MAIN] = process_new (class);
 
1809
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
1810
                        class->process[PROCESS_MAIN],
 
1811
                        /* XXX: note the required quoting */
 
1812
                        "%s -ne \"hello world\\n\\n\\n\"", TEST_CMD_ECHO);
 
1813
        class->process[PROCESS_MAIN]->script = FALSE;
 
1814
 
 
1815
        job = job_new (class, "");
 
1816
        job->goal = JOB_START;
 
1817
        job->state = JOB_SPAWNED;
 
1818
 
 
1819
        ret = job_process_run (job, PROCESS_MAIN);
 
1820
        TEST_EQ (ret, 0);
 
1821
 
 
1822
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
1823
 
 
1824
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
1825
        TEST_TRUE (WIFEXITED (status));
 
1826
        TEST_EQ (WEXITSTATUS (status), 0);
 
1827
 
 
1828
        TEST_FORCE_WATCH_UPDATE ();
 
1829
 
 
1830
        TEST_EQ (stat (filename, &statbuf), 0);
 
1831
 
 
1832
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
1833
 
 
1834
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
1835
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
1836
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
1837
 
 
1838
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
1839
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
1840
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
1841
 
 
1842
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
1843
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
1844
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
1845
 
 
1846
        output = fopen (filename, "r");
 
1847
        TEST_NE_P (output, NULL);
 
1848
 
 
1849
        TEST_FILE_EQ (output, "hello world\r\n");
 
1850
        TEST_FILE_EQ (output, "\r\n");
 
1851
        TEST_FILE_EQ (output, "\r\n");
 
1852
        TEST_FILE_END (output);
 
1853
        fclose (output);
 
1854
 
 
1855
        TEST_EQ (unlink (filename), 0);
 
1856
        nih_free (class);
 
1857
 
 
1858
        /************************************************************/
 
1859
        TEST_FEATURE ("with multi-line script that writes >1 lines to stdout");
 
1860
 
 
1861
        class = job_class_new (NULL, "elf", NULL);
 
1862
        TEST_NE_P (class, NULL);
 
1863
 
 
1864
        TEST_FILENAME (filename);
 
1865
        TEST_GT (sprintf (filename, "%s/elf.log", dirname), 0);
 
1866
 
 
1867
        class->console = CONSOLE_LOG;
 
1868
        class->process[PROCESS_MAIN] = process_new (class);
 
1869
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
1870
                        class->process[PROCESS_MAIN],
 
1871
                        /* XXX: note the required quoting */
 
1872
                        "\n/bin/true\n%s -ne \"hello world\\n\\n\\n\"\n\n", TEST_CMD_ECHO);
 
1873
        class->process[PROCESS_MAIN]->script = TRUE;
 
1874
 
 
1875
        job = job_new (class, "");
 
1876
        job->goal = JOB_START;
 
1877
        job->state = JOB_SPAWNED;
 
1878
 
 
1879
        ret = job_process_run (job, PROCESS_MAIN);
 
1880
        TEST_EQ (ret, 0);
 
1881
 
 
1882
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
1883
 
 
1884
        TEST_FORCE_WATCH_UPDATE ();
 
1885
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
1886
        TEST_TRUE (WIFEXITED (status));
 
1887
        TEST_EQ (WEXITSTATUS (status), 0);
 
1888
        TEST_FORCE_WATCH_UPDATE ();
 
1889
 
 
1890
        TEST_EQ (stat (filename, &statbuf), 0);
 
1891
 
 
1892
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
1893
 
 
1894
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
1895
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
1896
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
1897
 
 
1898
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
1899
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
1900
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
1901
 
 
1902
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
1903
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
1904
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
1905
 
 
1906
        output = fopen (filename, "r");
 
1907
        TEST_NE_P (output, NULL);
 
1908
 
 
1909
        TEST_FILE_EQ (output, "hello world\r\n");
 
1910
        TEST_FILE_EQ (output, "\r\n");
 
1911
        TEST_FILE_EQ (output, "\r\n");
 
1912
        TEST_FILE_END (output);
 
1913
        fclose (output);
 
1914
 
 
1915
        TEST_EQ (unlink (filename), 0);
 
1916
        nih_free (class);
 
1917
 
 
1918
        /************************************************************/
 
1919
        TEST_FEATURE ("with single-line script that writes 1 line to stderr");
 
1920
 
 
1921
        class = job_class_new (NULL, "test", NULL);
 
1922
        TEST_NE_P (class, NULL);
 
1923
 
 
1924
        TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
 
1925
 
 
1926
        class->console = CONSOLE_LOG;
 
1927
        class->process[PROCESS_MAIN] = process_new (class);
 
1928
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
1929
                        class->process[PROCESS_MAIN],
 
1930
                        "%s hello world >&2", TEST_CMD_ECHO);
 
1931
        class->process[PROCESS_MAIN]->script = TRUE;
 
1932
 
 
1933
        job = job_new (class, "");
 
1934
        job->goal = JOB_START;
 
1935
        job->state = JOB_SPAWNED;
 
1936
 
 
1937
        ret = job_process_run (job, PROCESS_MAIN);
 
1938
        TEST_EQ (ret, 0);
 
1939
 
 
1940
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
1941
 
 
1942
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
1943
        TEST_TRUE (WIFEXITED (status));
 
1944
        TEST_EQ (WEXITSTATUS (status), 0);
 
1945
 
 
1946
        TEST_FORCE_WATCH_UPDATE ();
 
1947
 
 
1948
        TEST_EQ (stat (filename, &statbuf), 0);
 
1949
 
 
1950
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
1951
 
 
1952
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
1953
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
1954
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
1955
 
 
1956
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
1957
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
1958
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
1959
 
 
1960
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
1961
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
1962
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
1963
 
 
1964
        output = fopen (filename, "r");
 
1965
        TEST_NE_P (output, NULL);
 
1966
 
 
1967
        TEST_FILE_EQ (output, "hello world\r\n");
 
1968
        TEST_FILE_END (output);
 
1969
        fclose (output);
 
1970
 
 
1971
        TEST_EQ (unlink (filename), 0);
 
1972
        nih_free (class);
 
1973
 
 
1974
        /************************************************************/
 
1975
        TEST_FEATURE ("with single-line command that writes 1 line to stderr");
 
1976
 
 
1977
        /* Run a command that generates output to stderr without having
 
1978
         * to use script redirection.
 
1979
         *
 
1980
         * dd(1) is a good choice as it always writes to stderr.
 
1981
         */
 
1982
        class = job_class_new (NULL, "test", NULL);
 
1983
        TEST_NE_P (class, NULL);
 
1984
 
 
1985
        TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
 
1986
 
 
1987
        class->console = CONSOLE_LOG;
 
1988
        class->process[PROCESS_MAIN] = process_new (class);
 
1989
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
1990
                        class->process[PROCESS_MAIN],
 
1991
                        "%s if=/dev/zero of=/dev/null bs=1 count=0", TEST_CMD_DD);
 
1992
        class->process[PROCESS_MAIN]->script = TRUE;
 
1993
 
 
1994
        job = job_new (class, "");
 
1995
        job->goal = JOB_START;
 
1996
        job->state = JOB_SPAWNED;
 
1997
 
 
1998
        ret = job_process_run (job, PROCESS_MAIN);
 
1999
        TEST_EQ (ret, 0);
 
2000
 
 
2001
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
2002
 
 
2003
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
2004
        TEST_TRUE (WIFEXITED (status));
 
2005
        TEST_EQ (WEXITSTATUS (status), 0);
 
2006
 
 
2007
        TEST_FORCE_WATCH_UPDATE ();
 
2008
 
 
2009
        TEST_EQ (stat (filename, &statbuf), 0);
 
2010
 
 
2011
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
2012
 
 
2013
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
2014
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
2015
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
2016
 
 
2017
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
2018
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
2019
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
2020
 
 
2021
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
2022
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
2023
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
2024
 
 
2025
        output = fopen (filename, "r");
 
2026
        TEST_NE_P (output, NULL);
 
2027
 
 
2028
        TEST_FILE_EQ (output, "0+0 records in\r\n");
 
2029
        TEST_FILE_EQ (output, "0+0 records out\r\n");
 
2030
        TEST_FILE_MATCH (output, "0 bytes (0 B) copied,*\r\n");
 
2031
        TEST_FILE_END (output);
 
2032
        fclose (output);
 
2033
 
 
2034
        TEST_EQ (unlink (filename), 0);
 
2035
        nih_free (class);
 
2036
 
 
2037
        /************************************************************/
 
2038
        TEST_FEATURE ("with multi-line script that writes 1 line to stderr");
 
2039
 
 
2040
        class = job_class_new (NULL, "test", NULL);
 
2041
        TEST_NE_P (class, NULL);
 
2042
 
 
2043
        TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
 
2044
 
 
2045
        class->console = CONSOLE_LOG;
 
2046
        class->process[PROCESS_MAIN] = process_new (class);
 
2047
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
2048
                        class->process[PROCESS_MAIN],
 
2049
                        "/bin/true\n%s hello world >&2\n\n\n", TEST_CMD_ECHO);
 
2050
        class->process[PROCESS_MAIN]->script = TRUE;
 
2051
 
 
2052
        job = job_new (class, "");
 
2053
        job->goal = JOB_START;
 
2054
        job->state = JOB_SPAWNED;
 
2055
 
 
2056
        ret = job_process_run (job, PROCESS_MAIN);
 
2057
        TEST_EQ (ret, 0);
 
2058
 
 
2059
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
2060
 
 
2061
        TEST_FORCE_WATCH_UPDATE ();
 
2062
 
 
2063
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
2064
        TEST_TRUE (WIFEXITED (status));
 
2065
        TEST_EQ (WEXITSTATUS (status), 0);
 
2066
 
 
2067
        TEST_FORCE_WATCH_UPDATE ();
 
2068
 
 
2069
        TEST_EQ (stat (filename, &statbuf), 0);
 
2070
 
 
2071
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
2072
 
 
2073
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
2074
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
2075
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
2076
 
 
2077
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
2078
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
2079
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
2080
 
 
2081
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
2082
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
2083
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
2084
 
 
2085
        output = fopen (filename, "r");
 
2086
        TEST_NE_P (output, NULL);
 
2087
 
 
2088
        TEST_FILE_EQ (output, "hello world\r\n");
 
2089
        TEST_FILE_END (output);
 
2090
        fclose (output);
 
2091
 
 
2092
        TEST_EQ (unlink (filename), 0);
 
2093
        nih_free (class);
 
2094
 
 
2095
        /************************************************************/
 
2096
        TEST_FEATURE ("with single-line script that writes >1 lines to stderr");
 
2097
 
 
2098
        class = job_class_new (NULL, "foo", NULL);
 
2099
        TEST_NE_P (class, NULL);
 
2100
 
 
2101
        TEST_FILENAME (filename);
 
2102
        TEST_GT (sprintf (filename, "%s/foo.log", dirname), 0);
 
2103
 
 
2104
        class->console = CONSOLE_LOG;
 
2105
        class->process[PROCESS_MAIN] = process_new (class);
 
2106
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
2107
                        class->process[PROCESS_MAIN],
 
2108
                        /* XXX: note the required quoting */
 
2109
                        "%s -ne \"hello\\nworld\\n\\n\\n\" >&2", TEST_CMD_ECHO);
 
2110
        class->process[PROCESS_MAIN]->script = TRUE;
 
2111
 
 
2112
        job = job_new (class, "");
 
2113
        job->goal = JOB_START;
 
2114
        job->state = JOB_SPAWNED;
 
2115
 
 
2116
        ret = job_process_run (job, PROCESS_MAIN);
 
2117
        TEST_EQ (ret, 0);
 
2118
 
 
2119
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
2120
 
 
2121
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
2122
        TEST_TRUE (WIFEXITED (status));
 
2123
        TEST_EQ (WEXITSTATUS (status), 0);
 
2124
 
 
2125
        TEST_FORCE_WATCH_UPDATE ();
 
2126
 
 
2127
        TEST_EQ (stat (filename, &statbuf), 0);
 
2128
 
 
2129
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
2130
 
 
2131
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
2132
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
2133
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
2134
 
 
2135
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
2136
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
2137
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
2138
 
 
2139
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
2140
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
2141
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
2142
 
 
2143
        output = fopen (filename, "r");
 
2144
        TEST_NE_P (output, NULL);
 
2145
 
 
2146
        /* Yup, pseudo-terminals record *everything*,
 
2147
         * even the carriage returns.
 
2148
         */
 
2149
        TEST_FILE_EQ (output, "hello\r\n");
 
2150
        TEST_FILE_EQ (output, "world\r\n");
 
2151
        TEST_FILE_EQ (output, "\r\n");
 
2152
        TEST_FILE_EQ (output, "\r\n");
 
2153
        TEST_FILE_END (output);
 
2154
        fclose (output);
 
2155
 
 
2156
        TEST_EQ (unlink (filename), 0);
 
2157
        nih_free (class);
 
2158
 
 
2159
        /************************************************************/
 
2160
        TEST_FEATURE ("with single-line command that writes >1 lines to stderr");
 
2161
 
 
2162
        class = job_class_new (NULL, "foo", NULL);
 
2163
        TEST_NE_P (class, NULL);
 
2164
 
 
2165
        TEST_FILENAME (filename);
 
2166
        TEST_GT (sprintf (filename, "%s/foo.log", dirname), 0);
 
2167
 
 
2168
        class->console = CONSOLE_LOG;
 
2169
        class->process[PROCESS_MAIN] = process_new (class);
 
2170
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
2171
                        class->process[PROCESS_MAIN],
 
2172
                        /* XXX: note the required quoting */
 
2173
                        "%s -ne \"hello world\\n\\n\\n\"", TEST_CMD_ECHO);
 
2174
        class->process[PROCESS_MAIN]->script = FALSE;
 
2175
 
 
2176
        job = job_new (class, "");
 
2177
        job->goal = JOB_START;
 
2178
        job->state = JOB_SPAWNED;
 
2179
 
 
2180
        ret = job_process_run (job, PROCESS_MAIN);
 
2181
        TEST_EQ (ret, 0);
 
2182
 
 
2183
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
2184
 
 
2185
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
2186
        TEST_TRUE (WIFEXITED (status));
 
2187
        TEST_EQ (WEXITSTATUS (status), 0);
 
2188
 
 
2189
        TEST_FORCE_WATCH_UPDATE ();
 
2190
 
 
2191
        TEST_EQ (stat (filename, &statbuf), 0);
 
2192
 
 
2193
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
2194
 
 
2195
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
2196
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
2197
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
2198
 
 
2199
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
2200
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
2201
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
2202
 
 
2203
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
2204
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
2205
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
2206
 
 
2207
        output = fopen (filename, "r");
 
2208
        TEST_NE_P (output, NULL);
 
2209
 
 
2210
        TEST_FILE_EQ (output, "hello world\r\n");
 
2211
        TEST_FILE_EQ (output, "\r\n");
 
2212
        TEST_FILE_EQ (output, "\r\n");
 
2213
        TEST_FILE_END (output);
 
2214
        fclose (output);
 
2215
 
 
2216
        TEST_EQ (unlink (filename), 0);
 
2217
        nih_free (class);
 
2218
 
 
2219
        /************************************************************/
 
2220
        TEST_FEATURE ("with multi-line script that writes >1 lines to stderr");
 
2221
 
 
2222
        class = job_class_new (NULL, "elf", NULL);
 
2223
        TEST_NE_P (class, NULL);
 
2224
 
 
2225
        TEST_FILENAME (filename);
 
2226
        TEST_GT (sprintf (filename, "%s/elf.log", dirname), 0);
 
2227
 
 
2228
        class->console = CONSOLE_LOG;
 
2229
        class->process[PROCESS_MAIN] = process_new (class);
 
2230
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
2231
                        class->process[PROCESS_MAIN],
 
2232
                        /* XXX: note the required quoting */
 
2233
                        "/bin/true\n%s -ne \"hello world\\n\\n\" 1>&2\n\n\n", TEST_CMD_ECHO);
 
2234
        class->process[PROCESS_MAIN]->script = TRUE;
 
2235
 
 
2236
        job = job_new (class, "");
 
2237
        job->goal = JOB_START;
 
2238
        job->state = JOB_SPAWNED;
 
2239
 
 
2240
        ret = job_process_run (job, PROCESS_MAIN);
 
2241
        TEST_EQ (ret, 0);
 
2242
 
 
2243
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
2244
 
 
2245
        TEST_FORCE_WATCH_UPDATE ();
 
2246
 
 
2247
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
2248
        TEST_TRUE (WIFEXITED (status));
 
2249
        TEST_EQ (WEXITSTATUS (status), 0);
 
2250
 
 
2251
        TEST_FORCE_WATCH_UPDATE ();
 
2252
 
 
2253
        TEST_EQ (stat (filename, &statbuf), 0);
 
2254
 
 
2255
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
2256
 
 
2257
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
2258
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
2259
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
2260
 
 
2261
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
2262
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
2263
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
2264
 
 
2265
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
2266
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
2267
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
2268
 
 
2269
        output = fopen (filename, "r");
 
2270
        TEST_NE_P (output, NULL);
 
2271
 
 
2272
        TEST_FILE_EQ (output, "hello world\r\n");
 
2273
        TEST_FILE_EQ (output, "\r\n");
 
2274
        TEST_FILE_END (output);
 
2275
        fclose (output);
 
2276
 
 
2277
        TEST_EQ (unlink (filename), 0);
 
2278
        nih_free (class);
 
2279
 
 
2280
        /************************************************************/
 
2281
        TEST_FEATURE ("with single-line script that writes 1 line to stdout then 1 line to stderr");
 
2282
 
 
2283
        class = job_class_new (NULL, "blah", NULL);
 
2284
        TEST_NE_P (class, NULL);
 
2285
 
 
2286
        TEST_FILENAME (filename);
 
2287
        TEST_GT (sprintf (filename, "%s/blah.log", dirname), 0);
 
2288
 
 
2289
        class->console = CONSOLE_LOG;
 
2290
        class->process[PROCESS_MAIN] = process_new (class);
 
2291
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
2292
                        class->process[PROCESS_MAIN],
 
2293
                        /* XXX: note the required quoting */
 
2294
                        "%s stdout;%s stderr >&2",
 
2295
                        TEST_CMD_ECHO, TEST_CMD_ECHO);
 
2296
        class->process[PROCESS_MAIN]->script = TRUE;
 
2297
 
 
2298
        job = job_new (class, "");
 
2299
        job->goal = JOB_START;
 
2300
        job->state = JOB_SPAWNED;
 
2301
 
 
2302
        ret = job_process_run (job, PROCESS_MAIN);
 
2303
        TEST_EQ (ret, 0);
 
2304
 
 
2305
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
2306
 
 
2307
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
2308
        TEST_TRUE (WIFEXITED (status));
 
2309
        TEST_EQ (WEXITSTATUS (status), 0);
 
2310
 
 
2311
        TEST_FORCE_WATCH_UPDATE ();
 
2312
 
 
2313
        TEST_EQ (stat (filename, &statbuf), 0);
 
2314
 
 
2315
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
2316
 
 
2317
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
2318
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
2319
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
2320
 
 
2321
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
2322
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
2323
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
2324
 
 
2325
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
2326
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
2327
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
2328
 
 
2329
        output = fopen (filename, "r");
 
2330
        TEST_NE_P (output, NULL);
 
2331
 
 
2332
        TEST_FILE_EQ (output, "stdout\r\n");
 
2333
        TEST_FILE_EQ (output, "stderr\r\n");
 
2334
        TEST_FILE_END (output);
 
2335
        fclose (output);
 
2336
 
 
2337
        TEST_EQ (unlink (filename), 0);
 
2338
        nih_free (class);
 
2339
 
 
2340
        /************************************************************/
 
2341
        TEST_FEATURE ("with single-line script that writes 1 line to stderr then 1 line to stdout");
 
2342
 
 
2343
        class = job_class_new (NULL, "blah", NULL);
 
2344
        TEST_NE_P (class, NULL);
 
2345
 
 
2346
        TEST_FILENAME (filename);
 
2347
        TEST_GT (sprintf (filename, "%s/blah.log", dirname), 0);
 
2348
 
 
2349
        class->console = CONSOLE_LOG;
 
2350
        class->process[PROCESS_MAIN] = process_new (class);
 
2351
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
2352
                        class->process[PROCESS_MAIN],
 
2353
                        /* XXX: note the required quoting */
 
2354
                        "%s stderr >&2;%s stdout",
 
2355
                        TEST_CMD_ECHO, TEST_CMD_ECHO);
 
2356
        class->process[PROCESS_MAIN]->script = TRUE;
 
2357
 
 
2358
        job = job_new (class, "");
 
2359
        job->goal = JOB_START;
 
2360
        job->state = JOB_SPAWNED;
 
2361
 
 
2362
        ret = job_process_run (job, PROCESS_MAIN);
 
2363
        TEST_EQ (ret, 0);
 
2364
 
 
2365
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
2366
 
 
2367
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
2368
        TEST_TRUE (WIFEXITED (status));
 
2369
        TEST_EQ (WEXITSTATUS (status), 0);
 
2370
 
 
2371
        TEST_FORCE_WATCH_UPDATE ();
 
2372
 
 
2373
        TEST_EQ (stat (filename, &statbuf), 0);
 
2374
 
 
2375
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
2376
 
 
2377
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
2378
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
2379
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
2380
 
 
2381
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
2382
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
2383
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
2384
 
 
2385
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
2386
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
2387
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
2388
 
 
2389
        output = fopen (filename, "r");
 
2390
        TEST_NE_P (output, NULL);
 
2391
 
 
2392
        TEST_FILE_EQ (output, "stderr\r\n");
 
2393
        TEST_FILE_EQ (output, "stdout\r\n");
 
2394
        TEST_FILE_END (output);
 
2395
        fclose (output);
 
2396
 
 
2397
        TEST_EQ (unlink (filename), 0);
 
2398
        nih_free (class);
 
2399
 
 
2400
        /************************************************************/
 
2401
        TEST_FEATURE ("with single-line command that writes to stdout and stderr");
 
2402
 
 
2403
        class = job_class_new (NULL, "blah", NULL);
 
2404
        TEST_NE_P (class, NULL);
 
2405
 
 
2406
        TEST_FILENAME (filename);
 
2407
        TEST_GT (sprintf (filename, "%s/blah.log", dirname), 0);
 
2408
 
 
2409
        class->console = CONSOLE_LOG;
 
2410
        class->process[PROCESS_MAIN] = process_new (class);
 
2411
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
2412
                        class->process[PROCESS_MAIN],
 
2413
                        "%s if=/dev/zero bs=1 count=7", TEST_CMD_DD);
 
2414
        class->process[PROCESS_MAIN]->script = FALSE;
 
2415
 
 
2416
        job = job_new (class, "");
 
2417
        job->goal = JOB_START;
 
2418
        job->state = JOB_SPAWNED;
 
2419
 
 
2420
        ret = job_process_run (job, PROCESS_MAIN);
 
2421
        TEST_EQ (ret, 0);
 
2422
 
 
2423
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
2424
 
 
2425
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
2426
        TEST_TRUE (WIFEXITED (status));
 
2427
        TEST_EQ (WEXITSTATUS (status), 0);
 
2428
 
 
2429
        TEST_FORCE_WATCH_UPDATE ();
 
2430
 
 
2431
        TEST_EQ (stat (filename, &statbuf), 0);
 
2432
 
 
2433
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
2434
 
 
2435
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
2436
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
2437
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
2438
 
 
2439
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
2440
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
2441
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
2442
 
 
2443
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
2444
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
2445
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
2446
 
 
2447
        output = fopen (filename, "r");
 
2448
        TEST_NE_P (output, NULL);
 
2449
 
 
2450
        TEST_NE_P (fgets (filebuf, sizeof(filebuf), output), NULL);
 
2451
        TEST_EQ (memcmp (filebuf, "\000\000\000\000\000\000", 7), 0);
 
2452
        p = filebuf + 7;
 
2453
        TEST_EQ_STR (p, "7+0 records in\r\n");
 
2454
 
 
2455
        TEST_FILE_EQ (output, "7+0 records out\r\n");
 
2456
        TEST_FILE_MATCH (output, "7 bytes (7 B) copied,*\r\n");
 
2457
        TEST_FILE_END (output);
 
2458
        fclose (output);
 
2459
 
 
2460
        TEST_EQ (unlink (filename), 0);
 
2461
        nih_free (class);
 
2462
 
 
2463
        /************************************************************/
 
2464
        TEST_FEATURE ("with single-line script running an invalid command");
 
2465
 
 
2466
        class = job_class_new (NULL, "blah", NULL);
 
2467
        TEST_NE_P (class, NULL);
 
2468
 
 
2469
        TEST_FILENAME (filename);
 
2470
        TEST_GT (sprintf (filename, "%s/blah.log", dirname), 0);
 
2471
 
 
2472
        class->console = CONSOLE_LOG;
 
2473
        class->process[PROCESS_MAIN] = process_new (class);
 
2474
        class->process[PROCESS_MAIN]->command = nih_strdup (
 
2475
                        class->process[PROCESS_MAIN],
 
2476
                        "/this/command/does/not/exist");
 
2477
        class->process[PROCESS_MAIN]->script = TRUE;
 
2478
 
 
2479
        job = job_new (class, "");
 
2480
        job->goal = JOB_START;
 
2481
        job->state = JOB_SPAWNED;
 
2482
 
 
2483
        ret = job_process_run (job, PROCESS_MAIN);
 
2484
        TEST_EQ (ret, 0);
 
2485
 
 
2486
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
2487
 
 
2488
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
2489
        TEST_TRUE (WIFEXITED (status));
 
2490
        TEST_NE (WEXITSTATUS (status), 0);
 
2491
 
 
2492
        TEST_FORCE_WATCH_UPDATE ();
 
2493
 
 
2494
        TEST_EQ (stat (filename, &statbuf), 0);
 
2495
 
 
2496
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
2497
 
 
2498
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
2499
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
2500
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
2501
 
 
2502
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
2503
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
2504
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
2505
 
 
2506
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
2507
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
2508
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
2509
 
 
2510
        output = fopen (filename, "r");
 
2511
        TEST_NE_P (output, NULL);
 
2512
 
 
2513
        TEST_FILE_EQ (output, "/bin/sh: 1: /this/command/does/not/exist: not found\r\n");
 
2514
        TEST_FILE_END (output);
 
2515
        fclose (output);
 
2516
 
 
2517
        TEST_EQ (unlink (filename), 0);
 
2518
        nih_free (class);
 
2519
 
 
2520
        /************************************************************
 
2521
         * No point in running a test for:
 
2522
         *
 
2523
         *   TEST_FEATURE ("with single-line command running an invalid command");
 
2524
         *
 
2525
         * Since as such commands are exec'ed directly, there is no shell to report
 
2526
         * an error back - exec just fails.
 
2527
         *
 
2528
         ************************************************************/
 
2529
 
 
2530
        /************************************************************/
 
2531
        TEST_FEATURE ("with multi-line script running an invalid command");
 
2532
 
 
2533
        class = job_class_new (NULL, "blah", NULL);
 
2534
        TEST_NE_P (class, NULL);
 
2535
 
 
2536
        TEST_FILENAME (filename);
 
2537
        TEST_GT (sprintf (filename, "%s/blah.log", dirname), 0);
 
2538
 
 
2539
        class->console = CONSOLE_LOG;
 
2540
        class->process[PROCESS_MAIN] = process_new (class);
 
2541
        class->process[PROCESS_MAIN]->command = nih_strdup (
 
2542
                        class->process[PROCESS_MAIN],
 
2543
                        "true\n/this/command/does/not/exist");
 
2544
        class->process[PROCESS_MAIN]->script = TRUE;
 
2545
 
 
2546
        job = job_new (class, "");
 
2547
        job->goal = JOB_START;
 
2548
        job->state = JOB_SPAWNED;
 
2549
 
 
2550
        ret = job_process_run (job, PROCESS_MAIN);
 
2551
        TEST_EQ (ret, 0);
 
2552
 
 
2553
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
2554
 
 
2555
        TEST_FORCE_WATCH_UPDATE ();
 
2556
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
2557
        TEST_TRUE (WIFEXITED (status));
 
2558
        TEST_NE (WEXITSTATUS (status), 0);
 
2559
        TEST_FORCE_WATCH_UPDATE ();
 
2560
 
 
2561
        TEST_EQ (stat (filename, &statbuf), 0);
 
2562
 
 
2563
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
2564
 
 
2565
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
2566
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
2567
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
2568
 
 
2569
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
2570
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
2571
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
2572
 
 
2573
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
2574
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
2575
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
2576
 
 
2577
        output = fopen (filename, "r");
 
2578
        TEST_NE_P (output, NULL);
 
2579
 
 
2580
        TEST_FILE_EQ (output, "/proc/self/fd/9: 3: /this/command/does/not/exist: not found\r\n");
 
2581
        TEST_FILE_END (output);
 
2582
        fclose (output);
 
2583
 
 
2584
        TEST_EQ (unlink (filename), 0);
 
2585
        nih_free (class);
 
2586
 
 
2587
        /************************************************************/
 
2588
        TEST_FEATURE ("with multi-line script that writes 1 line to stdout then 1 line to stderr");
 
2589
 
 
2590
        class = job_class_new (NULL, "blah", NULL);
 
2591
        TEST_NE_P (class, NULL);
 
2592
 
 
2593
        TEST_FILENAME (filename);
 
2594
        TEST_GT (sprintf (filename, "%s/blah.log", dirname), 0);
 
2595
 
 
2596
        class->console = CONSOLE_LOG;
 
2597
        class->process[PROCESS_MAIN] = process_new (class);
 
2598
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
2599
                        class->process[PROCESS_MAIN],
 
2600
                        "%s stdout\n%s stderr >&2\n",
 
2601
                        TEST_CMD_ECHO, TEST_CMD_ECHO);
 
2602
        class->process[PROCESS_MAIN]->script = TRUE;
 
2603
 
 
2604
        job = job_new (class, "");
 
2605
        job->goal = JOB_START;
 
2606
        job->state = JOB_SPAWNED;
 
2607
 
 
2608
        ret = job_process_run (job, PROCESS_MAIN);
 
2609
        TEST_EQ (ret, 0);
 
2610
 
 
2611
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
2612
 
 
2613
        TEST_FORCE_WATCH_UPDATE ();
 
2614
 
 
2615
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
2616
        TEST_TRUE (WIFEXITED (status));
 
2617
        TEST_EQ (WEXITSTATUS (status), 0);
 
2618
 
 
2619
        TEST_FORCE_WATCH_UPDATE ();
 
2620
 
 
2621
        TEST_EQ (stat (filename, &statbuf), 0);
 
2622
 
 
2623
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
2624
 
 
2625
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
2626
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
2627
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
2628
 
 
2629
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
2630
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
2631
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
2632
 
 
2633
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
2634
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
2635
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
2636
 
 
2637
        output = fopen (filename, "r");
 
2638
        TEST_NE_P (output, NULL);
 
2639
 
 
2640
        TEST_FILE_EQ (output, "stdout\r\n");
 
2641
        TEST_FILE_EQ (output, "stderr\r\n");
 
2642
        TEST_FILE_END (output);
 
2643
        fclose (output);
 
2644
 
 
2645
        TEST_EQ (unlink (filename), 0);
 
2646
        nih_free (class);
 
2647
 
 
2648
        /************************************************************/
 
2649
        TEST_FEATURE ("with multi-line script that writes 1 line to stderr then 1 line to stdout");
 
2650
 
 
2651
        class = job_class_new (NULL, "blah", NULL);
 
2652
        TEST_NE_P (class, NULL);
 
2653
 
 
2654
        TEST_FILENAME (filename);
 
2655
        TEST_GT (sprintf (filename, "%s/blah.log", dirname), 0);
 
2656
 
 
2657
        class->console = CONSOLE_LOG;
 
2658
        class->process[PROCESS_MAIN] = process_new (class);
 
2659
        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
2660
                        class->process[PROCESS_MAIN],
 
2661
                        "%s stderr >&2\n%s stdout\n",
 
2662
                        TEST_CMD_ECHO, TEST_CMD_ECHO);
 
2663
        class->process[PROCESS_MAIN]->script = TRUE;
 
2664
 
 
2665
        job = job_new (class, "");
 
2666
        job->goal = JOB_START;
 
2667
        job->state = JOB_SPAWNED;
 
2668
 
 
2669
        ret = job_process_run (job, PROCESS_MAIN);
 
2670
        TEST_EQ (ret, 0);
 
2671
 
 
2672
        TEST_NE (job->pid[PROCESS_MAIN], 0);
 
2673
 
 
2674
        TEST_FORCE_WATCH_UPDATE ();
 
2675
 
 
2676
        waitpid (job->pid[PROCESS_MAIN], &status, 0);
 
2677
        TEST_TRUE (WIFEXITED (status));
 
2678
        TEST_EQ (WEXITSTATUS (status), 0);
 
2679
 
 
2680
        TEST_FORCE_WATCH_UPDATE ();
 
2681
 
 
2682
        TEST_EQ (stat (filename, &statbuf), 0);
 
2683
 
 
2684
        TEST_TRUE (S_ISREG (statbuf.st_mode));
 
2685
 
 
2686
        TEST_TRUE  (statbuf.st_mode & S_IRUSR);
 
2687
        TEST_TRUE  (statbuf.st_mode & S_IWUSR);
 
2688
        TEST_FALSE (statbuf.st_mode & S_IXUSR);
 
2689
 
 
2690
        TEST_TRUE  (statbuf.st_mode & S_IRGRP);
 
2691
        TEST_FALSE (statbuf.st_mode & S_IWGRP);
 
2692
        TEST_FALSE (statbuf.st_mode & S_IXGRP);
 
2693
 
 
2694
        TEST_FALSE (statbuf.st_mode & S_IROTH);
 
2695
        TEST_FALSE (statbuf.st_mode & S_IWOTH);
 
2696
        TEST_FALSE (statbuf.st_mode & S_IXOTH);
 
2697
 
 
2698
        output = fopen (filename, "r");
 
2699
        TEST_NE_P (output, NULL);
 
2700
 
 
2701
        TEST_FILE_EQ (output, "stderr\r\n");
 
2702
        TEST_FILE_EQ (output, "stdout\r\n");
 
2703
        TEST_FILE_END (output);
 
2704
        fclose (output);
 
2705
 
 
2706
        TEST_EQ (unlink (filename), 0);
 
2707
        nih_free (class);
 
2708
 
 
2709
        /* Check that we can succesfully setuid and setgid to
 
2710
         * ourselves. This should always work, privileged or
 
2711
         * otherwise.
 
2712
         */
 
2713
        TEST_FEATURE ("with setuid me");
 
2714
        TEST_ALLOC_FAIL {
 
2715
                TEST_ALLOC_SAFE {
 
2716
                        class = job_class_new (NULL, "test", NULL);
 
2717
                        class->process[PROCESS_MAIN] = process_new (class);
 
2718
                        class->process[PROCESS_MAIN]->command = nih_sprintf (
 
2719
                                class->process[PROCESS_MAIN],
 
2720
                                "touch %s", filename);
 
2721
 
 
2722
                        pwd = getpwuid (getuid ());
 
2723
                        TEST_NE (pwd, NULL);
 
2724
                        class->setuid = nih_strdup (class, pwd->pw_name);
 
2725
 
 
2726
                        grp = getgrgid (getgid ());
 
2727
                        TEST_NE (grp, NULL);
 
2728
                        class->setuid = nih_strdup (class, grp->gr_name);
 
2729
 
 
2730
                        job = job_new (class, "");
 
2731
                        job->goal = JOB_START;
 
2732
                        job->state = JOB_SPAWNED;
 
2733
                }
 
2734
 
 
2735
                ret = job_process_run (job, PROCESS_MAIN);
 
2736
                TEST_EQ (ret, 0);
 
2737
 
 
2738
                TEST_NE (job->pid[PROCESS_MAIN], 0);
 
2739
 
 
2740
                waitpid (job->pid[PROCESS_MAIN], NULL, 0);
 
2741
                TEST_EQ (stat (filename, &statbuf), 0);
 
2742
 
 
2743
                unlink (filename);
 
2744
                nih_free (class);
 
2745
        }
 
2746
 
 
2747
        /************************************************************/
 
2748
        /* Final clean-up */
 
2749
 
 
2750
        TEST_EQ (rmdir (dirname), 0);
 
2751
        TEST_EQ (unsetenv ("UPSTART_LOGDIR"), 0);
790
2752
}
791
2753
 
792
2754
 
 
2755
/* 
 
2756
 * XXX: Note that none of these tests attempts to test with a Session
 
2757
 * passed to job_class_new() since to do so would modify the home
 
2758
 * directory of the user running these tests (BAD!!).
 
2759
 *
 
2760
 * (Such tests are handled in the bundled test_user_sessions.sh script).
 
2761
 */
793
2762
void
794
2763
test_spawn (void)
795
2764
{
796
 
        FILE             *output;
 
2765
        FILE             *output, *input;
797
2766
        char              function[PATH_MAX], filename[PATH_MAX];
 
2767
        char              dirname[PATH_MAX];
 
2768
        char              script[PATH_MAX];
798
2769
        char              buf[80];
799
 
        char             *env[3], *args[4];
 
2770
        char              filebuf[1024];
 
2771
        char             *args[6];
 
2772
        char             *env[3];
 
2773
        nih_local char  **args_array = NULL;
 
2774
        size_t            argc;
800
2775
        JobClass         *class;
 
2776
        Job              *job;
801
2777
        pid_t             pid;
802
2778
        siginfo_t         info;
803
2779
        NihError         *err;
820
2796
        sprintf (function, "%d", TEST_PIDS);
821
2797
 
822
2798
        class = job_class_new (NULL, "test", NULL);
 
2799
        class->console = CONSOLE_NONE;
 
2800
        job   = job_new (class, "");
823
2801
 
824
 
        pid = job_process_spawn (class, args, NULL, FALSE, -1);
 
2802
        pid = job_process_spawn (job, args, NULL, FALSE, -1);
825
2803
        TEST_GT (pid, 0);
826
2804
 
827
2805
        waitpid (pid, NULL, 0);
858
2836
 
859
2837
        class = job_class_new (NULL, "test", NULL);
860
2838
        class->console = CONSOLE_NONE;
 
2839
        job = job_new (class, "");
861
2840
 
862
 
        pid = job_process_spawn (class, args, NULL, FALSE, -1);
 
2841
        pid = job_process_spawn (job, args, NULL, FALSE, -1);
863
2842
        TEST_GT (pid, 0);
864
2843
 
865
2844
        waitpid (pid, NULL, 0);
875
2854
 
876
2855
        nih_free (class);
877
2856
 
 
2857
        /* Check that a job spawned with a log console has file descriptors:
 
2858
         *
 
2859
         * 0 bound to the /dev/null device.
 
2860
         * 1 bound to the pseudo-tty device.
 
2861
         * 2 bound to the pseudo-tty device.
 
2862
         *
 
2863
         */
 
2864
        TEST_FEATURE ("with console logging");
 
2865
        sprintf (function, "%d", TEST_CONSOLE);
 
2866
 
 
2867
        class = job_class_new (NULL, "test", NULL);
 
2868
        class->console = CONSOLE_LOG;
 
2869
        job = job_new (class, "");
 
2870
 
 
2871
        pid = job_process_spawn (job, args, NULL, FALSE, -1);
 
2872
        TEST_GT (pid, 0);
 
2873
 
 
2874
        waitpid (pid, NULL, 0);
 
2875
        output = fopen (filename, "r");
 
2876
 
 
2877
        /* /dev/null */
 
2878
        TEST_FILE_EQ (output, "0: 1 3\n");
 
2879
 
 
2880
        /* stdout and stderr should be bound to the same Unix98 PTY slave
 
2881
         * device (one of the char devices in range 136-143).
 
2882
         * We ignore the minor as it could be any value.
 
2883
         */
 
2884
        {
 
2885
                unsigned int major, saved_major;
 
2886
                unsigned int unused;
 
2887
 
 
2888
                TEST_EQ (fscanf (output, "1: %u %u\n", &major, &unused), 2);
 
2889
                TEST_TRUE (major >= 136 && major <= 143);
 
2890
                saved_major = major;
 
2891
 
 
2892
                TEST_EQ (fscanf (output, "2: %u %u\n", &major, &unused), 2);
 
2893
                TEST_TRUE (major == saved_major);
 
2894
        }
 
2895
 
 
2896
 
 
2897
        TEST_FILE_END (output);
 
2898
 
 
2899
        fclose (output);
 
2900
        unlink (filename);
 
2901
 
 
2902
        nih_free (class);
 
2903
 
878
2904
 
879
2905
        /* Check that a job with an alternate working directory is run from
880
2906
         * that directory.
883
2909
        sprintf (function, "%d", TEST_PWD);
884
2910
 
885
2911
        class = job_class_new (NULL, "test", NULL);
 
2912
        class->console = CONSOLE_NONE;
886
2913
        class->chdir = "/tmp";
 
2914
        job = job_new (class, "");
887
2915
 
888
 
        pid = job_process_spawn (class, args, NULL, FALSE, -1);
 
2916
        pid = job_process_spawn (job, args, NULL, FALSE, -1);
889
2917
        TEST_GT (pid, 0);
890
2918
 
891
2919
        waitpid (pid, NULL, 0);
912
2940
        env[2] = NULL;
913
2941
 
914
2942
        class = job_class_new (NULL, "test", NULL);
 
2943
        class->console = CONSOLE_NONE;
 
2944
        job   = job_new (class, "");
915
2945
 
916
 
        pid = job_process_spawn (class, args, env, FALSE, -1);
 
2946
        pid = job_process_spawn (job, args, env, FALSE, -1);
917
2947
        TEST_GT (pid, 0);
918
2948
 
919
2949
        waitpid (pid, NULL, 0);
921
2951
 
922
2952
        TEST_FILE_EQ (output, "PATH=/bin\n");
923
2953
        TEST_FILE_EQ (output, "FOO=bar\n");
 
2954
        TEST_FILE_EQ (output, "UPSTART_NO_SESSIONS=1\n");
924
2955
        TEST_FILE_END (output);
925
2956
 
926
2957
        fclose (output);
937
2968
        sprintf (function, "%d", TEST_SIMPLE);
938
2969
 
939
2970
        class = job_class_new (NULL, "test", NULL);
 
2971
        class->console = CONSOLE_NONE;
 
2972
        job   = job_new (class, "");
940
2973
 
941
 
        pid = job_process_spawn (class, args, NULL, FALSE, -1);
 
2974
        pid = job_process_spawn (job, args, NULL, FALSE, -1);
942
2975
        TEST_GT (pid, 0);
943
2976
 
944
2977
        assert0 (waitid (P_PID, pid, &info, WEXITED | WSTOPPED | WCONTINUED));
957
2990
        sprintf (function, "%d", TEST_SIMPLE);
958
2991
 
959
2992
        class = job_class_new (NULL, "test", NULL);
960
 
 
961
 
        pid = job_process_spawn (class, args, NULL, TRUE, -1);
 
2993
        job   = job_new (class, "");
 
2994
        class->console = CONSOLE_NONE;
 
2995
        pid = job_process_spawn (job, args, NULL, TRUE, -1);
962
2996
        TEST_GT (pid, 0);
963
2997
 
964
2998
        assert0 (waitid (P_PID, pid, &info, WEXITED | WSTOPPED | WCONTINUED));
986
3020
        args[2] = NULL;
987
3021
 
988
3022
        class = job_class_new (NULL, "test", NULL);
 
3023
        class->console = CONSOLE_NONE;
 
3024
        job   = job_new (class, "");
989
3025
 
990
 
        pid = job_process_spawn (class, args, NULL, FALSE, -1);
 
3026
        pid = job_process_spawn (job, args, NULL, FALSE, -1);
991
3027
        TEST_LT (pid, 0);
992
3028
 
993
3029
        err = nih_error_get ();
1005
3041
        TEST_FEATURE ("with debug enabled");
1006
3042
 
1007
3043
        class = job_class_new (NULL, "test", NULL);
 
3044
        class->console = CONSOLE_NONE;
1008
3045
        class->debug = TRUE;
 
3046
        job = job_new (class, "");
1009
3047
 
1010
3048
        sprintf (function, "%s", "/bin/true");
1011
3049
        args[0] = function;
1012
3050
        args[1] = function;
1013
3051
        args[2] = NULL;
1014
3052
 
1015
 
        pid = job_process_spawn (class, args, NULL, FALSE, -1);
 
3053
        pid = job_process_spawn (job, args, NULL, FALSE, -1);
1016
3054
        TEST_GT (pid, 0);
1017
3055
 
1018
3056
        /* Ensure process is still running after some period of time.
1033
3071
        TEST_EQ (WEXITSTATUS (status), 0);
1034
3072
 
1035
3073
        nih_free (class);
 
3074
 
 
3075
        /* Check that when the job process is execed that no unexpected
 
3076
         * signals are blocked or ignored.
 
3077
         */
 
3078
        TEST_FEATURE ("ensure sane signal state with no console");
 
3079
 
 
3080
        sprintf (function, "%d", TEST_SIGNALS);
 
3081
 
 
3082
        args[0] = argv0;
 
3083
        args[1] = function;
 
3084
        args[2] = filename;
 
3085
        args[3] = NULL;
 
3086
 
 
3087
        class = job_class_new (NULL, "test", NULL);
 
3088
        class->console = CONSOLE_NONE;
 
3089
        job = job_new (class, "");
 
3090
 
 
3091
        pid = job_process_spawn (job, args, NULL, FALSE, -1);
 
3092
        TEST_GT (pid, 0);
 
3093
 
 
3094
        waitpid (pid, NULL, 0);
 
3095
        output = fopen (filename, "r");
 
3096
 
 
3097
        TEST_NE_P (output, NULL);
 
3098
 
 
3099
        {
 
3100
                unsigned long int value;
 
3101
 
 
3102
                /* No signals should be blocked */
 
3103
                TEST_TRUE (fgets (filebuf, sizeof(filebuf), output));
 
3104
                TEST_EQ (sscanf (filebuf, "SigBlk: %lx", &value), 1);
 
3105
                TEST_EQ (value, 0x0);
 
3106
 
 
3107
                /* No signals should be ignored */
 
3108
                TEST_TRUE (fgets (filebuf, sizeof(filebuf), output));
 
3109
                TEST_EQ (sscanf (filebuf, "SigIgn: %lx", &value), 1);
 
3110
                TEST_EQ (value, 0x0);
 
3111
 
 
3112
                TEST_FILE_END (output);
 
3113
        }
 
3114
 
 
3115
        fclose (output);
 
3116
        unlink (filename);
 
3117
 
 
3118
        nih_free (class);
 
3119
 
 
3120
        TEST_FEATURE ("ensure sane signal state with log console");
 
3121
 
 
3122
        sprintf (function, "%d", TEST_SIGNALS);
 
3123
 
 
3124
        args[0] = argv0;
 
3125
        args[1] = function;
 
3126
        args[2] = filename;
 
3127
        args[3] = NULL;
 
3128
 
 
3129
        class = job_class_new (NULL, "test", NULL);
 
3130
        class->console = CONSOLE_LOG;
 
3131
        job = job_new (class, "");
 
3132
 
 
3133
        pid = job_process_spawn (job, args, NULL, FALSE, -1);
 
3134
        TEST_GT (pid, 0);
 
3135
 
 
3136
        waitpid (pid, NULL, 0);
 
3137
        output = fopen (filename, "r");
 
3138
 
 
3139
        TEST_NE_P (output, NULL);
 
3140
 
 
3141
        {
 
3142
                unsigned long int value;
 
3143
 
 
3144
                /* No signals should be blocked */
 
3145
                TEST_TRUE (fgets (filebuf, sizeof(filebuf), output));
 
3146
                TEST_EQ (sscanf (filebuf, "SigBlk: %lx", &value), 1);
 
3147
                TEST_EQ (value, 0x0);
 
3148
 
 
3149
                /* No signals should be ignored */
 
3150
                TEST_TRUE (fgets (filebuf, sizeof(filebuf), output));
 
3151
                TEST_EQ (sscanf (filebuf, "SigIgn: %lx", &value), 1);
 
3152
                TEST_EQ (value, 0x0);
 
3153
 
 
3154
                TEST_FILE_END (output);
 
3155
        }
 
3156
 
 
3157
        fclose (output);
 
3158
        unlink (filename);
 
3159
 
 
3160
        nih_free (class);
 
3161
 
 
3162
        /************************************************************/
 
3163
        TEST_FEATURE ("simple test");
 
3164
 
 
3165
        TEST_FILENAME (dirname);       
 
3166
        umask(0);
 
3167
        TEST_EQ (mkdir (dirname, 0755), 0);
 
3168
 
 
3169
        TEST_EQ (setenv ("UPSTART_LOGDIR", dirname, 1), 0);
 
3170
        TEST_ALLOC_FAIL {
 
3171
                TEST_ALLOC_SAFE {
 
3172
                        class = job_class_new (NULL, "test", NULL);
 
3173
                        TEST_NE_P (class, NULL);
 
3174
 
 
3175
                        TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
 
3176
                        job = job_new (class, "");
 
3177
                        TEST_NE_P (job, NULL);
 
3178
 
 
3179
                        argc = 0;
 
3180
                        args_array = NIH_MUST (nih_str_array_new (NULL));
 
3181
 
 
3182
                        TEST_FILENAME (script);
 
3183
                        input = fopen (script, "w");
 
3184
                        TEST_NE_P (input, NULL);
 
3185
                        TEST_GT (fprintf (input, "%s hello world\n", TEST_CMD_ECHO), 0);
 
3186
                        fclose (input);
 
3187
 
 
3188
                        NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, TEST_SHELL));
 
3189
                        NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, TEST_SHELL_ARG));
 
3190
                        NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, script));
 
3191
                }
 
3192
 
 
3193
                pid = job_process_spawn (job, args_array, NULL, FALSE, -1);
 
3194
 
 
3195
                if (test_alloc_failed) {
 
3196
                        err = nih_error_get ();
 
3197
                        TEST_NE_P (err, NULL);
 
3198
                        TEST_EQ (err->number, ENOMEM);
 
3199
                        nih_free (err);
 
3200
                        TEST_LT (pid, 0);
 
3201
                } else {
 
3202
                        TEST_GT (pid, 0);
 
3203
                }
 
3204
        }
 
3205
 
 
3206
        /************************************************************/
 
3207
        TEST_FEATURE ("with single-line script and 'console log'");
 
3208
 
 
3209
        /* Check that we can spawn a job and retrieve its output.
 
3210
         */
 
3211
 
 
3212
        TEST_FILENAME (dirname);       
 
3213
        umask(0);
 
3214
        TEST_EQ (mkdir (dirname, 0755), 0);
 
3215
 
 
3216
        /* Override default location to ensure job output goes to a
 
3217
         * writeable location
 
3218
         */
 
3219
        TEST_EQ (setenv ("UPSTART_LOGDIR", dirname, 1), 0);
 
3220
 
 
3221
        class = job_class_new (NULL, "test", NULL);
 
3222
        TEST_NE_P (class, NULL);
 
3223
 
 
3224
        TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
 
3225
        job = job_new (class, "");
 
3226
        TEST_NE_P (job, NULL);
 
3227
 
 
3228
        argc = 0;
 
3229
        args_array = NIH_MUST (nih_str_array_new (NULL));
 
3230
        
 
3231
        TEST_FILENAME (script);       
 
3232
        input = fopen (script, "w");
 
3233
        TEST_NE_P (input, NULL);
 
3234
        TEST_GT (fprintf (input, "%s hello world\n", TEST_CMD_ECHO), 0);
 
3235
        fclose (input);
 
3236
 
 
3237
        NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, TEST_SHELL));
 
3238
        NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, TEST_SHELL_ARG));
 
3239
        NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, script));
 
3240
 
 
3241
        pid = job_process_spawn (job, args_array, NULL, FALSE, -1);
 
3242
        TEST_GT (pid, 0);
 
3243
 
 
3244
        TEST_EQ (waitpid (pid, &status, 0), pid);
 
3245
        TEST_TRUE (WIFEXITED (status));
 
3246
 
 
3247
        TEST_FORCE_WATCH_UPDATE ();
 
3248
 
 
3249
        output = fopen (filename, "r");
 
3250
        TEST_NE_P (output, NULL);
 
3251
 
 
3252
        TEST_FILE_EQ (output, "hello world\r\n");
 
3253
        TEST_FILE_END (output);
 
3254
 
 
3255
        TEST_EQ (fclose (output), 0);
 
3256
 
 
3257
        unlink (filename);
 
3258
 
 
3259
        TEST_EQ (rmdir (dirname), 0);
 
3260
        TEST_EQ (unsetenv ("UPSTART_LOGDIR"), 0);
 
3261
 
 
3262
        nih_free (job);
 
3263
 
 
3264
        /************************************************************/
 
3265
        TEST_FEATURE ("with multi-line script and 'console log'");
 
3266
 
 
3267
        /* Check that we can spawn a job and retrieve its output.
 
3268
         */
 
3269
        TEST_FILENAME (dirname);       
 
3270
        umask(0);
 
3271
        TEST_EQ (mkdir (dirname, 0755), 0);
 
3272
 
 
3273
        TEST_EQ (setenv ("UPSTART_LOGDIR", dirname, 1), 0);
 
3274
 
 
3275
        class = job_class_new (NULL, "test", NULL);
 
3276
        TEST_NE_P (class, NULL);
 
3277
 
 
3278
        TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
 
3279
        job = job_new (class, "");
 
3280
        TEST_NE_P (job, NULL);
 
3281
 
 
3282
        argc = 0;
 
3283
        args_array = NIH_MUST (nih_str_array_new (NULL));
 
3284
        
 
3285
        TEST_FILENAME (script);       
 
3286
        input = fopen (script, "w");
 
3287
        TEST_NE_P (input, NULL);
 
3288
        TEST_GT (fprintf (input, "/bin/true\n%s hello world\n", TEST_CMD_ECHO), 0);
 
3289
        fclose (input);
 
3290
 
 
3291
        NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, TEST_SHELL));
 
3292
        NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, TEST_SHELL_ARG));
 
3293
        NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, script));
 
3294
 
 
3295
        pid = job_process_spawn (job, args_array, NULL, FALSE, -1);
 
3296
        TEST_GT (pid, 0);
 
3297
 
 
3298
        TEST_EQ (waitpid (pid, &status, 0), pid);
 
3299
        TEST_TRUE (WIFEXITED (status));
 
3300
 
 
3301
        TEST_FORCE_WATCH_UPDATE ();
 
3302
 
 
3303
        output = fopen (filename, "r");
 
3304
        TEST_NE_P (output, NULL);
 
3305
 
 
3306
        TEST_FILE_EQ (output, "hello world\r\n");
 
3307
        TEST_FILE_END (output);
 
3308
 
 
3309
        TEST_EQ (fclose (output), 0);
 
3310
 
 
3311
        unlink (filename);
 
3312
 
 
3313
        TEST_EQ (rmdir (dirname), 0);
 
3314
        TEST_EQ (unsetenv ("UPSTART_LOGDIR"), 0);
 
3315
 
 
3316
        nih_free (job);
 
3317
 
 
3318
        /************************************************************/
 
3319
        TEST_FEATURE ("read single null byte with 'console log'");
 
3320
 
 
3321
        /* Check that we can spawn a job and read a single byte written
 
3322
         * to stdout.
 
3323
         */
 
3324
 
 
3325
        TEST_FILENAME (dirname);       
 
3326
        umask(0);
 
3327
        TEST_EQ (mkdir (dirname, 0755), 0);
 
3328
 
 
3329
        TEST_EQ (setenv ("UPSTART_LOGDIR", dirname, 1), 0);
 
3330
 
 
3331
        class = job_class_new (NULL, "test", NULL);
 
3332
        TEST_NE_P (class, NULL);
 
3333
 
 
3334
        TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
 
3335
        job = job_new (class, "");
 
3336
        TEST_NE_P (job, NULL);
 
3337
 
 
3338
        argc = 0;
 
3339
        args_array = NIH_MUST (nih_str_array_new (NULL));
 
3340
 
 
3341
        NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, TEST_CMD_ECHO));
 
3342
        NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, "-en"));
 
3343
        NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, "\\000"));
 
3344
 
 
3345
        pid = job_process_spawn (job, args_array, NULL, FALSE, -1);
 
3346
        TEST_GT (pid, 0);
 
3347
 
 
3348
        TEST_EQ (waitpid (pid, &status, 0), pid);
 
3349
        TEST_TRUE (WIFEXITED (status));
 
3350
 
 
3351
        TEST_FORCE_WATCH_UPDATE ();
 
3352
 
 
3353
        output = fopen (filename, "r");
 
3354
        TEST_NE_P (output, NULL);
 
3355
 
 
3356
        TEST_NE_P (fgets (filebuf, sizeof(filebuf), output), NULL);
 
3357
        TEST_EQ (memcmp (filebuf, "\000", 1), 0);
 
3358
        TEST_FILE_END (output);
 
3359
 
 
3360
        TEST_EQ (fclose (output), 0);
 
3361
 
 
3362
        unlink (filename);
 
3363
 
 
3364
        TEST_EQ (rmdir (dirname), 0);
 
3365
        TEST_EQ (unsetenv ("UPSTART_LOGDIR"), 0);
 
3366
 
 
3367
        nih_free (job);
 
3368
 
 
3369
        /************************************************************/
 
3370
        TEST_FEATURE ("read data from forked process");
 
3371
 
 
3372
        TEST_FILENAME (dirname);
 
3373
        umask(0);
 
3374
        TEST_EQ (mkdir (dirname, 0755), 0);
 
3375
 
 
3376
        TEST_EQ (setenv ("UPSTART_LOGDIR", dirname, 1), 0);
 
3377
 
 
3378
        class = job_class_new (NULL, "test", NULL);
 
3379
        TEST_NE_P (class, NULL);
 
3380
 
 
3381
        TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
 
3382
        job = job_new (class, "");
 
3383
        TEST_NE_P (job, NULL);
 
3384
 
 
3385
        sprintf (function, "%d", TEST_OUTPUT);
 
3386
 
 
3387
        /* fork */
 
3388
        sprintf (filebuf, "%d", 1);
 
3389
        TEST_FILENAME (script);
 
3390
 
 
3391
        args[0] = argv0;
 
3392
        args[1] = function;
 
3393
        args[2] = script;
 
3394
        args[3] = filebuf;
 
3395
        args[4] = NULL;
 
3396
 
 
3397
        pid = job_process_spawn (job, args, NULL, FALSE, -1);
 
3398
        TEST_GT (pid, 0);
 
3399
 
 
3400
        TEST_EQ (waitpid (pid, &status, 0), pid);
 
3401
        TEST_TRUE (WIFEXITED (status));
 
3402
 
 
3403
        TEST_FORCE_WATCH_UPDATE ();
 
3404
 
 
3405
        output = fopen (filename, "r");
 
3406
        TEST_NE_P (output, NULL);
 
3407
 
 
3408
        TEST_FILE_EQ (output, "stdout\r\n");
 
3409
        TEST_FILE_EQ (output, "stderr\r\n");
 
3410
        TEST_FILE_END (output);
 
3411
 
 
3412
        TEST_EQ (fclose (output), 0);
 
3413
 
 
3414
        unlink (filename);
 
3415
 
 
3416
        TEST_EQ (rmdir (dirname), 0);
 
3417
        TEST_EQ (unsetenv ("UPSTART_LOGDIR"), 0);
 
3418
 
 
3419
        nih_free (job);
 
3420
 
 
3421
        /************************************************************/
 
3422
        TEST_FEATURE ("read data from daemon process");
 
3423
 
 
3424
        TEST_FILENAME (dirname);       
 
3425
        umask(0);
 
3426
        TEST_EQ (mkdir (dirname, 0755), 0);
 
3427
 
 
3428
        TEST_EQ (setenv ("UPSTART_LOGDIR", dirname, 1), 0);
 
3429
 
 
3430
        class = job_class_new (NULL, "test", NULL);
 
3431
        TEST_NE_P (class, NULL);
 
3432
 
 
3433
        TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
 
3434
        job = job_new (class, "");
 
3435
        TEST_NE_P (job, NULL);
 
3436
 
 
3437
        sprintf (function, "%d", TEST_OUTPUT);
 
3438
 
 
3439
        /* daemonize */
 
3440
        sprintf (filebuf, "%d", 2);
 
3441
        TEST_FILENAME (script);
 
3442
 
 
3443
        args[0] = argv0;
 
3444
        args[1] = function;
 
3445
        args[2] = script;
 
3446
        args[3] = filebuf;
 
3447
        args[4] = NULL;
 
3448
 
 
3449
        pid = job_process_spawn (job, args, NULL, FALSE, -1);
 
3450
        TEST_GT (pid, 0);
 
3451
 
 
3452
        TEST_EQ (waitpid (pid, &status, 0), pid);
 
3453
        TEST_TRUE (WIFEXITED (status));
 
3454
 
 
3455
        TEST_FORCE_WATCH_UPDATE ();
 
3456
 
 
3457
        output = fopen (filename, "r");
 
3458
        TEST_NE_P (output, NULL);
 
3459
 
 
3460
        TEST_FILE_EQ (output, "stdout\r\n");
 
3461
        TEST_FILE_EQ (output, "stderr\r\n");
 
3462
        TEST_FILE_END (output);
 
3463
 
 
3464
        TEST_EQ (fclose (output), 0);
 
3465
 
 
3466
        unlink (filename);
 
3467
 
 
3468
        TEST_EQ (rmdir (dirname), 0);
 
3469
        TEST_EQ (unsetenv ("UPSTART_LOGDIR"), 0);
 
3470
 
 
3471
        nih_free (job);
 
3472
 
 
3473
 
 
3474
        /* FIXME */
 
3475
#if 0
 
3476
        /************************************************************/
 
3477
        TEST_FEATURE ("when no free ptys");
 
3478
        {
 
3479
                int            available_ptys;
 
3480
                int            ret;
 
3481
                struct rlimit  rlimit;
 
3482
 
 
3483
                TEST_FILENAME (dirname);       
 
3484
                umask(0);
 
3485
                TEST_EQ (mkdir (dirname, 0755), 0);
 
3486
 
 
3487
                TEST_EQ (setenv ("UPSTART_LOGDIR", dirname, 1), 0);
 
3488
 
 
3489
                class = job_class_new (NULL, "test", NULL);
 
3490
                TEST_NE_P (class, NULL);
 
3491
                TEST_EQ (class->console, CONSOLE_LOG);
 
3492
 
 
3493
                TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
 
3494
                job = job_new (class, "");
 
3495
                TEST_NE_P (job, NULL);
 
3496
 
 
3497
                available_ptys = get_available_pty_count ();
 
3498
                TEST_GT (available_ptys, 1);
 
3499
                
 
3500
                TEST_EQ (getrlimit (RLIMIT_NOFILE, &rlimit), 0);
 
3501
                
 
3502
                if ((unsigned int)rlimit.rlim_cur <= (unsigned int)available_ptys) {
 
3503
                        /* Since we do not run as root, we are unable to
 
3504
                         * raise our limit to allow us to consume all
 
3505
                         * remaining ptys.
 
3506
                         */
 
3507
                        printf ("WARNING:\n");
 
3508
                        printf ("WARNING: Test not run as insufficient files available\n");
 
3509
                        printf ("WARNING: (%u available files for uid %d, need atleast %u)\n",
 
3510
                                         (unsigned int)rlimit.rlim_cur,
 
3511
                                         (int)getuid (),
 
3512
                                         (unsigned int)available_ptys);
 
3513
                        printf ("WARNING:\n");
 
3514
                        printf ("WARNING: See limits(5).\n");
 
3515
                        printf ("WARNING:\n");
 
3516
                } else {
 
3517
                        /* Consume all free ptys */
 
3518
                        {
 
3519
                                int pty;
 
3520
 
 
3521
                                for (pty = 0; pty < available_ptys; ++pty) {
 
3522
                                        ret = posix_openpt (O_RDWR | O_NOCTTY);
 
3523
                                        if (ret < 0)
 
3524
                                                break;
 
3525
                                }
 
3526
                        }
 
3527
 
 
3528
                        pid = job_process_spawn (job, args, NULL, FALSE, -1);
 
3529
                        TEST_LT (pid, 0);
 
3530
                        err = nih_error_get ();
 
3531
                        TEST_EQ (err->number, ENOMEM);
 
3532
                        nih_free (err);
 
3533
                }
 
3534
                nih_free (job);
 
3535
        }
 
3536
#else
 
3537
                /* FIXME */
 
3538
                TEST_FEATURE ("WARNING: FIXME: test 'when no free ptys' disabled due to kernel bug");
 
3539
#endif
 
3540
 
 
3541
        nih_free (class);
 
3542
        TEST_EQ (unsetenv ("UPSTART_LOGDIR"), 0);
 
3543
}
 
3544
 
 
3545
void
 
3546
test_log_path (void)
 
3547
{
 
3548
        JobClass        *class = NULL;
 
3549
        Job             *job = NULL;
 
3550
        nih_local char  *log_path = NULL;
 
3551
        nih_local char  *expected = NULL;
 
3552
        nih_local char  *home = NULL;
 
3553
        char             dirname[PATH_MAX];
 
3554
 
 
3555
        TEST_FILENAME (dirname);       
 
3556
 
 
3557
        TEST_FUNCTION ("job_process_log_path");
 
3558
 
 
3559
        /************************************************************/
 
3560
        TEST_FEATURE ("with system job with simple name");
 
3561
 
 
3562
        class = job_class_new (NULL, "system", NULL);
 
3563
        TEST_NE_P (class, NULL);
 
3564
        job = job_new (class, "");
 
3565
        TEST_NE_P (job, NULL);
 
3566
 
 
3567
        log_path = job_process_log_path (job, FALSE);
 
3568
        TEST_NE_P (log_path, NULL);
 
3569
 
 
3570
        expected = NIH_MUST (nih_sprintf (NULL, "%s/%s.log",
 
3571
                                EXPECTED_JOB_LOGDIR, "system"));
 
3572
        TEST_EQ_STR (log_path, expected);
 
3573
        nih_free (job);
 
3574
 
 
3575
        /************************************************************/
 
3576
        TEST_FEATURE ("with system job containing illegal path characters");
 
3577
 
 
3578
        class = job_class_new (NULL, "//hello_foo bar.z/", NULL);
 
3579
        TEST_NE_P (class, NULL);
 
3580
        job = job_new (class, "");
 
3581
        TEST_NE_P (job, NULL);
 
3582
 
 
3583
        log_path = job_process_log_path (job, FALSE);
 
3584
        TEST_NE_P (log_path, NULL);
 
3585
 
 
3586
        expected = NIH_MUST (nih_sprintf (NULL, "%s/%s.log",
 
3587
                                EXPECTED_JOB_LOGDIR, "__hello_foo bar.z_"));
 
3588
        TEST_EQ_STR (log_path, expected);
 
3589
        nih_free (job);
 
3590
 
 
3591
        /************************************************************/
 
3592
        TEST_FEATURE ("with system job with named instance");
 
3593
 
 
3594
        class = job_class_new (NULL, "foo bar", NULL);
 
3595
        TEST_NE_P (class, NULL);
 
3596
        job = job_new (class, "bar foo");
 
3597
        TEST_NE_P (job, NULL);
 
3598
 
 
3599
        log_path = job_process_log_path (job, FALSE);
 
3600
        TEST_NE_P (log_path, NULL);
 
3601
 
 
3602
        expected = NIH_MUST (nih_sprintf (NULL, "%s/%s.log",
 
3603
                                EXPECTED_JOB_LOGDIR, "foo bar-bar foo"));
 
3604
        TEST_EQ_STR (log_path, expected);
 
3605
        nih_free (job);
 
3606
 
 
3607
        /************************************************************/
 
3608
        TEST_FEATURE ("with system job with named instance and illegal path characters");
 
3609
 
 
3610
        class = job_class_new (NULL, "a/b", NULL);
 
3611
        TEST_NE_P (class, NULL);
 
3612
        job = job_new (class, "c/d_?/");
 
3613
        TEST_NE_P (job, NULL);
 
3614
 
 
3615
        log_path = job_process_log_path (job, FALSE);
 
3616
        TEST_NE_P (log_path, NULL);
 
3617
 
 
3618
        expected = NIH_MUST (nih_sprintf (NULL, "%s/%s.log",
 
3619
                                EXPECTED_JOB_LOGDIR, "a_b-c_d_?_"));
 
3620
        TEST_EQ_STR (log_path, expected);
 
3621
        nih_free (job);
 
3622
 
 
3623
        /************************************************************/
 
3624
        TEST_FEATURE ("with subverted logdir and system job with named instance and illegal path characters");
 
3625
 
 
3626
        TEST_EQ (setenv ("UPSTART_LOGDIR", dirname, 1), 0);
 
3627
 
 
3628
        class = job_class_new (NULL, "a/b", NULL);
 
3629
        TEST_NE_P (class, NULL);
 
3630
        job = job_new (class, "c/d_?/");
 
3631
        TEST_NE_P (job, NULL);
 
3632
 
 
3633
        log_path = job_process_log_path (job, FALSE);
 
3634
        TEST_NE_P (log_path, NULL);
 
3635
 
 
3636
        expected = NIH_MUST (nih_sprintf (NULL, "%s/%s.log",
 
3637
                                dirname, "a_b-c_d_?_"));
 
3638
        TEST_EQ_STR (log_path, expected);
 
3639
        nih_free (job);
 
3640
 
 
3641
        TEST_EQ (unsetenv ("UPSTART_LOGDIR"), 0);
1036
3642
}
1037
3643
 
1038
3644
 
1197
3803
        siginfo_t       info;
1198
3804
        unsigned long   data;
1199
3805
        struct timespec now;
 
3806
        char            dirname[PATH_MAX];
 
3807
 
 
3808
        TEST_FILENAME (dirname);       
 
3809
        TEST_EQ (mkdir (dirname, 0755), 0);
 
3810
        TEST_EQ (setenv ("UPSTART_LOGDIR", dirname, 1), 0);
1200
3811
 
1201
3812
        TEST_FUNCTION ("job_process_handler");
1202
3813
        program_name = "test";
1205
3816
        source = conf_source_new (NULL, "/tmp", CONF_JOB_DIR);
1206
3817
        file = conf_file_new (source, "/tmp/test");
1207
3818
        file->job = class = job_class_new (NULL, "test", NULL);
 
3819
        TEST_NE_P (file->job, NULL);
1208
3820
        class->process[PROCESS_MAIN] = process_new (class);
1209
3821
        class->process[PROCESS_MAIN]->command = "echo";
1210
3822
 
4353
6965
 
4354
6966
        nih_free (event);
4355
6967
        event_poll ();
 
6968
 
 
6969
        TEST_EQ (unsetenv ("UPSTART_LOGDIR"), 0);
4356
6970
}
4357
6971
 
4358
6972
 
4732
7346
         * environment).
4733
7347
         */
4734
7348
 
 
7349
        /* run tests in legacy (pre-session support) mode */
 
7350
        setenv ("UPSTART_NO_SESSIONS", "1", 1);
 
7351
 
 
7352
 
4735
7353
        /* We re-exec this binary to test various children features.  To
4736
7354
         * do that, we need to know the full path to the program.
4737
7355
         */
4746
7364
                argv0 = path;
4747
7365
        }
4748
7366
 
 
7367
        /* If three arguments are given, the first is the child enum,
 
7368
         * second is a filename to write the result to and the third is
 
7369
         * the number of times to fork.
 
7370
         */
 
7371
        if (argc == 4) {
 
7372
                int forks = atol (argv[3]);
 
7373
                nih_assert (forks > 0);
 
7374
 
 
7375
                do {
 
7376
                        if (fork () != 0)
 
7377
                                exit (0);
 
7378
 
 
7379
                } while (forks--);
 
7380
 
 
7381
                child (atoi (argv[1]), argv[2]);
 
7382
                exit (1);
 
7383
        }
 
7384
 
4749
7385
        /* If two arguments are given, the first is the child enum and the
4750
7386
         * second is a filename to write the result to.
4751
7387
         */
4754
7390
                exit (1);
4755
7391
        }
4756
7392
 
 
7393
        job_class_init ();
 
7394
        nih_error_init ();
 
7395
        nih_io_init ();
 
7396
 
4757
7397
        /* Otherwise run the tests as normal */
4758
7398
        test_run ();
4759
7399
        test_spawn ();
 
7400
        test_log_path ();
4760
7401
        test_kill ();
4761
7402
        test_handler ();
4762
7403
        test_utmp ();
4763
 
 
4764
7404
        test_find ();
4765
7405
 
4766
7406
        return 0;