aboutsummaryrefslogtreecommitdiffstats
path: root/add-syslog
blob: dd51527f42b977ee6362ce25db3e0fccddd50443 (plain)
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
#!/usr/bin/perl
# rpm helper scriptlet to add an entry into default syslog implementation
# $Id$
use Getopt::Std;
use strict;

my @facilities = qw/auth authpriv cron daemon \
                    kern lpr mail mark news syslog \
                    user uucp local0 local1 local2 \
                    local3 local4 local5 local6 local7/;
my %facilities = map { $_ => 1 } @facilities;
my @priorities = qw/debug info notice warning err crit alert emerg/;
my $i;
my %priorities = map { $_ => $i++ } @priorities;

main(@ARGV) unless caller();

sub main {
    my %opts = ( 
        s => '/dev/log',
        m => 'debug',
        M => 'emerg'
    );
    getopts('s:m:M:', \%opts);
    my ($source, $min, $max) = @opts{qw/s m M/};

    die <<EOF if @ARGV < 4;
usage: $0 [options] <pkg> <nb> <facility> <dest>
Available options:
-s <source>   source (default: /dev/log)
-m <priority> min priority (default: debug)
-M <priority> max priority (default: emerg)
EOF
    my ($package, $number, $dest, $facility) = @ARGV;

    # don't do anything for upgrade
    exit(0) if $number == 2;

    # check arguments
    die "invalid facility '$facility'" if $facility && !$facilities{$facility};

    die "invalid min priority '$min'" if $min && ! defined $priorities{$min};
    die "invalid max priority '$max'" if $max && ! defined $priorities{$max};
    die "maximum priority '$max' lower than minimum priority '$min'"
        if $min && $max && ($priorities{$max} < $priorities{$min});

    open(my $fh, '<', '/etc/mandriva-release')
        or die "can't open /etc/mandriva-release: $!";
    my $line = <$fh>;
    $line =~ /^Mandriva Linux release (\d\d\d\d\.\d)/;
    my $release = $1;
    close($fh);

    # add an entry to default syslog implementation, if installed
    if (version->parse($release) < version->parse("2010.1")) {
        add_sysklogd_entry($package, $source, $dest, $facility, $min, $max)
            if -f '/etc/init.d/syslog';
    } else {
        add_rsyslog_entry($package, $source, $dest, $facility, $min, $max)
            if -f '/etc/init.d/rsyslog';
    }
}

sub add_sysklogd_entry {
    my ($package, $source, $dest, $facility, $min, $max) = @_;

    # ensure source is configured
    add_new_source($source, '/etc/sysconfig/syslog')
        if $source ne '/dev/log';

    # compute selector
    my $selector = get_selector($facility, $min, $max);

    # compute spacing to keep default configuration file formatting
    my $tabs = length($selector) < 48 ?
        ((48 - length($selector)) / 8) :
        1;

    # append entry
    open(my $out, '>>', '/etc/syslog.conf')
        or die "Can't open /etc/syslog.conf for appending: $!";
    print $out "# BEGIN: Automatically added by $package installation\n";
    print $out "$selector" . ("\t" x $tabs) . "-$dest\n";
    print $out "# END\n";
    close($out);

    # relaunch syslog
    system('service syslog condrestart 2>&1 >/dev/null');
}

sub add_rsyslog_entry {
    my ($package, $source, $dest, $facility, $min, $max) = @_;

    # compute selector
    my $selector = get_selector($facility, $min, $max);

    # append entry
    open(my $out, '>', "/etc/rsyslog.d/$package.conf")
        or die "Can't open /etc/rsyslog.d/$package.conf for writing: $!";
    print $out "# Automatically added by $package installation\n";
    print $out "\$AddUnixListenSocket $source\n" if $source ne '/dev/log';
    print $out "$selector\t-$dest\n";
    close($out);

    # relaunch rsyslog
    system('service rsyslog condrestart 2>&1 >/dev/null');
}

sub add_new_source {
    my ($source, $file) = @_;

    my ($content, $changed);
    open(my $in, '<', $file)
        or die "Can't open $file for reading: $!";

    while (my $line = <$in>) {
        if ($line =~ /^SYSLOGD_OPTIONS=(.*)/) {
            my $options = $1;
            if ($options) {
                my $quote;
                if ($options !~ /-a\s+$source/) {
                    if ($options =~ /^(["'])(.*)\1$/) {
                        $quote = $1;
                        $options = $2;
                    } else {
                        $quote = '"';
                    }
                    $options = $quote . $options . " -a $source" . $quote;
                    $changed = 1;
                }
            } else {
                $options = "\"-a $source\"";
                $changed = 1;
            }

            $content .= "SYSLOGD_OPTIONS=$options\n";
        } else {
            $content .= $line;
        }
    }
    close($in);

    if ($changed) {
        open(my $out, '>', $file)
            or die "Can't open $file for writing: $!";
        print $out $content;
        close($out);
    }
}

sub get_selector {
    my ($facility, $min, $max) = @_;

    my $selector;
    if ($max eq 'emerg') {
        if ($min eq 'debug') {
            $selector = "$facility.*";
        } else {
            $selector = "$facility.$min";
        }
    } else {
        for my $i ($priorities{$min} .. $priorities{$max}) {
            $selector .= ';' if $selector;
            $selector .= "$facility.=$priorities[$i]";
        }
    }

    return $selector;
}

1;
d='n260' href='#n260'>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
/*
 * Copyright 1999 Egbert Eich
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of the authors not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  The authors makes no representations
 * about the suitability of this software for any purpose.  It is provided
 * "as is" without express or implied warranty.
 *
 * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */
#include <fcntl.h>
#include <unistd.h>
#include <malloc.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#if defined (__alpha__) || defined (__ia64__)
#include <sys/io.h>
#endif
#include "AsmMacros.h"

#include "pci.h"

#define RESORT 1
#define FIX_ROM 0

/*
 * I'm rather simple mindend - therefore I do a poor man's
 * pci scan without all the fancy stuff that is done in
 * scanpci. However that's all we need.
 */

PciStructPtr PciStruct = NULL;
PciBusPtr PciBuses = NULL;
PciStructPtr CurrentPci = NULL;
PciStructPtr PciList = NULL;
int pciMaxBus = 0;

static CARD32 PciCfg1Addr;

static void readConfigSpaceCfg1(CARD32 bus, CARD32 dev, CARD32 func,
				CARD32 *reg);
static int checkSlotCfg1(CARD32 bus, CARD32 dev, CARD32 func);
static int checkSlotCfg2(CARD32 bus, int dev);
static void readConfigSpaceCfg2(CARD32 bus, int dev, CARD32 *reg);
static CARD8 interpretConfigSpace(CARD32 *reg, int busidx,
				  CARD8 dev, CARD8 func);
static CARD32 findBIOSMap(PciStructPtr pciP, CARD32 *biosSize);
static void restoreMem(PciStructPtr pciP);


#ifdef __alpha__
#define PCI_BUS_FROM_TAG(tag)  (((tag) & 0x00ff0000) >> 16)
#define PCI_DFN_FROM_TAG(tag) (((tag) & 0x0000ff00) >> 8)

#include <asm/unistd.h>

CARD32
axpPciCfgRead(CARD32 tag)
{
    int bus, dfn;
    CARD32 val = 0xffffffff;
    
    bus = PCI_BUS_FROM_TAG(tag);
    dfn = PCI_DFN_FROM_TAG(tag);
    
    syscall(__NR_pciconfig_read, bus, dfn, tag & 0xff, 4, &val);
    return(val);
}

void
axpPciCfgWrite(CARD32 tag, CARD32 val)
{
    int bus, dfn;
    
    bus = PCI_BUS_FROM_TAG(tag);
    dfn = PCI_DFN_FROM_TAG(tag);
    
    syscall(__NR_pciconfig_write, bus, dfn, tag & 0xff, 4, &val);
}

static CARD32 (*readPci)(CARD32 reg) = axpPciCfgRead;
static void (*writePci)(CARD32 reg, CARD32 val) = axpPciCfgWrite;
#else
static CARD32 readPciCfg1(CARD32 reg);
static void writePciCfg1(CARD32 reg, CARD32 val);
#ifndef __ia64__
static CARD32 readPciCfg2(CARD32 reg);
static void writePciCfg2(CARD32 reg, CARD32 val);
#endif

static CARD32 (*readPci)(CARD32 reg) = readPciCfg1;
static void (*writePci)(CARD32 reg, CARD32 val) = writePciCfg1;
#endif

#if defined(__alpha__) || defined(__sparc__)
#define PCI_EN 0x00000000
#else
#define PCI_EN 0x80000000
#endif


static int numbus;
static int hostbridges = 1;
static unsigned long pciMinMemReg = ~0;



void
scan_pci(int pci_cfg_method)
{
    unsigned short configtype;
    
    CARD32 reg[64];
    int busidx;
    CARD8 cardnum;
    CARD8 func;
    int idx;
    
    PciStructPtr pci1;
    PciBusPtr pci_b1,pci_b2;
    
    if(pci_cfg_method) {
      configtype = pci_cfg_method;
    }
    else {
#if defined(__alpha__) || defined(__powerpc__) || defined(__sparc__) || defined(__ia64__)
    configtype = 1;
#else
    CARD8 tmp1, tmp2;
    CARD32 tmp32_1, tmp32_2;
    outb(PCI_MODE2_ENABLE_REG, 0x00);
    outb(PCI_MODE2_FORWARD_REG, 0x00);
    tmp1 = inb(PCI_MODE2_ENABLE_REG);
    tmp2 = inb(PCI_MODE2_FORWARD_REG);
    if ((tmp1 == 0x00) && (tmp2 == 0x00)) {
		configtype = 2;
		readPci = readPciCfg2;
		writePci = writePciCfg2;
    } else {
		tmp32_1 = inl(PCI_MODE1_ADDRESS_REG);
		outl(PCI_MODE1_ADDRESS_REG, PCI_EN);
		tmp32_2 = inl(PCI_MODE1_ADDRESS_REG);
		outl(PCI_MODE1_ADDRESS_REG, tmp32_1);
		if (tmp32_2 == PCI_EN) {
			configtype = 1;
		} else {
			return;
		}
    }
#endif
    }
    
    if (configtype == 1) {
		busidx = 0;
		numbus = 1;
		idx = 0;
		do {
			for (cardnum = 0; cardnum < MAX_DEV_PER_VENDOR_CFG1; cardnum++) {
				func = 0;
				do {
					/* loop over the different functions, if present */
				    if (!checkSlotCfg1(busidx,cardnum,func)) {
						if (!func)
							break;
						else {
							func++;
							continue;
						}
				    }
					readConfigSpaceCfg1(busidx,cardnum,func,reg);
		    
					func = interpretConfigSpace(reg,busidx,
												cardnum,func);
		    
					if (++idx >= MAX_PCI_DEVICES)
						break;
				} while (func < 8);
				if (idx >= MAX_PCI_DEVICES)
				    break;
			}
			if (idx >= MAX_PCI_DEVICES)
			    break;
		} while (++busidx < PCI_MAXBUS);
#if defined(__alpha__) || defined(__powerpc__) || defined(__sparc__) || defined(__ia64__)
		/* don't use outl()  ;-) */
#else
		outl(PCI_MODE1_ADDRESS_REG, 0);
#endif
    } else {
		int slot;
	
		busidx = 0;
		numbus = 1;
		idx = 0;
		do {
			for (slot=0xc0; slot<0xd0; slot++) {
				if (!checkSlotCfg2(busidx,slot))
					break;
				readConfigSpaceCfg2(busidx,slot,reg);
		
				interpretConfigSpace(reg,busidx,
									 slot,0);
				if (++idx >= MAX_PCI_DEVICES)
					break;
			}
			if (idx >= MAX_PCI_DEVICES)
			    break;
		}  while (++busidx < PCI_MAXBUS);
    }
    
    
    pciMaxBus = numbus - 1;
    
    /* link buses */
    pci_b1 = PciBuses;
    while (pci_b1) {
		pci_b2 = PciBuses;
		pci_b1->pBus = NULL;
		while (pci_b2) {
			if (pci_b1->primary == pci_b2->secondary)
				pci_b1->pBus = pci_b2;
			pci_b2 = pci_b2->next;
		}
		pci_b1 = pci_b1->next;
    }
    pci1 = PciStruct;
    while (pci1) {
		pci_b2 = PciBuses;
		pci1->pBus = NULL;
		while (pci_b2) {
			if (pci1->bus == pci_b2->secondary)
				pci1->pBus = pci_b2;
			pci_b2 = pci_b2->next;
		}
		pci1 = pci1->next;
    }
    if (RESORT) {
		PciStructPtr tmp = PciStruct, tmp1;
		PciStruct = NULL;
		while (tmp) {
			tmp1 = tmp->next;
			tmp->next = PciStruct;
			PciStruct = tmp;
			tmp = tmp1;
		}
    }
    PciList = CurrentPci = PciStruct;
}

#ifndef __alpha__
static CARD32
readPciCfg1(CARD32 reg)
{
    CARD32 val;
    
    outl(PCI_MODE1_ADDRESS_REG, reg);
    val = inl(PCI_MODE1_DATA_REG);
    outl(PCI_MODE1_ADDRESS_REG, 0);
    return val;
}

static void
writePciCfg1(CARD32 reg, CARD32 val)
{
    outl(PCI_MODE1_ADDRESS_REG, reg);
    outl(PCI_MODE1_DATA_REG,val);
    outl(PCI_MODE1_ADDRESS_REG, 0);
}

#ifndef __ia64__
static CARD32
readPciCfg2(CARD32 reg)
{
    CARD32 val;
    CARD8 bus = (reg >> 16) & 0xff;
    CARD8 dev = (reg >> 11) & 0x1f;
    CARD8 num = reg & 0xff;
    
    outb(PCI_MODE2_ENABLE_REG, 0xF1);
    outb(PCI_MODE2_FORWARD_REG, bus);
    val = inl((dev << 8) + num);
    outb(PCI_MODE2_ENABLE_REG, 0x00);
    return val;
}

static void
writePciCfg2(CARD32 reg, CARD32 val)
{
    CARD8 bus = (reg >> 16) & 0xff;
    CARD8 dev = (reg >> 11) & 0x1f;
    CARD8 num = reg & 0xff;

    outb(PCI_MODE2_ENABLE_REG, 0xF1);
    outb(PCI_MODE2_FORWARD_REG, bus);
    outl((dev << 8) + num,val);
    outb(PCI_MODE2_ENABLE_REG, 0x00);
}
#endif
#endif

void
pciVideoDisable(void)
{
    /* disable VGA routing on bridges */
    PciBusPtr pbp = PciBuses;
    PciStructPtr pcp = PciStruct;
    
    while (pbp) {
		writePci(pbp->Slot.l | 0x3c, pbp->bctl & ~(CARD32)(8<<16));
		pbp = pbp->next;
    }
    /* disable display devices */
    while (pcp) {
		writePci(pcp->Slot.l | 0x04, pcp->cmd_st & ~(CARD32)3);