# vim: set et ts=4 sw=4: package AdminPanel::Shared::Services; #============================================================= -*-perl-*- =head1 NAME AdminPanel::Shared::Services - shares the API to manage services =head1 SYNOPSIS use AdminPanel::Shared::Services; my ($l, $on_services) = AdminPanel::Shared::Services::services(); =head1 DESCRIPTION This module aims to share all the API to manage system services, to be used from GUI applications or console. From the original code drakx services. =head1 EXPORT description services xinetd_services is_service_running restart_or_start stopService startService restart set_service service_exists start_not_running_service starts_on_boot start_service_on_boot do_not_start_service_on_boot enable disable set_status =head1 SUPPORT You can find documentation for this module with the perldoc command: perldoc AdminPanel::Shared::Services =head1 SEE ALSO AdminPanel::Shared =head1 AUTHOR Angelo Naselli =head1 COPYRIGHT and LICENSE Copyright (C) 2013-2014, Angelo Naselli. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA =head1 FUNCTIONS =cut #-###################################################################################### #- misc imports #-###################################################################################### use strict; use diagnostics; use Sys::Syslog; use File::Basename qw( basename ); use AdminPanel::Shared qw(member); use AdminPanel::Shared::Locales; use lib qw(/usr/lib/libDrakX); use MDK::Common::Func qw(find); use MDK::Common::File qw(cat_); use run_program qw(rooted); use base qw(Exporter); our @EXPORT = qw( description services xinetd_services is_service_running restart_or_start stopService startService restart set_service service_exists start_not_running_service starts_on_boot start_service_on_boot do_not_start_service_on_boot enable disable set_status ); my $loc = AdminPanel::Shared::Locales->new(domain_name => 'libDrakX-standalone'); #============================================================= =head2 description =head3 INPUT name: Service Name =head3 OUTPUT Description: Service description =head3 DESCRIPTION THis function return the description for the given service =cut #============================================================= sub description { my %services = ( acpid => $loc->N_("Listen and dispatch ACPI events from the kernel"), alsa => $loc->N_("Launch the ALSA (Advanced Linux Sound Architecture) sound system"), anacron => $loc->N_("Anacron is a periodic command scheduler."), apmd => $loc->N_("apmd is used for monitoring battery status and logging it via syslog. It can also be used for shutting down the machine when the battery is low."), atd => $loc->N_("Runs commands scheduled by the at command at the time specified when at was run, and runs batch commands when the load average is low enough."), 'avahi-deamon' => $loc->N_("Avahi is a ZeroConf daemon which implements an mDNS stack"), chronyd => $loc->N_("An NTP client/server"), cpufreq => $loc->N_("Set CPU frequency settings"), crond => $loc->N_("cron is a standard UNIX program that runs user-specified programs at periodic scheduled times. vixie cron adds a number of features to the basic UNIX cron, including better security and more powerful configuration options."), cups => $loc->N_("Common UNIX Printing System (CUPS) is an advanced printer spooling system"), dm => $loc->N_("Launches the graphical display manager"), fam => $loc->N_("FAM is a file monitoring daemon. It is used to get reports when files change. It is used by GNOME and KDE"), g15daemon => $loc->N_("G15Daemon allows users access to all extra keys by decoding them and pushing them back into the kernel via the linux UINPUT driver. This driver must be loaded before g15daemon can be used for keyboard access. The G15 LCD is also supported. By default, with no other clients active, g15daemon will display a clock. Client applications and scripts can access the LCD via a simple API."), gpm => $loc->N_("GPM adds mouse support to text-based Linux applications such the Midnight Commander. It also allows mouse-based console cut-and-paste operations, and includes support for pop-up menus on the console."), haldaemon => $loc->N_("HAL is a daemon that collects and maintains information about hardware"), harddrake => $loc->N_("HardDrake runs a hardware probe, and optionally configures new/changed hardware."), httpd => $loc->N_("Apache is a World Wide Web server. It is used to serve HTML files and CGI."), inet => $loc->N_("The internet superserver daemon (commonly called inetd) starts a variety of other internet services as needed. It is responsible for starting many services, including telnet, ftp, rsh, and rlogin. Disabling inetd disables all of the services it is responsible for."), ip6tables => $loc->N_("Automates a packet filtering firewall with ip6tables"), iptables => $loc->N_("Automates a packet filtering firewall with iptables"), irqbalance => $loc->N_("Evenly distributes IRQ load across multiple CPUs for enhanced performance"), keytable => $loc->N_("This package loads the selected keyboard map as set in /etc/sysconfig/keyboard. This can be selected using the kbdconfig utility. You should leave this enabled for most machines."), kheader => $loc->N_("Automatic regeneration of kernel header in /boot for /usr/include/linux/{autoconf,version}.h"), kudzu => $loc->N_("Automatic detection and configuration of hardware at boot."), 'laptop-mode' => $loc->N_("Tweaks system behavior to extend battery life"), linuxconf => $loc->N_("Linuxconf will sometimes arrange to perform various tasks at boot-time to maintain the system configuration."), lpd => $loc->N_("lpd is the print daemon required for lpr to work properly. It is basically a server that arbitrates print jobs to printer(s)."), lvs => $loc->N_("Linux Virtual Server, used to build a high-performance and highly available server."), mandi => $loc->N_("Monitors the network (Interactive Firewall and wireless"), mdadm => $loc->N_("Software RAID monitoring and management"), messagebus => $loc->N_("DBUS is a daemon which broadcasts notifications of system events and other messages"), msec => $loc->N_("Enables MSEC security policy on system startup"), named => $loc->N_("named (BIND) is a Domain Name Server (DNS) that is used to resolve host names to IP addresses."), netconsole => $loc->N_("Initializes network console logging"), netfs => $loc->N_("Mounts and unmounts all Network File System (NFS), SMB (Lan Manager/Windows), and NCP (NetWare) mount points."), network => $loc->N_("Activates/Deactivates all network interfaces configured to start at boot time."), 'network-auth' => $loc->N_("Requires network to be up if enabled"), 'network-up' => $loc->N_("Wait for the hotplugged network to be up"), nfs => $loc->N_("NFS is a popular protocol for file sharing across TCP/IP networks. This service provides NFS server functionality, which is configured via the /etc/exports file."), nfslock => $loc->N_("NFS is a popular protocol for file sharing across TCP/IP networks. This service provides NFS file locking functionality."), ntpd => $loc->N_("Synchronizes system time using the Network Time Protocol (NTP)"), numlock => $loc->N_("Automatically switch on numlock key locker under console and Xorg at boot."), oki4daemon => $loc->N_("Support the OKI 4w and compatible winprinters."), partmon => $loc->N_("Checks if a partition is close to full up"), pcmcia => $loc->N_("PCMCIA support is usually to support things like ethernet and modems in laptops. It will not get started unless configured so it is safe to have it installed on machines that do not need it."), portmap => $loc->N_("The portmapper manages RPC connections, which are used by protocols such as NFS and NIS. The portmap server must be running on machines which act as servers for protocols which make use of the RPC mechanism."), portreserve => $loc->N_("Reserves some TCP ports"), postfix => $loc->N_("Postfix is a Mail Transport Agent, which is the program that moves mail from one machine to another."), random => $loc->N_("Saves and restores system entropy pool for higher quality random number generation."), rawdevices => $loc->N_("Assign raw devices to block devices (such as hard disk drive partitions), for the use of applications such as Oracle or DVD players"), resolvconf => $loc->N_("Nameserver information manager"), routed => $loc->N_("The routed daemon allows for automatic IP router table updated via the RIP protocol. While RIP is widely used on small networks, more complex routing protocols are needed for complex networks."), rstatd => $loc->N_("The rstat protocol allows users on a network to retrieve performance metrics for any machine on that network."), rsyslog => $loc->N_("Syslog is the facility by which many daemons use to log messages to various system log files. It is a good idea to always run rsyslog."), rusersd => $loc->N_("The rusers protocol allows users on a network to identify who is logged in on other responding machines."), rwhod => $loc->N_("The rwho protocol lets remote users get a list of all of the users logged into a machine running the rwho daemon (similar to finger)."), saned => $loc->N_("SANE (Scanner Access Now Easy) enables to access scanners, video cameras, ..."), shorewall => $loc->N_("Packet filtering firewall"), smb => $loc->N_("The SMB/CIFS protocol enables to share access to files & printers and also integrates with a Windows Server domain"), sound => $loc->N_("Launch the sound system on your machine"), 'speech-dispatcherd' => $loc->N_("layer for speech analysis"), sshd => $loc->N_("Secure Shell is a network protocol that allows data to be exchanged over a secure channel between two computers"), syslog => $loc->N_("Syslog is the facility by which many daemons use to log messages to various system log files. It is a good idea to always run syslog."), 'udev-post' => $loc->N_("Moves the generated persistent udev rules to /etc/udev/rules.d"), usb => $loc->N_("Load the drivers for your usb devices."), vnStat => $loc->N_("A lightweight network traffic monitor"), xfs => $loc->N_("Starts the X Font Server."), xinetd => $loc->N_("Starts other deamons on demand."), ); my ($name) = @_; my $s = $services{$name}; if ($s) { $s = $loc->N($s); } else { my $file = "$::prefix/usr/lib/systemd/system/$name.service"; if (-e $file) { $s = MDK::Common::File::cat_($file); $s = $s =~ /^Description=(.*)/mg ? $1 : ''; } else { $file = MDK::Common::Func::find { -e $_ } map { "$::prefix$_/$name" } '/etc/rc.d/init.d', '/etc/init.d', '/etc/xinetd.d'; $s = MDK::Common::File::cat_($file); $s =~ s/\\\s*\n#\s*//mg; $s = $s =~ /^#\s+(?:Short-)?[dD]escription:\s+(.*?)^(?:[^#]|# {0,2}\S)/sm ? $1 : $s =~ /^#\s*(.*?)^[^#]/sm ? $1 : ''; $s =~ s/#\s*//mg; } } $s =~ s/\n/ /gm; $s =~ s/\s+$//; $s; } #============================================================= =head2 set_service =head3 INPUT $service: Service name $enable: enable/disable service =head3 DESCRIPTION This function enable/disable at boot the given service =cut #============================================================= sub set_service { my ($service, $enable) = @_; my @xinetd_services = map { $_->[0] } xinetd_services(); if (AdminPanel::Shared::member($service, @xinetd_services)) { $ENV{PATH} = "/usr/bin:/usr/sbin"; run_program::rooted($::prefix, "/usr/sbin/chkconfig", $enable ? "--add" : "--del", $service); } elsif (_running_systemd() || _has_systemd()) { # systemctl rejects any symlinked units. You have to enabled the real file if (-l "/lib/systemd/system/$service.service") { my $name = readlink("/lib/systemd/system/$service.service"); $service = File::Basename::basename($name); } else { $service = $service . ".service"; } $ENV{PATH} = "/usr/bin:/usr/sbin"; run_program::rooted($::prefix, "/usr/bin/systemctl", $enable ? "enable" : "disable", $service); } else { my $script = "/etc/rc.d/init.d/$service"; $ENV{PATH} = "/usr/bin:/usr/sbin"; run_program::rooted($::prefix, "/usr/sbin/chkconfig", $enable ? "--add" : "--del", $service); #- FIXME: handle services with no chkconfig line and with no Default-Start levels in LSB header if ($enable && MDK::Common::File::cat_("$::prefix$script") =~ /^#\s+chkconfig:\s+-/m) { $ENV{PATH} = "/usr/bin:/usr/sbin"; run_program::rooted($::prefix, "/usr/sbin/chkconfig", "--level", "35", $service, "on"); } } } sub _run_action { my ($service, $action, $do_not_block) = @_; if (_running_systemd()) { if ($do_not_block) { $ENV{PATH} = "/usr/bin:/usr/sbin"; run_program::rooted($::prefix, '/usr//bin/systemctl', '--no-block', $action, "$service.service"); } else { $ENV{PATH} = "/usr/bin:/usr/sbin"; run_program::rooted($::prefix, '/usr/bin/systemctl', $action, "$service.service"); } } else { $ENV{PATH} = "/usr/bin:/usr/sbin:/etc/rc.d/init.d/"; run_program::rooted($::prefix, "/etc/rc.d/init.d/$service", $action); } } sub _running_systemd() { $ENV{PATH} = "/usr/bin:/usr/sbin"; run_program::rooted($::prefix, '/usr/bin/mountpoint', '-q', '/sys/fs/cgroup/systemd'); } sub _has_systemd() { $ENV{PATH} = "/usr/bin:/usr/sbin"; run_program::rooted($::prefix, '/usr/bin/rpm', '-q', 'systemd'); } #============================================================= =head2 xinetd_services =head3 OUTPUT xinetd_services: All the xinetd services =head3 DESCRIPTION This functions returns all the xinetd services in the system. NOTE that xinetd *must* be enable at boot to get this info =cut #============================================================= sub xinetd_services() { local $ENV{LANGUAGE} = 'C'; my @xinetd_services; $ENV{PATH} = "/usr/bin:/usr/sbin"; foreach (run_program::rooted_get_stdout($::prefix, '/usr/sbin/chkconfig', '--list', '--type', 'xinetd')) { if (my ($xinetd_name, $on_off) = m!^\t(\S+):\s*(on|off)!) { push @xinetd_services, [ $xinetd_name, $on_off eq 'on' ]; } } @xinetd_services; } sub _systemd_services() { local $ENV{LANGUAGE} = 'C'; my @services; my %loaded; # Running system using systemd Sys::Syslog::syslog('info|local1', "Detected systemd running. Using systemctl introspection."); foreach (run_program::rooted_get_stdout($::prefix, '/usr/bin/systemctl', '--full', '--all', 'list-units')) { if (my ($name) = m!^(\S+)\.service\s+loaded!) { # We only look at non-template, non-linked service files in /lib # We also check for any non-masked sysvinit files as these are # also handled by systemd if ($name !~ /.*\@$/g && (-e "$::prefix/lib/systemd/system/$name.service" or -e "$::prefix/etc/rc.d/init.d/$name") && ! -l "$::prefix/lib/systemd/system/$name.service") { push @services, [ $name, !!run_program::rooted($::prefix, '/usr/bin/systemctl', '--quiet', 'is-enabled', "$name.service") ]; $loaded{$name} = 1; } } } # list-units will not list disabled units that can be enabled foreach (run_program::rooted_get_stdout($::prefix, '/usr/bin/systemctl', '--full', 'list-unit-files')) { if (my ($name) = m!^(\S+)\.service\s+disabled!) { # We only look at non-template, non-linked service files in /lib # We also check for any non-masked sysvinit files as these are # also handled by systemd if (!exists $loaded{$name} && $name !~ /.*\@$/g && (-e "$::prefix/lib/systemd/system/$name.service" or -e "$::prefix/etc/rc.d/init.d/$name") && ! -l "$::prefix/lib/systemd/system/$name.service") { # Limit ourselves to "standard" targets which can be enabled my $wantedby = MDK::Common::File::cat_("$::prefix/lib/systemd/system/$name.service") =~ /^WantedBy=(graphical|multi-user).target$/sm ? $1 : ''; if ($wantedby) { push @services, [ $name, 0 ]; } } } } @services; } sub _legacy_services() { local $ENV{LANGUAGE} = 'C'; my @services; my $has_systemd = _has_systemd(); if ($has_systemd) { # The system not using systemd but will be at next boot. This is # is typically the case in the installer. In this mode we must read # as much as is practicable from the native systemd unit files and # combine that with information from chkconfig regarding legacy sysvinit # scripts (which systemd will parse and include when running) Sys::Syslog::syslog('info|local1', "Detected systemd installed. Using fake service+chkconfig introspection."); foreach (glob_("$::prefix/lib/systemd/system/*.service")) { my ($name) = m!([^/]*).service$!; # We only look at non-template, non-symlinked service files if (!(/.*\@\.service$/g) && ! -l $_) { # Limit ourselves to "standard" targets my $wantedby = MDK::Common::File::cat_($_) =~ /^WantedBy=(graphical|multi-user).target$/sm ? $1 : ''; if ($wantedby) { # Exclude if enabled statically # Note DO NOT use -e when testing for files that could # be symbolic links as this will fail under a chroot # setup where -e will fail if the symlink target does # exist which is typically the case when viewed outside # of the chroot. if (!-l "$::prefix/lib/systemd/system/$wantedby.target.wants/$name.service") { push @services, [ $name, !!-l "$::prefix/etc/systemd/system/$wantedby.target.wants/$name.service" ]; } } } } } else { Sys::Syslog::syslog('info|local1', "Could not detect systemd. Using chkconfig service introspection."); } # Regardless of whether we expect to use systemd on next boot, we still # need to instrospect information about non-systemd native services. my $runlevel; my $on_off; if (!$::isInstall) { $runlevel = (split " ", `/sbin/runlevel`)[1]; } foreach (run_program::rooted_get_stdout($::prefix, '/sbin/chkconfig', '--list', '--type', 'sysv')) { if (my ($name, $l) = m!^(\S+)\s+(0:(on|off).*)!) { # If we expect to use systemd (i.e. installer) only show those # sysvinit scripts which are not masked by a native systemd unit. my $has_systemd_unit = _systemd_unit_exists($name); if (!$has_systemd || !$has_systemd_unit) { if ($::isInstall) { $on_off = $l =~ /\d+:on/g; } else { $on_off = $l =~ /$runlevel:on/g; } push @services, [ $name, $on_off ]; } } } @services; } #- returns: #--- the listref of installed services #--- the listref of "on" services #============================================================= =head2 services =head3 OUTPUT @l: all the system services @on_services: all the services that start at boot =head3 DESCRIPTION This function returns two lists, all the system service and all the active ones. =cut #============================================================= sub services() { my @Services; if (_running_systemd()) { @Services = _systemd_services(); } else { @Services = _legacy_services(); } my @l = xinetd_services(); push @l, @Services; @l = sort { $a->[0] cmp $b->[0] } @l; [ map { $_->[0] } @l ], [ map { $_->[0] } grep { $_->[1] } @l ]; } sub _systemd_unit_exists { my ($name) = @_; # we test with -l as symlinks are not valid when the system is chrooted: -e "$::prefix/lib/systemd/system/$name.service" or -l "$::prefix/lib/systemd/system/$name.service"; } #============================================================= =head2 service_exists =head3 INPUT $service: Service name =head3 OUTPUT 0/1: if the service exists =head3 DESCRIPTION This function checks if a service is installed by looking for its unit or init.d service =cut #============================================================= sub service_exists { my ($service) = @_; -x "$::prefix/etc/rc.d/init.d/$service" or _systemd_unit_exists($service); } #============================================================= =head2 restart =head3 INPUT $service: Service to restart =head3 DESCRIPTION This function restarts a given service =cut #============================================================= sub restart ($) { my ($service) = @_; # Exit silently if the service is not installed service_exists($service) or return 1; _run_action($service, "restart"); } #============================================================= =head2 restart_or_start =head3 INPUT $service: Service to restart or start =head3 DESCRIPTION This function starts a given service if it is not running, it restarts that otherwise =cut #============================================================= sub restart_or_start ($) { my ($service) = @_; # Exit silently if the service is not installed service_exists($service) or return 1; _run_action($service, is_service_running($service) ? "restart" : "start"); } #============================================================= =head2 startService =head3 INPUT $service: Service to start =head3 DESCRIPTION This function starts a given service =cut #============================================================= sub startService ($) { my ($service) = @_; # Exit silently if the service is not installed service_exists($service) or return 1; _run_action($service, "start"); } #============================================================= =head2 start_not_running_service =head3 INPUT $service: Service to start =head3 DESCRIPTION This function starts a given service if not running =cut #============================================================= sub start_not_running_service ($) { my ($service) = @_; # Exit silently if the service is not installed service_exists($service) or return 1; is_service_running($service) || _run_action($service, "start"); } #============================================================= =head2 stopService =head3 INPUT $service: Service to stop =head3 DESCRIPTION This function stops a given service =cut #============================================================= sub stopService ($) { my ($service) = @_; # Exit silently if the service is not installed service_exists($service) or return 1; _run_action($service, "stop"); } #============================================================= =head2 is_service_running =head3 INPUT $service: Service to check =head3 DESCRIPTION This function returns if the given service is running =cut #============================================================= sub is_service_running ($) { my ($service) = @_; # Exit silently if the service is not installed service_exists($service) or return 0; my $out; if (_running_systemd()) { $ENV{PATH} = "/usr/bin:/usr/sbin"; $out = run_program::rooted($::prefix, '/usr/bin/systemctl', '--quiet', 'is-active', "$service.service"); } else { $ENV{PATH} = "/usr/bin:/usr/sbin"; $out = run_program::rooted($::prefix, '/usr/sbin/service', $service, 'status'); } return $out; } #============================================================= =head2 starts_on_boot =head3 INPUT $service: Service name =head3 DESCRIPTION This function returns if the given service starts at boot =cut #============================================================= sub starts_on_boot { my ($service) = @_; my (undef, $on_services) = services(); AdminPanel::Shared::member($service, @$on_services); } #============================================================= =head2 start_service_on_boot =head3 INPUT $service: Service name =head3 DESCRIPTION This function set the given service active at boot =cut #============================================================= sub start_service_on_boot ($) { my ($service) = @_; set_service($service, 1); } #============================================================= =head2 do_not_start_service_on_boot =head3 INPUT $service: Service name =head3 DESCRIPTION This function set the given service disabled at boot =cut #============================================================= sub do_not_start_service_on_boot ($) { my ($service) = @_; set_service($service, 0); } #============================================================= =head2 enable =head3 INPUT $service: Service name $o_dont_apply: do not start it now =head3 DESCRIPTION This function set the given service active at boot and restarts it if o_dont_apply is not given =cut #============================================================= sub enable { my ($service, $o_dont_apply) = @_; start_service_on_boot($service); restart_or_start($service) unless $o_dont_apply; } #============================================================= =head2 disable =head3 INPUT $service: Service name $o_dont_apply: do not stop it now =head3 DESCRIPTION This function set the given service disabled at boot and stops it if o_dont_apply is not given =cut #============================================================= sub disable { my ($service, $o_dont_apply) = @_; do_not_start_service_on_boot($service); stopService($service) unless $o_dont_apply; } #============================================================= =head2 set_status =head3 INPUT $service: Service name $enable: Enable/disable $o_dont_apply: do not start it now =head3 DESCRIPTION This function set the given service to enable/disable at boot and restarts/stops it if o_dont_apply is not given =cut #============================================================= sub set_status { my ($service, $enable, $o_dont_apply) = @_; if ($enable) { enable($service, $o_dont_apply); } else { disable($service, $o_dont_apply); } } 1; n715'>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
# vim: set et ts=4 sw=4:
package AdminPanel::Rpmdragora::pkg;
#*****************************************************************************
#
#  Copyright (c) 2002 Guillaume Cottenceau
#  Copyright (c) 2002-2007 Thierry Vignaud <tvignaud@mandriva.com>
#  Copyright (c) 2003, 2004, 2005 MandrakeSoft SA
#  Copyright (c) 2005-2007 Mandriva SA
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2, as
#  published by the Free Software Foundation.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#*****************************************************************************
#
# $Id: pkg.pm 270160 2010-06-22 19:55:40Z jvictor $

use strict;
use MDK::Common::Func 'any';
use MDK::Common::DataStructure;
use MDK::Common::System;
use MDK::Common::File;
use MDK::Common::Various;

# use lib qw(/usr/lib/libDrakX);
# use common;

use POSIX qw(_exit ceil);
use URPM;
use utf8;
use AdminPanel::rpmdragora;
use AdminPanel::Rpmdragora::open_db;
use AdminPanel::Rpmdragora::gurpm;
use AdminPanel::Rpmdragora::formatting;
use AdminPanel::Rpmdragora::rpmnew;

use AdminPanel::rpmdragora;
use urpm;
use urpm::lock;
use urpm::install;
use urpm::signature;
use urpm::get_pkgs;
use urpm::select;
use urpm::main_loop;
use urpm::args qw();
use urpm::util;
use Carp;

my $loc = AdminPanel::rpmdragora::locale();

use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(
                    $priority_up_alread_warned
                    download_callback
                    extract_header
                    find_installed_version
                    get_pkgs
                    perform_installation
                    perform_removal
                    run_rpm
                    sort_packages
                    );

our $priority_up_alread_warned;

sub sort_packages_biarch {
    sort {
        my ($na, $aa) = $a =~ /^(.*-[^-]+-[^-]+)\.([^.-]+)$/;
        my ($nb, $ab) = $b =~ /^(.*-[^-]+-[^-]+)\.([^.-]+)$/;
        !defined($na) ?
            (!defined($nb) ? 0 : 1) :
            (!defined($nb) ? -1 : $na cmp $nb || ($ab =~ /64$/) <=> ($aa =~ /64$/));
    } @_;
}

sub sort_packages_monoarch {
    sort { uc($a) cmp uc($b) } @_;
}

*sort_packages = MDK::Common::System::arch() =~ /x86_64/ ? \&sort_packages_biarch : \&sort_packages_monoarch;

sub run_rpm {
    foreach (qw(LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT LC_IDENTIFICATION LC_ALL)) {
        local $ENV{$_} = $ENV{$_} . '.UTF-8' if $ENV{$_} && $ENV{$_} !~ /UTF-8/;
    }
    my @l = map { ensure_utf8($_); $_ } run_program::get_stdout(@_);
    wantarray() ? @l : join('', @l);
}


sub extract_header {
    my ($pkg, $urpm, $xml_info, $o_installed_version) = @_;
    my %fields = (
        info      => 'description',
        files     => 'files',
        changelog => 'changelog',
    );
    # already extracted:
    return if $pkg->{$fields{$xml_info}};

    my $p = $pkg->{pkg};

    if (!$p) {
        warn ">> ghost package '$pkg' has no URPM object!!!\n";
        return;
    }

    my $name = $p->fullname;
    # fix extracting info for SRPMS and RPM GPG keys:
    $name =~ s!\.src!!;

    if ($p->flag_installed && !$p->flag_upgrade) {
        my @files = map { MDK::Common::Various::chomp_($_) } run_rpm("rpm -ql $name");
        MDK::Common::DataStructure::add2hash($pkg, { files => [ @files ? @files : $loc->N("(none)") ],
            description => rpm_description(scalar(run_rpm("rpm -q --qf '%{description}' $name"))),
                 changelog => format_changelog_string($o_installed_version, scalar(run_rpm("rpm -q --changelog $name"))) });
    } else {
        my $medium = pkg2medium($p, $urpm);
        my ($local_source, %xml_info_pkgs, $bar_id);
        my $_statusbar_clean_guard = MDK::Common::Func::before_leaving { $bar_id and statusbar_msg_remove($bar_id) };
        my $dir = urpm::file_from_local_url($medium->{url});

        print "p->filename: ". $p->filename."\n";
        $local_source = "$dir/" . $p->filename if $dir;
        print "local_source: " . ($local_source ? $local_source : "") . "\n";

        if ($local_source && -e $local_source) {
            $bar_id = statusbar_msg($loc->N("Getting information from %s...", $dir), 0);
            $urpm->{log}("getting information from rpms from $dir");
        } else {
            my $gurpm;
            $bar_id = statusbar_msg($loc->N("Getting '%s' from XML meta-data...", $xml_info), 0);
            my $_gurpm_clean_guard = MDK::Common::Func::before_leaving { undef $gurpm };
            if (my $xml_info_file = eval { urpm::media::any_xml_info($urpm, $medium, $xml_info, undef, sub {
                $gurpm ||= AdminPanel::Rpmdragora::gurpm->new($loc->N("Please wait"),
                                                              '', # FIXME: add a real string after cooker
                                                              transient => $::main_window);
                download_callback($gurpm, @_)
                or goto header_non_available;
            }) }) {
                require urpm::xml_info;
                require urpm::xml_info_pkg;
                $urpm->{log}("getting information from $xml_info_file");
                my %nodes = eval { urpm::xml_info::get_nodes($xml_info, $xml_info_file, [ $name ]) };
                goto header_non_available if $@;
                MDK::Common::DataStructure::put_in_hash($xml_info_pkgs{$name} ||= {}, $nodes{$name});
            } else {
                if ($xml_info eq 'info') {
                    $urpm->{info}($loc->N("No xml info for medium \"%s\", only partial result for package %s", $medium->{name}, $name));
                } else {
                    $urpm->{error}($loc->N("No xml info for medium \"%s\", unable to return any result for package %s", $medium->{name}, $name));
                }
            }
        }

        #- even if non-root, search for a header in the global cachedir
        if ($local_source && -s $local_source) {
            $p->update_header($local_source) or do {
                warn "Warning, could not extract header for $name from $medium!";
                goto header_non_available;
            };
            my @files = $p->files;
            @files = $loc->N("(none)") if !@files;
            MDK::Common::DataStructure::add2hash($pkg, { description => rpm_description($p->description),
                files => \@files,
                url => $p->url,
                changelog => format_changelog_changelogs($o_installed_version, $p->changelogs) });
            $p->pack_header; # needed in order to call methods on objects outside ->traverse
        } elsif ($xml_info_pkgs{$name}) {
            if ($xml_info eq 'info') {
                MDK::Common::DataStructure::add2hash($pkg, { description => rpm_description($xml_info_pkgs{$name}{description}),
                         url => $xml_info_pkgs{$name}{url}
                });
            } elsif ($xml_info eq 'files') {
                my @files = map { MDK::Common::Various::chomp_($loc->to_utf8($_)) } split("\n", $xml_info_pkgs{$name}{files});
                MDK::Common::DataStructure::add2hash($pkg, { files => scalar(@files) ? \@files : [ $loc->N("(none)") ] });
            } elsif ($xml_info eq 'changelog') {
                MDK::Common::DataStructure::add2hash($pkg, {
                    changelog => format_changelog_changelogs($o_installed_version,
                                                             @{$xml_info_pkgs{$name}{changelogs}})
                });
            }
        } else {
            goto header_non_available;
        }
        return;
        header_non_available:
        MDK::Common::DataStructure::add2hash($pkg, { summary => $p->summary || $loc->N("(Not available)"), description => undef });
    }
}

sub find_installed_version {
    my ($p) = @_;

    my $version = $loc->N("(none)");
    open_rpm_db()->traverse_tag_find('name', $p->name, sub { $version = $_[0]->EVR; return ($version ? 1 : 0) }) if $p;
    return $version;
}

my $canceled;
sub download_callback {
    my ($gurpm, $mode, $file, $percent, $total, $eta, $speed) = @_;
    $canceled = 0;
    if ($mode eq 'start') {
        $gurpm->label($loc->N("Downloading package `%s'...", urpm::util::basename($file)));
        $gurpm->validate_cancel(but($loc->N("Cancel")), sub { $canceled = 1 });
    } elsif ($mode eq 'progress') {
        $gurpm->label(
            join("\n",
                 $loc->N("Downloading package `%s'...", urpm::util::basename($file)),
                 (defined $total && defined $eta ?
                    $loc->N("        %s%% of %s completed, ETA = %s, speed = %s", $percent, $total, $eta, $speed)
                      : $loc->N("        %s%% completed, speed = %s", $percent, $speed)
                  ) =~ /^\s*(.*)/
              ),
        );
	#$gurpm->progress($percenti/100);
        $gurpm->progress(ceil($percent*100));
    } elsif ($mode eq 'end') {
        $gurpm->progress(100);
        $gurpm->invalidate_cancel;
    }
    !$canceled;
}


# -=-=-=---=-=-=---=-=-=-- install packages -=-=-=---=-=-=---=-=-=-

my (@update_medias, $is_update_media_already_asked);

sub warn_about_media {
    my ($w, %options) = @_;

    return if $::MODE ne 'update';
    return if $::rpmdragora_options{'no-media-update'};

    # we use our own instance of the urpmi db in order not to mess up with skip-list managment (#31092):
    # and no need to fully configure urpmi since we may have to do it again anyway because of new media:
    my $urpm = fast_open_urpmi_db();

    my $_lock = urpm::lock::urpmi_db($urpm, undef, wait => $urpm->{options}{wait_lock});

    # build media list:
    @update_medias = get_update_medias($urpm);

    # do not update again media after installing/removing some packages:
    $::rpmdragora_options{'no-media-update'} ||= 1;

	    if (@update_medias > 0) {
		if (!$options{skip_updating_mu} && !$is_update_media_already_asked) {
              $is_update_media_already_asked = 1;
		     $::rpmdragora_options{'no-confirmation'} or interactive_msg($loc->N("Confirmation"),
$loc->N("I need to contact the mirror to get latest update packages.
Please check that your network is currently running.

Is it ok to continue?"), yesno => 1
# TODO                   widget =>  gtknew('CheckButton', text => $loc->N("Do not ask me next time"),
#                                      active_ref => \$::rpmdragora_options{'no-confirmation'}
#                                  )
                                                                        ) or myexit(-1);
		    writeconf();
		    urpm::media::select_media($urpm, map { $_->{name} } @update_medias);
		    update_sources($urpm, noclean => 1, medialist => [ map { $_->{name} } @update_medias ]);
		}
	    } else {
		if (any { $_->{update} } @{$urpm->{media}}) {
		    interactive_msg($loc->N("Already existing update media"),
$loc->N("You already have at least one update medium configured, but
all of them are currently disabled. You should run the Software
Media Manager to enable at least one (check it in the \"%s\"
column).

Then, restart \"%s\".", $loc->N("Enabled"), $AdminPanel::rpmdragora::myname_update));
		    myexit(-1);
		}
		my ($mirror) = choose_mirror($urpm, transient => $w->{real_window} || $::main_window,
                                       message => join("\n\n",
                                                       $loc->N("You have no configured update media. MageiaUpdate cannot operate without any update media."),
                                                       $loc->N("I need to contact the Mageia website to get the mirror list.
Please check that your network is currently running.

Is it ok to continue?"),
                                                         ),
                                   );
		my $m = ref($mirror) ? $mirror->{url} : '';
		$m or interactive_msg($loc->N("How to choose manually your mirror"),
$loc->N("You may also choose your desired mirror manually: to do so,
launch the Software Media Manager, and then add a `Security
updates' medium.

Then, restart %s.", $AdminPanel::rpmdragora::myname_update)), myexit(-1);
		add_distrib_update_media($urpm, $mirror, only_updates => 1);
	    }
}


sub get_parallel_group() {
    $::rpmdragora_options{parallel} ? $::rpmdragora_options{parallel}[0] : undef;
}

my ($count, $level, $limit, $new_stage, $prev_stage, $total);

sub init_progress_bar {
    my ($urpm) = @_;
    undef $_ foreach $count, $prev_stage, $new_stage, $limit;
    $level = 0.05;
    $total = @{$urpm->{depslist}};
}

sub reset_pbar_count {
    undef $prev_stage;
    $count = 0;
    $limit = $_[0];
}

sub update_pbar {
    my ($gurpm) = @_;
    return if !$total;          # don't die if there's no source
    $count++;
    $new_stage = $level+($limit-$level)*$count/$total;
    $prev_stage = 0 if(!defined($prev_stage));
    if ($prev_stage + 0.01*100 < $new_stage) {
        $prev_stage = $new_stage;
        $gurpm->progress(ceil($new_stage));
    }
}


sub get_installed_packages {
    my ($urpm, $db, $all_pkgs, $gurpm) = @_;

    $urpm->{global_config}{'prohibit-remove'} = '' if(!defined($urpm->{global_config}{'prohibit-remove'}));
    my @base = ("basesystem", split /,\s*/, $urpm->{global_config}{'prohibit-remove'});
    my (%base, %basepackages, @installed_pkgs, @processed_base);
    reset_pbar_count(0.33);
    while (defined(local $_ = shift @base)) {
	exists $basepackages{$_} and next;
	$db->traverse_tag(m|^/| ? 'path' : 'whatprovides', [ $_ ], sub {
			      update_pbar($gurpm);
			      my $name = $_[0]->fullname;
			      # workaround looping in URPM:
			      return if MDK::Common::DataStructure::member($name, @processed_base);
			      push @processed_base, $name;
			      push @{$basepackages{$_}}, $name;
			      push @base, $_[0]->requires_nosense;
			  });
    }
    foreach (values %basepackages) {
	my $n = @$_;            #- count number of times it's provided
	foreach (@$_) {
	    $base{$_} = \$n;
	}
    }
    # costly:
    $db->traverse(sub {
                      my ($pkg) = @_;
                      update_pbar($gurpm);
                      my $fullname = urpm_name($pkg);
                      return if $fullname =~ /@/;
                      $all_pkgs->{$fullname} = {
                          pkg => $pkg, urpm_name => $fullname,
                      } if !($all_pkgs->{$fullname} && $all_pkgs->{$fullname}{description});
                      if (my $name = $base{$fullname}) {
                          $all_pkgs->{$fullname}{base} = \$name;
                          $pkg->set_flag_base(1) if $$name == 1;
                      }
                      push @installed_pkgs, $fullname;
                      $pkg->set_flag_installed;
                      $pkg->pack_header; # needed in order to call methods on objects outside ->traverse
                  });
    @installed_pkgs;
}

urpm::select::add_packages_to_priority_upgrade_list('rpmdragora', 'perl-Glib', 'perl-Gtk2');

my ($priority_state, $priority_requested);
our $need_restart;

our $probe_only_for_updates;

sub get_updates_list {
    my ($urpm, $db, $state, $requested, $requested_list, $requested_strict, $all_pkgs, %limit_preselect) = @_;

    $urpm->request_packages_to_upgrade(
	$db,
	$state,
	$requested,
	%limit_preselect
    );

    my %common_opts = (
        callback_choices => \&AdminPanel::Rpmdragora::gui::callback_choices,
        priority_upgrade => $urpm->{options}{'priority-upgrade'},
    );

    if ($urpm->{options}{'priority-upgrade'}) {
        $need_restart =
          urpm::select::resolve_priority_upgrades_after_auto_select($urpm, $db, $state,
                                                                    $requested, %common_opts);
    }

    # list of updates (including those matching /etc/urpmi/skip.list):
    @$requested_list = sort map {
	my $name = urpm_name($_);
        $all_pkgs->{$name} = { pkg => $_ };
	$name;
    } @{$urpm->{depslist}}[keys %$requested];

    # list of pure updates (w/o those matching /etc/urpmi/skip.list but with their deps):
    if ($probe_only_for_updates && !$need_restart) {
        @$requested_strict = sort map {
            urpm_name($_);
        } $urpm->resolve_requested($db, $state, $requested, callback_choices => \&AdminPanel::Rpmdragora::gui::callback_choices);

        if (my @l = grep { $state->{selected}{$_->id} }
              urpm::select::_priority_upgrade_pkgs($urpm, $urpm->{options}{'priority-upgrade'})) {
            if (!$need_restart) {
                $need_restart =
                  urpm::select::_resolve_priority_upgrades($urpm, $db, $state, $state->{selected},
                                                           \@l, %common_opts);
            }
        }
    }

    if ($need_restart) {
        $requested_strict = [ map { scalar $_->fullname } @{$urpm->{depslist}}[keys %{$state->{selected}}] ];
        # drop non priority updates:
        @$requested_list = ();
    }

    # list updates including skiped ones + their deps in MageiaUpdate:
    @$requested_list = MDK::Common::DataStructure::uniq(@$requested_list, @$requested_strict);

    # do not pre select updates in rpmdragora:
    @$requested_strict = () if !$probe_only_for_updates;
}

sub get_pkgs {
    my (%options) = @_;
    my $w = $::main_window;

    my $gurpm = AdminPanel::Rpmdragora::gurpm->new(1 ? $loc->N("Please wait") : $loc->N("Package installation..."), $loc->N("Initializing..."), transient => $::main_window);
    my $_gurpm_clean_guard = MDK::Common::Func::before_leaving { undef $gurpm };
    #my $_flush_guard = Gtk2::GUI_Update_Guard->new;

    warn_about_media($w, %options);

    my $urpm = open_urpmi_db(update => $probe_only_for_updates && !is_it_a_devel_distro());

    my $_drop_lock = MDK::Common::Func::before_leaving { undef $urpm->{lock} };

    $priority_up_alread_warned = 0;

    # update media list in case warn_about_media() added some:
    @update_medias = get_update_medias($urpm);

    $gurpm->label($loc->N("Reading updates description"));
    $gurpm->progress(100);

	#- parse the description file
    my $update_descr = urpm::get_updates_description($urpm, @update_medias);

    my $_unused = $loc->N("Please wait, finding available packages...");

    # find out installed packages:

    init_progress_bar($urpm);

    $gurpm->label($loc->N("Please wait, listing base packages..."));
    $gurpm->progress(ceil($level*100));

    my $db = eval { open_rpm_db() };
    if (my $err = $@) {
	interactive_msg($loc->N("Error"), $loc->N("A fatal error occurred: %s.", $err));
        return;
    }

    my $sig_handler = sub { undef $db; exit 3 };
    local $SIG{INT} = $sig_handler;
    local $SIG{QUIT} = $sig_handler;

    $gurpm->label($loc->N("Please wait, finding installed packages..."));
    $level = 0.33*100;
    $gurpm->progress(ceil($level));
    reset_pbar_count(0.66*100);
    my (@installed_pkgs, %all_pkgs);
    if (!$probe_only_for_updates) {
        @installed_pkgs = get_installed_packages($urpm, $db, \%all_pkgs, $gurpm);
    }

    if (my $group = get_parallel_group()) {
        urpm::media::configure($urpm, parallel => $group);
    }

    # find out availlable packages:

    $urpm->{state} = {};

    $gurpm->label($loc->N("Please wait, finding available packages..."));
    $level = 0.66*100;
    $gurpm->progress(ceil($level));

    check_update_media_version($urpm, @update_medias);

    my $requested = {};
    my $state = {};
    my (@requested, @requested_strict);

    if ($compute_updates->[0] || $::MODE eq 'update') {
        my %filter;
        if ($options{pure_updates}) {
            # limit to packages from update-media (dependencies can still come from other media)
            %filter = (idlist => [ map { $_->{start} .. $_->{end} } @update_medias ]);
        }
        get_updates_list($urpm, $db, $state, $requested, \@requested, \@requested_strict, \%all_pkgs, %filter);
    }

    if ($need_restart) {
        $priority_state = $state;
        $priority_requested = $requested;
    } else {