6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo#!/usr/perl5/bin/perl
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo#
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo# CDDL HEADER START
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo#
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo# The contents of this file are subject to the terms of the
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo# Common Development and Distribution License (the "License").
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo# You may not use this file except in compliance with the License.
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo#
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo# or http://www.opensolaris.org/os/licensing.
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo# See the License for the specific language governing permissions
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo# and limitations under the License.
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo#
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo# When distributing Covered Code, include this CDDL HEADER in each
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo# If applicable, add the following below this CDDL HEADER, with the
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo# fields enclosed by brackets "[]" replaced with your own identifying
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo# information: Portions Copyright [yyyy] [name of copyright owner]
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo#
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo# CDDL HEADER END
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo#
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo# Use is subject to license terms.
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo#
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo#
4d55575c4716bff317c5c79721ff1559769462d6Ghee Teouse Errno qw(EINTR :POSIX);
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teomy $SVC_CUPS_SCHEDULER = 'cups/scheduler';
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teomy $SVCPROP = '/usr/bin/svcprop';
4d55575c4716bff317c5c79721ff1559769462d6Ghee Teomy $reexec = 0;
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teosub svcprop {
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo local ($fmri, $property) = @_;
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo my $FH;
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo open($FH, "$SVCPROP -C -p $property $fmri 2>/dev/null |");
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo $result = <$FH>;
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo close($FH);
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo return ($result);
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo}
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teosub print_service {
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo my $service;
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo $service = svcprop("$SVC_CUPS_SCHEDULER:default", "general/active");
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo ($service =~ /true/) && ($service = 'cups') || ($service = 'lp');
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo return ($service);
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo}
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teosub kill_running_applets {
4d55575c4716bff317c5c79721ff1559769462d6Ghee Teo $reexec = 1;
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo # cups applet daemon
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo system("pkill -f '/system-config-printer/applet.py'");
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo # lp applet daemon
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo system("pkill ospm-applet");
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo}
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teosub handle_signal {
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo kill_running_applets ();
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo}
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo
4d55575c4716bff317c5c79721ff1559769462d6Ghee Teo$SIG{USR1} = \&handle_signal;
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo
4d55575c4716bff317c5c79721ff1559769462d6Ghee Teomy $pid = fork();
4d55575c4716bff317c5c79721ff1559769462d6Ghee Teoif (! defined($pid)) {
4d55575c4716bff317c5c79721ff1559769462d6Ghee Teo die "Error: fork() failed\n";
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo}
4d55575c4716bff317c5c79721ff1559769462d6Ghee Teoelsif ($pid == 0) {
4d55575c4716bff317c5c79721ff1559769462d6Ghee Teo my $service = print_service();
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo if ($service eq 'lp') {
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo exec('/usr/lib/lp/bin/desktop-print-management-applet');
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo }
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo else {
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo exec('/usr/lib/cups/bin/desktop-print-management-applet');
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo }
4d55575c4716bff317c5c79721ff1559769462d6Ghee Teo
4d55575c4716bff317c5c79721ff1559769462d6Ghee Teo}
4d55575c4716bff317c5c79721ff1559769462d6Ghee Teoelse {
4d55575c4716bff317c5c79721ff1559769462d6Ghee Teo my $retid;
4d55575c4716bff317c5c79721ff1559769462d6Ghee Teo while ((($retid = waitpid($pid, 0)) < 0) && $!{EINTR}) {
4d55575c4716bff317c5c79721ff1559769462d6Ghee Teo }
4d55575c4716bff317c5c79721ff1559769462d6Ghee Teo
4d55575c4716bff317c5c79721ff1559769462d6Ghee Teo if ($retid == $pid && $reexec) {
4d55575c4716bff317c5c79721ff1559769462d6Ghee Teo exec('/usr/bin/desktop-print-management-applet');
4d55575c4716bff317c5c79721ff1559769462d6Ghee Teo }
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo}
6124874e2cec65f2e7a974b3833b05dbf2a7d905Ghee Teo
4d55575c4716bff317c5c79721ff1559769462d6Ghee Teoexit(0);