1# $OpenBSD: Ospfd.pm,v 1.4 2020/01/30 13:03:46 bluhm Exp $ 2 3# Copyright (c) 2010-2014 Alexander Bluhm <bluhm@openbsd.org> 4# Copyright (c) 2014 Florian Riehm <mail@friehm.de> 5# 6# Permission to use, copy, modify, and distribute this software for any 7# purpose with or without fee is hereby granted, provided that the above 8# copyright notice and this permission notice appear in all copies. 9# 10# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 18use strict; 19use warnings; 20 21package Ospfd; 22use parent 'Proc'; 23use Carp; 24use File::Basename; 25 26sub new { 27 my $class = shift; 28 my %args = @_; 29 $args{logfile} ||= "ospfd.log"; 30 $args{up} ||= "startup"; 31 $args{down} ||= "terminating"; 32 $args{func} = sub { Carp::confess "$class func may not be called" }; 33 $args{conffile} ||= "ospfd.conf"; 34 my $self = Proc::new($class, %args); 35 36 # generate ospfd.conf from config keys in %args 37 unlink $self->{conffile}; 38 open(my $fh, '>', $self->{conffile}) 39 or die ref($self), " conf file $self->{conffile} create failed: $!"; 40 my %global_conf = %{$args{conf}{global}}; 41 foreach my $key (keys %global_conf) { 42 print $fh "$key $global_conf{$key}\n"; 43 } 44 my %area_conf = %{$args{conf}{areas}}; 45 foreach my $area (keys %area_conf) { 46 print $fh "area $area {\n"; 47 foreach my $if (keys %{${area_conf}{$area}}) { 48 print $fh "\tinterface $if {\n"; 49 foreach my $if_opt (keys %{${area_conf}{$area}{$if}}) { 50 print $fh "\t\t$if_opt " 51 . "$area_conf{$area}{$if}{$if_opt}\n"; 52 } 53 print $fh "\t}\n"; 54 } 55 print $fh "}\n"; 56 } 57 close $fh; 58 chmod(0600, $self->{conffile}); 59 my @sudo = $ENV{SUDO} ? $ENV{SUDO} : (); 60 my @cmd = (@sudo, "/sbin/chown", "0:0", $self->{conffile}); 61 system(@cmd) 62 and die ref($self), " system '@cmd' failed: $?"; 63 64 return $self; 65} 66 67sub child { 68 my $self = shift; 69 my @sudo = $ENV{SUDO} ? $ENV{SUDO} : (); 70 my @ktrace = $ENV{KTRACE} || (); 71 push @ktrace, "-i", "-f", "ospfd.ktrace" if @ktrace; 72 my $ospfd = $ENV{OSPFD} ? $ENV{OSPFD} : "ospfd"; 73 my @cmd = (@sudo, @ktrace, $ospfd, "-dv", "-f", $self->{conffile}); 74 print STDERR "execute: @cmd\n"; 75 exec @cmd; 76 die ref($self), " exec '@cmd' failed: $!"; 77} 78 791; 80