OpenSolaris_b135/cmd/print/selector/desktop-print-management-applet

#!/usr/perl5/bin/perl
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#
use Errno qw(EINTR :POSIX);

my $SVC_CUPS_SCHEDULER = 'cups/scheduler';
my $SVCPROP = '/usr/bin/svcprop';
my $reexec = 0;

sub svcprop {
        local ($fmri, $property) = @_;
        my $FH;

        open($FH, "$SVCPROP -C -p $property $fmri 2>/dev/null |");
        $result = <$FH>;
        close($FH);

        return ($result);
}

sub print_service {
        my $service;

        $service = svcprop("$SVC_CUPS_SCHEDULER:default", "general/active");
        ($service =~ /true/) && ($service = 'cups') || ($service = 'lp');

        return ($service);
}

sub kill_running_applets {
	$reexec = 1;
        # cups applet daemon
        system("pkill -f '/system-config-printer/applet.py'");
        # lp applet daemon
        system("pkill ospm-applet");
}

sub handle_signal {
        kill_running_applets ();
}

$SIG{USR1} = \&handle_signal;

my $pid = fork();
if (! defined($pid)) {
	die "Error: fork() failed\n";
}
elsif ($pid == 0) {
	my $service =  print_service();
        if ($service eq 'lp') {
                exec('/usr/lib/lp/bin/desktop-print-management-applet');
        }
        else {
                exec('/usr/lib/cups/bin/desktop-print-management-applet');
        }

}
else {
	my $retid;
	while ((($retid = waitpid($pid, 0)) < 0) && $!{EINTR}) {
	} 

	if ($retid == $pid && $reexec) {
        	exec('/usr/bin/desktop-print-management-applet');
	}
}

exit(0);