[go: up one dir, main page]

File: pyupstart.py

package info (click to toggle)
upstart 1.11-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 12,988 kB
  • ctags: 2,100
  • sloc: ansic: 67,932; sh: 12,339; python: 1,718; makefile: 1,093; xml: 162; sed: 16
file content (1122 lines) | stat: -rwxr-xr-x 33,023 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
#!/usr/bin/python3
#---------------------------------------------------------------------
# = Limitations =
#
# - Override files are not currently supported.
#
#   Note that you can make use of Upstart.test_dir to determine
#   where to create the override but be aware that after creating a
#   '.override' file, you must wait until Upstart has re-parsed the job.
#
#---------------------------------------------------------------------

"""
Upstart test module.
"""

import os
import sys
import logging
import tempfile
import pyinotify
import subprocess
import shutil
import dbus
import dbus.service
import dbus.mainloop.glib
import time
import json
from datetime import datetime, timedelta
from gi.repository import GLib

VERSION = '0.1'
NAME = 'TestUpstart'

UPSTART = '/sbin/init'
INITCTL = '/sbin/initctl'

UPSTART_SESSION_ENV = 'UPSTART_SESSION'

UPSTART_STATE_FILE = 'upstart.state'

INIT_SOCKET = 'unix:abstract=/com/ubuntu/upstart'

SYSTEM_JOB_DIR = '/etc/init'
SYSTEM_LOG_DIR = '/var/log/upstart'

# used to log session init output
DEFAULT_LOGFILE = '/tmp/upstart.log'

DEFAULT_SESSION_INSTALL_PATH = '/usr/share/upstart/sessions'

SESSION_DIR_FMT = 'upstart/sessions'

BUS_NAME                 = 'com.ubuntu.Upstart'
INTERFACE_NAME           = 'com.ubuntu.Upstart0_6'
JOB_INTERFACE_NAME       = 'com.ubuntu.Upstart0_6.Job'
INSTANCE_INTERFACE_NAME  = 'com.ubuntu.Upstart0_6.Instance'
OBJECT_PATH              = '/com/ubuntu/Upstart'
FREEDESKTOP_PROPERTIES   = 'org.freedesktop.DBus.Properties'

# Maximum number of seconds to wait for Upstart to detect a new job
# has been created
JOB_WAIT_SECS = 5

# Maximum number of seconds to wait for session file to appear after
# startup of Session Init
SESSION_FILE_WAIT_SECS = 5

# Maximum number of seconds to wait for Upstart to complete a re-exec
REEXEC_WAIT_SECS = 5

# Maximum number of seconds to wait for Upstart to create a file
FILE_WAIT_SECS = 5

# Maximum number of seconds to wait for Upstart to create a logfile
LOGFILE_WAIT_SECS = 5

#---------------------------------------------------------------------


def dbus_encode(str):
    """
    Simulate nih_dbus_path() which Upstart uses to convert
    a job path into one suitable for use as a D-Bus object path.

    This entails converting all non-alpha-numeric bytes in the
    string into a 3 byte string comprising an underscore ('_'),
    followed by the 2 byte lower-case hex representation of the byte
    in question. Alpha-numeric bytes are left unmolested.

    Note that in the special case of the specified string being None
    or the nul string, it is encoded as '_'.

    Examples:

    'hello-world' would be encoded as 'hello_2dworld' since
    '-' is 2d in hex (resulting in '_2d').

    Similarly, '_2f' would be the encoding for '/' since that character
    has hex value 2f.

    """
    if not str:
        return '_'

    hex = []
    for ch in str:
        if ch.isalpha() or ch.isdigit():
            hex.append(ch)
        else:
            hex.append("_%02x" % ord(ch))

    # convert back into a string
    return ''.join(hex)


def secs_to_milli(secs):
    """
    Convert @secs seconds to milli-seconds.
    """
    return secs * 1000


def wait_for_file(path, timeout=FILE_WAIT_SECS):
    """
    Wait for a specified file to exist.

    @path: Full path to file to wait for.

    Returns: True if file was created within @timeout seconds, else
     False.
    """
    until = datetime.now() + timedelta(seconds=timeout)

    while datetime.now() < until:
        if os.path.exists(path):
            return True
        time.sleep(0.1)

    return False


class InotifyHandler(pyinotify.ProcessEvent):

    # We don't actually do anything here since all we care
    # about is whether we timed-out.
    def process_IN_CREATE(self, event):
        pass


class UpstartException(Exception):
    """
    An Upstart Exception.
    """
    pass


class Upstart:
    """
    Upstart Class.

    conf_dir: Full path to job configuration file directory.
    test_dir: Full path to directory below @conf_dir used to store
     test jobs.
    test_dir_name: Relative directory of @test_dir below @conf_dir
        (effectively '@conf_dir - @test_dir').
    log_dir: Full path to job log files directory.
    """

    def __init__(self):
        self.logger = logging.getLogger(self.__class__.__name__)
        self.jobs = []

        self.conf_dir = None
        self.test_dir = None
        self.test_dir_name = None
        self.log_dir = None

        self.socket = None
        self.connection = None

        # Set to True when a new job is created
        self.job_seen = False

        self.new_job = None
        self.mainloop = None
        self.timeout_source = None

        dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

        self.create_dirs()

    def connect(self, force=False):
        """
        Connect to Upstart.

        @force: if True, connect regardless of whether already
        connected.

        Notes:
          - Raises an UpstartException() if already connected.
        """
        if self.connection and not force:
            raise UpstartException('Already connected')

        # Create appropriate D-Bus connection
        self.connection = dbus.connection.Connection(self.socket)
        self.remote_object = self.connection.get_object(
            object_path=OBJECT_PATH)
        self.proxy = dbus.Interface(self.remote_object, INTERFACE_NAME)

    def reconnect(self):
        """
        Forcibly reconnect to Upstart.
        """
        self.connect(force=True)

    def polling_connect(self, timeout=REEXEC_WAIT_SECS, force=False):
        """
        Attempt to connect to Upstart repeatedly for up to @timeout
        seconds.

        Useful after a re-exec since that operation although fast takes
        an indeterminate amount of time to complete.

        @timeout: seconds to wait for successful connection.
        @force: if True, force a reconnection.
        """
        for i in range(timeout):
            try:
                self.connect(force=force)
                self.version()
                return
            except dbus.exceptions.DBusException:
                time.sleep(1)

        raise UpstartException(
            'Failed to reconnect to Upstart after %d seconds' % timeout)

    def _timeout_cb(self):
        """
        Handle timeout if job not seen in a reasonable amount of time.
        """
        self.mainloop.quit()

    def _idle_create_job_cb(self, name, body, *args):
        """
        Handler to create a Job Configuration File as soon as
        the main loop starts.

        """
        self.new_job = Job(self, self.test_dir, self.test_dir_name,
            name, body=body, retain=self.retain)

        # deregister
        return False

    def _job_added_cb(self, path):
        """
        Handle the 'JobAdded(Object path)' signal.
        """

        # ignore signals that don't match the job we care about.
        if path != self.job_object_path:
            return True

        self.job_seen = True

        assert self.timeout_source, 'Expected timeout source to be defined'

        # remove timeout handler
        GLib.source_remove(self.timeout_source)

        self.mainloop.quit()

        # deregister
        return False

    def set_test_dir(self):
        """
        Create a directory to hold the test jobs beneath the job
        configuration directory.
        """
        self.test_dir = tempfile.mkdtemp(prefix=NAME + '-', dir=self.conf_dir)
        self.test_dir_name = self.test_dir.replace("%s/" % self.conf_dir, '')

    def create_dirs(self):
        """
        Create the directories required to store job configuration files
        and log job output.
        """
        for dir in (self.conf_dir, self.test_dir, self.log_dir):
            if dir:
                try:
                    os.makedirs(dir)
                except FileExistsError:
                    pass

    def destroy(self):
        """
        Remove all jobs.
        """
        for job in self.jobs:
            job.destroy()

        if self.test_dir:
            shutil.rmtree(self.test_dir)

        # invalidate
        self.connection = None
        self.remote_object = None
        self.proxy = None

    def emit(self, event, env=None, wait=True):
        """
        @event: Name of event to emit.
        @env: optional environment for event.
        @wait: if True, wait for event to be fully emitted
         (synchronous), else async.

        Emit event @event with optional environment @env.
        """
        if env is None:
            env = []
        self.proxy.EmitEvent(event, dbus.Array(env, 's'), wait)

    def version(self, raw=False):
        """
        Determine version of running instance of Upstart.

        @raw: if True, return full version string, else just the
        version in the form 'x.y'.

        Returns: Version as a string.

        """
        properties = dbus.Interface(self.remote_object, FREEDESKTOP_PROPERTIES)
        version_string = properties.Get(INTERFACE_NAME, 'version')
        if raw:
            return version_string

        return version_string.split()[2].strip(')')

    def get_state_json(self):
        """
        Obtain Upstart internal state in JSON format.
        """
        return self.proxy.GetState()

    def get_state(self):
        """
        Obtain Upstart internal state (JSON) and convert to Python
        dictionary format before returning.
        """
        return json.loads(self.get_state_json())

    def get_sessions(self):
        """
        Returns dictionary of session details.
        """
        state = self.get_state()
        sessions = state['sessions']
        return sessions

    def session_count(self):
        """
        Returns number of chroot sessions.
        """
        return len(self.get_sessions())

    def sessions_exist(self):
        """
        Returns True if sessions exist, else False.
        """
        return self.session_count() > 0

    def reexec(self):
        """
        Request Upstart re-exec itself.

        Note that after a re-exec, it is necessary to reconnect to
        Upstart.
        """
        raise NotImplementedError('method must be implemented by subclass')

    def job_create(self, name, body, retain=False):
        """
        Create a Job Configuration File.

        @name: Name to give the job.
        @body: String representation of configuration file, or list of
         strings.
        @retain: if True, don't remove the Job Configuration File when
         object is cleaned up.

        Strategy:

        Arranging for this method to detect that Upstart has registered
        this job in an efficient manner is tricky. The approach adopted
        is to:

        - Create a glib main loop.
        - Register a D-Bus signal handler which looks for the 'JobAdded'
          signal emitted by Upstart when a new job is available (in
          other words when Upstart has parsed its job configuration file
          successfully).
        - Create a glib main loop idle handler that will be called as
          soon as the loop starts and will actually create the job.
        - Add a glib main loop timeout handler which will detect if the
          job failed to be registered.
        - Run the main loop, which performs the following steps:
          - Calls the idle handler immediately. This creates the
            job configuration file, then deregisters itself so it is
            never called again.
            - If Upstart fails to parse the job, the timeout handler gets
              called which causes the main loop to exit. job_seen will not
              be set.
            - If Upstart does parse the file, the _job_added_cb()
              callback gets called as a result of the 'JobAdded' D-Bus
              signal being emitted. This will set job_seen and request
              the main loop exits.
        - Check the job_seen variable and react accordingly.
        """

        # Create a new mainloop
        self.mainloop = GLib.MainLoop()

        # reset
        self.job_seen = False
        self.new_job = None

        self.retain = retain

        # construct the D-Bus path for the new job
        job_path = '{}/{}'.format(self.test_dir_name, name)

        self.job_object_path = '{}/{}/{}'.format(
            OBJECT_PATH, 'jobs', dbus_encode(job_path)
        )

        self.connection.add_signal_receiver(
            self._job_added_cb,
            dbus_interface=INTERFACE_NAME,
            path=OBJECT_PATH,
            signal_name='JobAdded')

        GLib.idle_add(self._idle_create_job_cb, name, body)
        self.timeout_source = GLib.timeout_add(
            secs_to_milli(JOB_WAIT_SECS),
            self._timeout_cb
        )
        self.mainloop.run()

        if not self.job_seen:
            return None

        # reset
        self.job_seen = False
        self.mainloop = None

        self.jobs.append(self.new_job)

        return self.new_job

    def job_recreate(self, name, conf_path):
        """
        Create a job object from an existing Job Configuration File.

        @name: Name prefix of existing job configuration file.
        @conf_path: Full path to *existing* Job Configuration File.
        """

        assert (name)
        assert (conf_path)

        job_path = '{}/{}'.format(self.test_dir_name, name)
        self.job_object_path = '{}/{}/{}'.format(
            OBJECT_PATH, 'jobs', dbus_encode(job_path)
        )

        self.new_job = Job(self, self.test_dir, self.test_dir_name,
                name, body=None, reuse_path=conf_path)
        self.jobs.append(self.new_job)
        return self.new_job


class Job:
    """
    Representation of an Upstart Job.

    This equates to the Job Configuration file and details of the
    running instances.

    For single-instance jobs (those that do not specify the 'instance'
    stanza), this object is sufficient to control the job.

    For multi-instance jobs, manipulating this object will operate on
    *all* the instances where it makes sense to do so. If this is not
    desired behaviour, capture the JobInstance() returned from
    start() and operate on that instead.

    """

    def __init__(self, upstart, dir_name, subdir_name, job_name,
            body=None, reuse_path=None, retain=False):
        """
        @upstart: Upstart() parent object.
        @dir_name: Full path to job configuration files directory.
        @subdir_name: Relative directory of test_dir below job
         configuration file directory.
        @job_name: Name of job.
        @body: Contents of job configuration file (either a string, or a
         list of strings).
        @reuse_path: If set and @body is None, (re)create the job object
         using the existing specified job configuration file path.
        @retain: If True, don't delete the Job Configuration File on
         object destruction.
        """

        self.logger = logging.getLogger(self.__class__.__name__)

        self.instances = []
        self.instance_names = []

        self.upstart = upstart
        self.subdir_name = subdir_name
        self.name = job_name
        self.job_dir = dir_name
        self.body = body
        self.reuse_path = reuse_path
        self.retain = retain

        self.instance_name = None

        # proxy to job instance
        self.instance = None

        self.properties = None

        # need some way to create the job
        if not self.body and not self.reuse_path:
            raise UpstartException('No body or reusable path specified')

        if self.reuse_path:
            self.conffile = self.reuse_path
        else:
            self.conffile = os.path.join(self.job_dir, self.name + '.conf')

        if self.body and isinstance(self.body, str):
            # Assume body cannot be a bytes object.
            body = body.splitlines()

        if not self.body and self.reuse_path:
            # Just check conf file exists
            if not os.path.exists(self.conffile):
                raise UpstartException(
                        'File {} does not exist for reuse'.format(self.conffile))
        else:
            # Create conf file
            with open(self.conffile, 'w', encoding='utf-8') as fh:
                for line in body:
                    print(line.strip(), file=fh)
                print(file=fh)

        self.valid = True

        subdir_object_path = dbus_encode(
            "%s/%s" % (self.subdir_name, self.name)
        )
        self.object_path = "%s/%s/%s" % \
            (OBJECT_PATH, 'jobs', subdir_object_path)

        self.remote_object = \
            self.upstart.connection.get_object(BUS_NAME, self.object_path)
        self.interface = dbus.Interface(self.remote_object, JOB_INTERFACE_NAME)

    def destroy(self):
        """
        Stop all instances and cleanup.
        """
        try:
            for instance in self.instances:
                instance.destroy()

            if not self.retain:
                os.remove(self.conffile)
        except FileNotFoundError:
            pass

        self.valid = False

    def start(self, env=None, wait=True):
        """
        Start the job. For multi-instance jobs (those that specify the
        'instance' stanza), you will need to use the returned
        JobInstance object to manipulate the individual instance.

        Returns: JobInstance.
        """

        if env is None:
            env = []
        instance_path = self.interface.Start(dbus.Array(env, 's'), wait)

        instance_name = instance_path.replace("%s/" % self.object_path, '')

        # store the D-Bus encoded instance name ('_' for single-instance jobs)
        if instance_name not in self.instance_names:
            self.instance_names.append(instance_name)

        instance = JobInstance(self, instance_name, instance_path)
        self.instances.append(instance)
        return instance

    def get_instance(self):
        """
        Returns: JobInstance of calling Job.
        """

        # construct the D-Bus path for the new job
        job_path = '{}/{}'.format(self.upstart.test_dir_name, self.name)

        instance_path = '{}/{}/{}/{}'.format(
            OBJECT_PATH, 'jobs', dbus_encode(job_path),

            # XXX: LIMITATION - only support default instance.
            '_'
        )
        instance_name = instance_path.replace("%s/" % self.object_path, '')

        # store the D-Bus encoded instance name ('_' for single-instance jobs)
        if instance_name not in self.instance_names:
            self.instance_names.append(instance_name)

        instance = JobInstance(self, instance_name, instance_path)
        self.instances.append(instance)
        return instance

    def _get_dbus_instance(self, name):
        """
        Retrieve D-Bus job instance and its properties.

        @name: D-Bus encoded instance name.

        """

        assert name, 'Name must not be None'

        object_path = '{}/{}'.format(self.object_path, name)

        remote_object = \
            self.upstart.connection.get_object(BUS_NAME, object_path)

        return dbus.Interface(remote_object, INSTANCE_INTERFACE_NAME)

    def stop(self, wait=True):
        """
        Stop all running instance of the job.

        @wait: if False, stop job instances asynchronously.
        """

        for name in self.instance_names:
            instance = self._get_dbus_instance(name)
            try:
                instance.Stop(wait)
            except dbus.exceptions.DBusException:
                # job has already stopped
                pass

    def restart(self, wait=True):
        """
        Restart all running instance of the job.

        @wait: if False, stop job instances asynchronously.
        """
        for name in self.instance_names:
            instance = self._get_dbus_instance(name)
            instance.Restart(wait)

    def instance_object_paths(self):
        """
        Returns a list of instance object paths.
        """
        return ["%s/%s" % (self.object_path, instance)
                for instance in self.instance_names]

    def pids(self, name=None):
        """
        @name: D-Bus encoded instance name.

        Returns: Map of job processes:
            name=job process name
            value=pid

        Notes: If your job has multiple instances, call the method of
        the same name on the individual instance objects.
        """

        name = ('_' if name is None else name)

        if len(self.instance_names) > 1:
            raise UpstartException('Cannot handle multiple instances')

        assert(name in self.instance_names)

        instance = self._get_dbus_instance(name)
        assert (instance)

        properties = dbus.Interface(instance, FREEDESKTOP_PROPERTIES)
        assert (properties)

        # don't assert as there may not be any processes
        procs = properties.Get(INSTANCE_INTERFACE_NAME, 'processes')

        pid_map = {}

        for proc in procs:
            # convert back to natural types
            job_proc = str(proc[0])
            job_pid = int(proc[1])

            pid_map[job_proc] = job_pid

        return pid_map

    def running(self, name):
        """
        @name: D-Bus encoded name of job instance.

        Determine if an instance is currently running.

        Returns: True if @name is running, else false.
        """
        if len(self.instance_names) > 1:
            raise UpstartException('Cannot handle multiple instances')

        if name not in self.instance_names:
            return False

        return len(self.pids(name)) > 0

    def logfile_name(self, instance_name):
        """
        Determine full path to logfile for job instance.

        @instance_name: D-Bus encoded job instance name.

        Note: it is up to the caller to ensure the logfile exists.

        Returns: full path to logfile.
        """

        if instance_name != '_':
            filename = '{}_{}-{}.log'.format(
                self.subdir_name, self.name, instance_name
            )
        else:
            # Note the underscore that Upstart auto-maps from the subdirectory
            # slash (see init(5)).
            filename = '{}_{}.log'.format(self.subdir_name, self.name)

        logfile = os.path.join(self.upstart.log_dir, filename)

        return logfile


class LogFile:
    """
    Representation of an Upstart job logfile.
    """

    def __init__(self, path):
        """
        @path: full path to logfile.
        """
        self.logger = logging.getLogger(self.__class__.__name__)
        self.path = path
        self.valid = True

    def destroy(self):
        """
        Clean up: *MUST* be called by caller!
        """
        try:
            os.remove(self.path)
            self.valid = False
        except OSError:
            pass

    def exists(self):
        """
        Determine if logfile exists.

        Returns: True or False.
        """
        return os.path.exists(self.path)

    def _get_lines(self):
        """
        Get contents of log.

        Notes: '\r' characters added by pty() are removed by
        readlines().

        Returns: List of lines in logfile.

        """

        assert self.path, 'Path not set'

        with open(self.path, 'r', encoding='utf-8') as fh:
            return fh.readlines()

    def readlines(self, timeout=LOGFILE_WAIT_SECS):
        """
        Read logfile. A timeout has to be used to avoid "hanging" since the job
        associated with this logfile:

        - may not create any output.
        - may produce output only after a long period of time.

        @timeout: seconds to wait file to be created.

        Notes:
          - Raises an UpstartException() on timeout.
          - '\r' characters added by pty() will be removed.

        Returns: Array of lines in logfile or None if logfile was not
        created in @timeout seconds.

        """

        self.timeout = timeout

        # Polling - ugh. However, arranging for an inotify watch
        # at this level is tricky since the watch cannot be created here
        # as by the time the watch is in place, the logfile may already
        # have been created, which not only nullifies the reason for
        # adding the watch in the first place, but also results in an
        # impotent watch (unless a timeout on the watch is specified).
        # The correct place to create the watch is in one of the Upstart
        # classes. However, even then, timing issues result wrt to
        # calling the appropriate inotify APIs.
        #
        # Hence, altough polling is gross it has the advantage of
        # simplicity and reliability in this instance.
        until = datetime.now() + timedelta(seconds=self.timeout)

        while datetime.now() < until:
            try:
                return self._get_lines()
            except FileNotFoundError:
                time.sleep(0.1)

        return None


class JobInstance:
    """
    Representation of a running Upstart Job Instance.
    """

    def __init__(self, job, instance_name, object_path):
        """
        @instance_name: D-Bus encoded instance name.
        @object_path: D-Bus object path.
        """
        self.logger = logging.getLogger(self.__class__.__name__)
        self.job = job
        self.instance_name = instance_name
        self.object_path = object_path

        self.remote_object = \
            self.job.upstart.connection.get_object(BUS_NAME, self.object_path)

        self.instance = \
            dbus.Interface(self.remote_object, INSTANCE_INTERFACE_NAME)

        self.properties = dbus.Interface(self.instance, FREEDESKTOP_PROPERTIES)

        # all jobs are expected to be created in a subdirectory.
        assert(self.job.subdir_name)

        logfile = job.logfile_name(instance_name)
        self.logfile = LogFile(logfile)

    def stop(self, wait=True):
        """
        Stop instance.

        @wait: if True, wait for job, else perform operation
         asynchronously.
        """
        try:
            self.instance.Stop(wait)
        except dbus.exceptions.DBusException:
            # job has already stopped
            pass

    def restart(self, wait=True):
        """
        Restart instance.

        @wait: if True, wait for job, else perform operation
         asynchronously.
        """
        self.instance.Restart(wait)

    def pids(self):
        procs = self.properties.Get(INSTANCE_INTERFACE_NAME, 'processes')

        pid_map = {}

        for proc in procs:
            # convert back to natural types
            job_proc = str(proc[0])
            job_pid = int(proc[1])

            pid_map[job_proc] = job_pid

        return pid_map

    def destroy(self):
        """
        Stop the instance and cleanup.

	Note: If the instance specified retain when created, this will
	be a NOP.
        """
        if not self.job.retain:
            self.stop()
            self.logfile.destroy()

class SystemInit(Upstart):

    def __init__(self):
        super().__init__()
        self.logger = logging.getLogger(self.__class__.__name__)
        self.socket = INIT_SOCKET
        self.conf_dir = SYSTEM_JOB_DIR
        self.log_dir = SYSTEM_LOG_DIR
        self.set_test_dir()
        self.connect()

    def reexec(self):
        if not self.proxy:
            raise UpstartException('Not yet connected')

        # Use the official system interface
        os.system('telinit u')


class SessionInit(Upstart):
    """
    Create a new Upstart Session or join an existing one.
    """

    timeout = SESSION_FILE_WAIT_SECS

    def _get_sessions(self):
        """
        Obtain a list of running sessions.

        Returns: Map of sessions:
            name=pid
            value=socket address
        """

        sessions = {}

        args = [INITCTL, 'list-sessions']

        for line in subprocess.check_output(args,
                        universal_newlines=True).splitlines():
            pid, socket = line.split()
            sessions[pid] = socket

        return sessions

    def __init__(self, join=False, capture=None, extra=None):
        """
        @join: If False, start a new session. If TRUE join the existing
         main (non-test) session.
        @capture: Set to the name of a file to capture all stdout and
         stderr output.
        @extra: Array of extra arguments (only used when @join is False).

        Notes:
        - If @join is True, an UpstartException is raised if either
        no existing session is found, or multiple existing sessions are
        found.
        - Joining implies joining the main Upstart session, not a
        test session.

        """
        super().__init__()

        self.logger = logging.getLogger(self.__class__.__name__)

        self.join = join
        self.capture = capture

        self.socket = os.environ.get(UPSTART_SESSION_ENV)

        sessions = self._get_sessions()
        if self.join and len(sessions) > 1:
            raise UpstartException('Multiple existing sessions')

        if self.join and not self.socket:
            raise UpstartException('No existing session')

        if self.join:
            self.session = self.socket

            # We are joining the main desktop session.
            #
            # Multiple conf file directories are supported, but we'll
            # stick with the default.
            config_home = os.environ.get('XDG_CONFIG_HOME', "%s/%s"
                                         % (os.environ.get('HOME'), '.config'))
            cache_home = os.environ.get('XDG_CACHE_HOME', "%s/%s"
                                        % (os.environ.get('HOME'), '.cache'))

            self.conf_dir = "%s/%s" % (config_home, 'upstart')
            self.log_dir = "%s/%s" % (cache_home, 'upstart')
        else:
            args = []

            pid = os.getpid()

            self.conf_dir = \
                tempfile.mkdtemp(prefix="%s-confdir-%d-" % (NAME, pid))

            self.log_dir = \
                tempfile.mkdtemp(prefix="%s-logdir-%d-" % (NAME, pid))

            args.extend([UPSTART, '--user',
                           '--confdir', self.conf_dir,
                           '--logdir', self.log_dir])

            if extra:
                args.extend(extra)

            self.logger.debug(
                'Starting Session Init with arguments: %s' % " ".join(args)
            )

            watch_manager = pyinotify.WatchManager()
            mask = pyinotify.IN_CREATE
            notifier = \
                pyinotify.Notifier(watch_manager, InotifyHandler())
            session = os.path.join(os.environ['XDG_RUNTIME_DIR'], SESSION_DIR_FMT)
            watch_manager.add_watch(session, mask)

            notifier.process_events()

            if self.capture:
                self.out = open(self.capture, 'w')
            else:
                self.out = subprocess.DEVNULL

            self.proc = \
                subprocess.Popen(args=args, stdout=self.out, stderr=self.out)

            self.pid = self.proc.pid

            self.logger.debug('Session Init running with pid %d' % self.pid)

            millisecs_timeout = secs_to_milli(self.timeout)
            if not notifier.check_events(timeout=millisecs_timeout):
                msg = \
                    "Timed-out waiting for session file after %d seconds" \
                    % self.timeout
                raise UpstartException(msg)

            # consume
            notifier.read_events()
            notifier.stop()

            if self.capture:
                self.out.flush()

            # Activity has been seen in the session file directory, so
            # our Session Init should now have written the session file
            # (although this assumes no other Session Inits are being
            # started).
            sessions = self._get_sessions()

            if not str(self.pid) in sessions:
                msg = "Session with pid %d not found" % self.pid
                raise UpstartException(msg)

            self.socket = sessions[str(self.pid)]
            self.logger.debug("Created Upstart Session '%s'" % self.socket)
            os.putenv(UPSTART_SESSION_ENV, self.socket)

        self.set_test_dir()
        self.connect()

    def destroy(self):
        """
        Stop the SessionInit (if session was not joined) and cleanup.
        """
        super().destroy()
        if self.capture:
            self.out.close()
        if not self.join:
            self.logger.debug(
                'Stopping Session Init running with pid %d' % self.pid
            )
            self.proc.terminate()
            self.proc.wait()
            os.rmdir(self.log_dir)
            shutil.rmtree(self.conf_dir)
            os.unsetenv(UPSTART_SESSION_ENV)

    def reexec(self):
        if not self.proxy:
            raise UpstartException('Not yet connected')

        self.proxy.Restart()