~matkor/wicd/experimental-matkor

« back to all changes in this revision

Viewing changes to wicd/daemon.py

  • Committer: matkor
  • Date: 2008-07-01 21:44:37 UTC
  • Revision ID: matkor@laptop-hp-20080701214437-kjhjoyyeb3wucilb
Store pid even if not daemonized. Fixed pidfile option. Deleting pidfile when daemon ends.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1451
1451
\t-h\t--help        \tPrint this help.
1452
1452
"""
1453
1453
 
1454
 
def daemonize(write_pid, pidfile):
 
1454
def daemonize():
1455
1455
    """ Disconnect from the controlling terminal.
1456
1456
 
1457
1457
    Fork twice, once to disconnect ourselves from the parent terminal and a
1480
1480
    try:
1481
1481
        pid = os.fork()
1482
1482
        if pid > 0:
1483
 
            if not write_pid:
1484
 
                print "wicd daemon: pid " + str(pid)
1485
 
            else:
1486
 
                print >> open(pidfile,'wt'), str(pid)
1487
1483
            sys.exit(0)
1488
1484
    except OSError, e:
1489
1485
        print >> sys.stderr, "Fork #2 failed: %d (%s)" % (e.errno, e.strerror)
1490
1486
        sys.exit(1)
 
1487
    
1491
1488
 
1492
1489
def main(argv):
1493
1490
    """ The main daemon program.
1505
1502
    try:
1506
1503
        opts, args = getopt.getopt(sys.argv[1:], 'dfeosP:',
1507
1504
                ['help', 'no-daemon', 'no-stderr', 'no-stdout', 'no-scan', 'debug',
1508
 
                 'pidfile:'])
 
1505
                 'pidfile='])
1509
1506
    except getopt.GetoptError:
1510
1507
        # Print help information and exit
1511
1508
        usage()
1532
1529
        if o in ('-P', '--pidfile'):
1533
1530
            write_pid = True
1534
1531
            pid_file = a
1535
 
 
1536
 
    if do_daemonize: daemonize(write_pid, pid_file)
 
1532
    if do_daemonize: 
 
1533
        daemonize()
 
1534
    pid  = os.getpid()
 
1535
    print "wicd daemon: pid %s (daemonized: %s)." % (pid,do_daemonize,)
 
1536
    if write_pid:
 
1537
        print >> open(pid_file,'wt'), str(pid)
 
1538
        print "wicd daemon pid %s stored in %r" % (pid,pid_file,)
1537
1539
      
1538
1540
    if redirect_stderr or redirect_stdout: output = LogWriter()
1539
1541
    if redirect_stdout: sys.stdout = output
1564
1566
        ##    print "Exiting as KeyboardInterrupt raised." 
1565
1567
    finally:
1566
1568
        print "Out of mainloop, sigterming wicd-monitor..."
1567
 
        os.kill(child_pid, signal.SIGTERM)
 
1569
        if write_pid:
 
1570
            print "Deleting pid_file: %r" % (pid_file,)
 
1571
            os.unlink(pid_file)
 
1572
        os.kill(child_pid, signal.SIGTERM)  
 
1573
    print "wicd finished."
1568
1574
 
1569
1575
def sigterm_caught(sig, frame):
1570
1576
    """ Called when a SIGTERM is caught, kills monitor.py before exiting. """