From: rettenbe Date: Wed, 16 Jan 2008 13:30:13 +0000 (+0000) Subject: bugfix: get_column_names X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=1e5434986a2af80210c8d09efc130a044878f6da;p=gosa.git bugfix: get_column_names git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@8390 594d385d-05f5-0310-b6e9-bd551577e9d8 --- diff --git a/gosa-si/gosa-si-client b/gosa-si/gosa-si-client index 544030cdb..bd23dcd54 100755 --- a/gosa-si/gosa-si-client +++ b/gosa-si/gosa-si-client @@ -8,7 +8,7 @@ # DESCRIPTION: # # OPTIONS: --- -# REQUIREMENTS: --- +# REQUIREMENTS: libnetaddr-ip-perl # BUGS: --- # NOTES: # AUTHOR: (Andreas Rettenberger), diff --git a/gosa-si/gosa-si-server b/gosa-si/gosa-si-server index f0b59ce9c..c103cda34 100755 --- a/gosa-si/gosa-si-server +++ b/gosa-si/gosa-si-server @@ -485,10 +485,6 @@ sub get_processing_child { $answer = $tmp; } } - - #&print_known_daemons(); - #&print_known_clients(); - daemon_log("processing of msg finished", 5); if (defined $answer) { @@ -499,7 +495,7 @@ sub get_processing_child { daemon_log("\n$answer", 7); } else { print $PARENT_wr "done"."\n"; - daemon_log(" ", 7); + print $PARENT_wr "ENDMESSAGE\n"; } redo; } @@ -757,41 +753,41 @@ sub create_known_client { return; } -sub sysreadline(*;$) { - my ($hd, $timeout) = @_; - - $hd = qualify_to_ref($hd, caller()); - my $infinitely_patient = (@_ == 1 || $timeout < 0); - my $start_time = time(); - my $selector = IO::Select->new(); - $selector->add($hd); - my $line = ""; - -SLEEP: - until( at_eol($line)) { - if (not $infinitely_patient) { - return $line if time() > ($start_time + $timeout); - } - next SLEEP unless $selector->can_read(1.0); -INPUT_READY: - while( $selector->can_read(0.0)) { - my $was_blocking = $hd->blocking(0); -CHAR: while (sysread($hd, my $nextbyte, 1)) { - $line .= $nextbyte; - last CHAR if $nextbyte eq "\n"; - - } - $hd->blocking($was_blocking); - next SLEEP unless at_eol($line); - last INPUT_READY; - } - } - return $line; -} - -sub at_eol($) { - $_[0] =~ /\n\z/ ; -} +#sub sysreadline(*;$) { +# my ($hd, $timeout) = @_; +# +# $hd = qualify_to_ref($hd, caller()); +# my $infinitely_patient = (@_ == 1 || $timeout < 0); +# my $start_time = time(); +# my $selector = IO::Select->new(); +# $selector->add($hd); +# my $line = ""; +# +#SLEEP: +# until( at_eol($line)) { +# if (not $infinitely_patient) { +# return $line if time() > ($start_time + $timeout); +# } +# next SLEEP unless $selector->can_read(1.0); +#INPUT_READY: +# while( $selector->can_read(0.0)) { +# my $was_blocking = $hd->blocking(0); +#CHAR: while (sysread($hd, my $nextbyte, 1)) { +# $line .= $nextbyte; +# last CHAR if $nextbyte eq "\n"; +# +# } +# $hd->blocking($was_blocking); +# next SLEEP unless at_eol($line); +# last INPUT_READY; +# } +# } +# return $line; +#} +# +#sub at_eol($) { +# $_[0] =~ /\n\z/ ; +#} #==== MAIN = main ============================================================== @@ -841,6 +837,8 @@ if( 0 != $pid ) { daemon_log(" ", 1); daemon_log("$0 started!", 1); +# delete old DBsqlite lock files +system('rm -f /tmp/gosa_si_lock*'); # connect to gosa-si job queue my @job_col_names = ("id", "timestamp", "status", "result", "headertag", "targettag", "xmlmessage", "macaddress"); @@ -982,27 +980,22 @@ while(1) { } chomp($in_msg); - daemon_log("process child read:", 7); - daemon_log("\n$in_msg", 7); if (not defined $in_msg) { next; } elsif ($in_msg =~ "done") { + daemon_log("process child read: $in_msg", 7); delete $busy_child{$pid}; $free_child{$pid} = $child_hash; } else { + daemon_log("process child read:", 7); + daemon_log("\n$in_msg", 8); # send computed answer back to connected client my $act_client = $busy_child{$pid}{client_ref}; print $act_client $in_msg."\n"; - - #my $act_pipe = $busy_child{$pid}{pipe_rd}; delete $busy_child{$pid}; $free_child{$pid} = $child_hash; - # give the client a chance to read - #sleep(1); - #close ($act_client); - } } } diff --git a/gosa-si/modules/DBsqlite.pm b/gosa-si/modules/DBsqlite.pm index 618dd00c7..05946095c 100644 --- a/gosa-si/modules/DBsqlite.pm +++ b/gosa-si/modules/DBsqlite.pm @@ -34,7 +34,7 @@ sub lock_exists : locked { my $lock = $self->{db_lock}; my $result=(-f $lock); if($result) { - print STDERR "(".((defined $funcname)?$funcname:"").") Lock (PID ".$$.") $lock gefunden\n"; + #print STDERR "(".((defined $funcname)?$funcname:"").") Lock (PID ".$$.") $lock gefunden\n"; usleep 100; } return $result; @@ -43,11 +43,11 @@ sub lock_exists : locked { sub create_lock : locked { my $self=shift; my $funcname=shift; - print STDERR "(".((defined $funcname)?$funcname:"").") Erzeuge Lock (PID ".$$.") ".($self->{db_lock})."\n"; + #print STDERR "(".((defined $funcname)?$funcname:"").") Erzeuge Lock (PID ".$$.") ".($self->{db_lock})."\n"; my $lock = $self->{db_lock}; while( -f $lock ) { - print STDERR "(".((defined $funcname)?$funcname:"").") Lock (PID ".$$.") $lock gefunden\n"; + #print STDERR "(".((defined $funcname)?$funcname:"").") Lock (PID ".$$.") $lock gefunden\n"; sleep 1; } @@ -57,7 +57,7 @@ sub create_lock : locked { sub remove_lock : locked { my $self=shift; my $funcname=shift; - print STDERR "(".((defined $funcname)?$funcname:"").") Entferne Lock (PID ".$$.") ".$self->{db_lock}."\n"; + #print STDERR "(".((defined $funcname)?$funcname:"").") Entferne Lock (PID ".$$.") ".$self->{db_lock}."\n"; close($self->{db_lock_handle}); unlink($self->{db_lock}); } @@ -68,11 +68,8 @@ sub create_table { my $col_names_ref = shift; $col_names->{ $table_name } = $col_names_ref; my $col_names_string = join(', ', @{$col_names_ref}); - while(&lock_exists($self,'create_table')) { - print STDERR "Lock in create_table\n"; - } - &create_lock($self,'create_table'); my $sql_statement = "CREATE TABLE IF NOT EXISTS $table_name ( $col_names_string )"; + &create_lock($self,'create_table'); $self->{dbh}->do($sql_statement); &remove_lock($self,'create_table'); return 0; @@ -81,24 +78,17 @@ sub create_table { sub add_dbentry { - my $self = shift; my $arg = shift; - while(&lock_exists($self,'add_dbentry')) { - print STDERR "Lock in add_dbentry\n"; - } - &create_lock($self,'add_dbentry'); # if dbh not specified, return errorflag 1 my $table = $arg->{table}; - if (not defined $table) { - &remove_lock($self,'add_dbentry'); - return 1; + if( not defined $table ) { + return 1 ; } # specify primary key in table if (not exists $arg->{primkey}) { - &remove_lock($self,'add_dbentry'); return 2; } my $primkey = $arg->{primkey}; @@ -107,7 +97,9 @@ sub add_dbentry { if ($primkey eq 'id') { my $id; my $sql_statement = "SELECT MAX(id) FROM $table"; + &create_lock($self,'add_dbentry'); my $max_id = @{ @{ $self->{dbh}->selectall_arrayref($sql_statement) }[0] }[0]; + &remove_lock($self,'add_dbentry'); if( defined $max_id) { $id = $max_id + 1; } else { @@ -118,7 +110,6 @@ sub add_dbentry { # check wether value to primary key is specified if ( not exists $arg->{ $primkey } ) { - &remove_lock($self,'add_dbentry'); return 3; } @@ -127,9 +118,11 @@ sub add_dbentry { $arg->{timestamp} = &get_time; } - # check wether primkey is unique in table, otherwise return errorflag 3 + # check wether primkey is unique in table, otherwise return errorflag my $sql_statement = "SELECT * FROM $table WHERE $primkey='$arg->{$primkey}'"; + &create_lock($self,'add_dbentry'); my $res = @{ $self->{dbh}->selectall_arrayref($sql_statement) }; + &remove_lock($self,'add_dbentry'); if ($res == 0) { # fetch column names of table my $col_names = &get_table_columns("",$table); @@ -143,13 +136,14 @@ sub add_dbentry { } } - my $sql_statement = "BEGIN TRANSACTION; INSERT INTO $table VALUES ('".join("', '", @add_list)."'); COMMIT;"; + my $sql_statement = "INSERT INTO $table VALUES ('".join("', '", @add_list)."')"; + print STDERR $sql_statement; + &create_lock($self,'add_dbentry'); my $db_res = $self->{dbh}->do($sql_statement); + &remove_lock($self,'add_dbentry'); if( $db_res != 1 ) { - &remove_lock($self,'add_dbentry'); - return 1; + return 4; } else { - &remove_lock($self,'add_dbentry'); return 0; } @@ -164,15 +158,12 @@ sub add_dbentry { } my $db_res = &update_dbentry( $self, $update_hash ); if( $db_res != 1 ) { - &remove_lock($self,'add_dbentry'); - return 1; + return 5; } else { - &remove_lock($self,'add_dbentry'); return 0; } } - &remove_lock($self,'add_dbentry'); } @@ -186,15 +177,11 @@ sub update_dbentry { my $self = shift; my $arg = shift; - while(&lock_exists($self,'update_dbentry')) { - print STDERR "Lock in update_dbentry\n"; - } - &create_lock($self,'update_dbentry'); + # check completeness of function parameter # extract table statement from arg hash my $table = $arg->{table}; if (not defined $table) { - &remove_lock($self,'update_dbentry'); return 1; } else { delete $arg->{table}; @@ -231,7 +218,8 @@ sub update_dbentry { $update_statement .= join(', ', @update_list); } - my $sql_statement = "BEGIN TRANSACTION; UPDATE $table SET $update_statement $where_statement; COMMIT;"; + my $sql_statement = "UPDATE $table SET $update_statement $where_statement"; + &create_lock($self,'update_dbentry'); my $db_answer = $self->{dbh}->do($sql_statement); &remove_lock($self,'update_dbentry'); return $db_answer; @@ -242,15 +230,11 @@ sub del_dbentry { my $self = shift; my $arg = shift; - while(&lock_exists($self,'del_dbentry')) { - print STDERR "Lock in del_dbentry\n"; - } - &create_lock($self,'del_dbentry'); + # check completeness of function parameter # extract table statement from arg hash my $table = $arg->{table}; if (not defined $table) { - &remove_lock($self,'del_dbentry'); return 1; } else { delete $arg->{table}; @@ -273,9 +257,9 @@ sub del_dbentry { $where_statement = "WHERE ".join(' AND ', @del_list); } - my $sql_statement = "BEGIN TRANSACTION; DELETE FROM $table $where_statement; COMMIT;"; + my $sql_statement = "DELETE FROM $table $where_statement"; + &create_lock($self,'del_dbentry'); my $db_res = $self->{dbh}->do($sql_statement); - &remove_lock($self,'del_dbentry'); return $db_res; } @@ -284,8 +268,18 @@ sub del_dbentry { sub get_table_columns { my $self = shift; my $table = shift; - - my @column_names = @{$col_names->{$table}}; + my @column_names; + + if(exists $col_names->{$table}) { + @column_names = @{$col_names->{$table}}; + } else { + &create_lock($self,'get_table_columns'); + my @res = @{$self->{dbh}->selectall_arrayref("pragma table_info('$table')")}; + &remove_lock($self,'get_table_columns'); + foreach my $column (@res) { + push(@column_names, @$column[1]); + } + } return \@column_names; } @@ -294,16 +288,11 @@ sub select_dbentry { my $self = shift; my $arg = shift; - while(&lock_exists($self,'select_dbentry')) { - print STDERR "Lock in select_dbentry\n"; - } - &create_lock($self,'select_dbentry'); # check completeness of function parameter # extract table statement from arg hash my $table = $arg->{table}; if (not defined $table) { - &remove_lock($self,'select_dbentry'); return 1; } else { delete $arg->{table}; @@ -327,7 +316,9 @@ sub select_dbentry { } # query db + &create_lock($self,'select_dbentry'); my $query_answer = $self->{dbh}->selectall_arrayref($sql_statement); + &remove_lock($self,'select_dbentry'); # fetch column list of db and create a hash with column_name->column_value of the select query my $column_list = &get_table_columns($self, $table); @@ -344,7 +335,6 @@ sub select_dbentry { } } - &remove_lock($self,'select_dbentry'); return $answer; } @@ -352,16 +342,13 @@ sub select_dbentry { sub show_table { my $self = shift; my $table_name = shift; - while(&lock_exists($self,'show_table')) { - print STDERR "Lock in show_table\n"; - } &create_lock($self,'show_table'); my @res = @{$self->{dbh}->selectall_arrayref( "SELECT * FROM $table_name")}; + &remove_lock($self,'show_table'); my @answer; foreach my $hit (@res) { push(@answer, "hit: ".join(', ', @{$hit})); } - &remove_lock($self,'show_table'); return join("\n", @answer); } @@ -369,12 +356,9 @@ sub show_table { sub exec_statement { my $self = shift; my $sql_statement = shift; - while(&lock_exists($self,'exec_statement')) { - print STDERR "Lock in exec_statement\n"; - } &create_lock($self,'exec_statement'); my @res = @{$self->{dbh}->selectall_arrayref($sql_statement)}; - &remove_locK; + &remove_lock($self, 'exec_statement'); return \@res; } diff --git a/gosa-si/modules/GosaPackages.pm b/gosa-si/modules/GosaPackages.pm index 7f211a59d..73dd6b26c 100644 --- a/gosa-si/modules/GosaPackages.pm +++ b/gosa-si/modules/GosaPackages.pm @@ -333,7 +333,9 @@ sub process_incoming_msg { if ($out_msg =~ /(\d*?)<\/jobdb_id>/) { my $job_id = $1; - my $sql = "BEGIN TRANSATION; UPDATE '$main::job_queue_table_name' SET status='done', result='$out_msg' WHERE id='$job_id'; COMMIT;"; + my $sql = "BEGIN TRANSATION; UPDATE '".$main::job_queue_table_name. + "' SET status='done', result='".$out_msg. + "' WHERE id='$job_id'; COMMIT;"; my $res = $main::job_db->exec_statement($sql); return; @@ -350,7 +352,6 @@ sub process_gosa_msg { my ($msg, $header) = @_ ; my $out_msg; $header =~ s/gosa_//; - &main::daemon_log("GosaPackages: got a gosa msg $header", 5); # decide wether msg is a core function or a event handler if ( $header eq 'query_jobdb') { $out_msg = &query_jobdb } @@ -398,7 +399,6 @@ sub process_job_msg { my $header = @{$msg_hash->{header}}[0]; $header =~ s/job_//; - &main::daemon_log("GosaPackages: got a job msg for queue: $header", 5); # check wether mac address is already known in known_daemons or known_clients my $target = 'none'; diff --git a/gosa-si/modules/GosaSupportDaemon.pm b/gosa-si/modules/GosaSupportDaemon.pm index e3da5c037..002308e53 100644 --- a/gosa-si/modules/GosaSupportDaemon.pm +++ b/gosa-si/modules/GosaSupportDaemon.pm @@ -97,7 +97,7 @@ sub send_msg_hash2address ($$$){ close $socket; daemon_log("send '$header'-msg to $address", 1); - daemon_log("$msg_xml", 5); + daemon_log("message:\n$msg_xml", 8); return 0; } @@ -232,8 +232,7 @@ sub open_socket { if(not defined $socket) { return; } - &daemon_log("open_socket:", 7); - &daemon_log("\t$PeerAddr", 7); + &daemon_log("open_socket: $PeerAddr", 7); return $socket; } diff --git a/gosa-si/modules/ServerPackages.pm b/gosa-si/modules/ServerPackages.pm index 074a0ddf8..1aa8db2a8 100644 --- a/gosa-si/modules/ServerPackages.pm +++ b/gosa-si/modules/ServerPackages.pm @@ -84,9 +84,6 @@ if($server_activ eq "on"){ } } -# TODO -# füge den server selbst zu known_server hinzu, msgs können nämlich auch von sich selbst kommen (gosa!!!) - # register at bus if ($main::no_bus > 0) { @@ -99,18 +96,6 @@ if($bus_activ eq "on") { ### functions ################################################################# -#sub get_module_tags { -# -# # lese config file aus dort gibt es eine section Basic -# # dort stehen drei packettypen, für die sich das modul anmelden kann, gosa-admin-packages, -# # server-packages, client-packages -# my %tag_hash = (gosa_admin_packages => "yes", -# server_packages => "yes", -# client_packages => "yes", -# ); -# return \%tag_hash; -#} - sub get_module_info { my @info = ($server_address, @@ -302,23 +287,23 @@ sub get_ip { # RETURNS: socket IO::Socket::INET # DESCRIPTION: open a socket to PeerAddr #=============================================================================== -sub open_socket { - my ($PeerAddr, $PeerPort) = @_ ; - if(defined($PeerPort)){ - $PeerAddr = $PeerAddr.":".$PeerPort; - } - my $socket; - $socket = new IO::Socket::INET(PeerAddr => $PeerAddr , - Porto => "tcp" , - Type => SOCK_STREAM, - Timeout => 5, - ); - if(not defined $socket) { - return; - } - &main::daemon_log("open_socket to: $PeerAddr", 7); - return $socket; -} +#sub open_socket { +# my ($PeerAddr, $PeerPort) = @_ ; +# if(defined($PeerPort)){ +# $PeerAddr = $PeerAddr.":".$PeerPort; +# } +# my $socket; +# $socket = new IO::Socket::INET(PeerAddr => $PeerAddr , +# Porto => "tcp" , +# Type => SOCK_STREAM, +# Timeout => 5, +# ); +# if(not defined $socket) { +# return; +# } +# &main::daemon_log("open_socket to: $PeerAddr", 7); +# return $socket; +#} #=== FUNCTION ================================================================ # NAME: register_at_bus @@ -336,16 +321,6 @@ sub register_at_bus { hostkey=>$bus_passwd, timestamp=>&get_time, } ); -# if ($res == 3) { -# my $update_hash = { table=>'known_server' }; -# $update_hash->{where} = [ { hostname=>[$bus_address] } ]; -# $update_hash->{update} = [ { -# hostkey=>[$bus_passwd], -# timestamp=>[&get_time], -# } ]; -# $res = $main::known_server_db->update_dbentry( $update_hash ); -# } - my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address); my $answer = ""; $answer = &send_msg_hash2address($msg_hash, $bus_address, $bus_passwd); @@ -369,7 +344,7 @@ sub process_incoming_msg { &main::daemon_log("function 'process_incoming_msg': got no msg", 7); } - &main::daemon_log("ServerPackages: incoming msg: \n$crypted_msg", 7); + &main::daemon_log("ServerPackages: incoming msg: \n$crypted_msg", 8); $crypted_msg =~ /^([\s\S]*?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)$/; $crypted_msg = $1; @@ -468,7 +443,7 @@ sub process_incoming_msg { my $source = @{$msg_hash->{source}}[0]; &main::daemon_log("recieve '$header' at ServerPackages from $host", 1); - &main::daemon_log("ServerPackages: msg to process: \n$msg", 5); + &main::daemon_log("ServerPackages: msg to process: \n$msg", 7); my @targets = @{$msg_hash->{target}}; my $len_targets = @targets; @@ -480,52 +455,56 @@ sub process_incoming_msg { my $target = $targets[0]; &main::daemon_log("SeverPackages: msg is for: $target", 7); - if ($target eq $server_address) { - # msg is for server - if ($header eq 'new_passwd'){ &new_passwd($msg_hash)} - elsif ($header eq 'here_i_am') { &here_i_am($msg_hash)} - elsif ($header eq 'who_has') { &who_has($msg_hash) } - elsif ($header eq 'who_has_i_do') { &who_has_i_do($msg_hash)} - elsif ($header eq 'update_status') { &update_status($msg_hash) } - elsif ($header eq 'got_ping') { &got_ping($msg_hash)} - elsif ($header eq 'get_load') { &execute_actions($msg_hash)} - else { &main::daemon_log("ERROR: ServerPackages: no function assigned to this msg", 5) } - - } elsif ($target eq "*") { - # msg is for all clients - my $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients'} ); - while( my ($hit_num, $hit) = each %{ $query_res } ) { - $host_name = $hit->{hostname}; - $host_key = $hit->{hostkey}; - $msg_hash->{target} = [$host_name]; - &send_msg_hash2address($msg_hash, $host_name, $host_key); - } - return; - - } else { - # msg is for one host - my $query_res; - $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients', hostname=>$target} ); - if( 1 == keys %{$query_res} ) { - $host_key = $query_res->{1}->{host_key}; - &send_msg_hash2address($msg_hash, $target, $host_key); - return; - } + # msg is for server + if ($header eq 'new_passwd'){ &new_passwd($msg_hash)} + elsif ($header eq 'here_i_am') { &here_i_am($msg_hash)} + elsif ($header eq 'who_has') { &who_has($msg_hash) } + elsif ($header eq 'who_has_i_do') { &who_has_i_do($msg_hash)} + elsif ($header eq 'update_status') { &update_status($msg_hash) } + elsif ($header eq 'got_ping') { &got_ping($msg_hash)} + elsif ($header eq 'get_load') { &execute_actions($msg_hash)} + else { + if ($target eq "*") { + # msg is for all clients + my $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients'} ); + while( my ($hit_num, $hit) = each %{ $query_res } ) { + $host_name = $hit->{hostname}; + $host_key = $hit->{hostkey}; + $msg_hash->{target} = [$host_name]; + &send_msg_hash2address($msg_hash, $host_name, $host_key); + } - $query_res = $main::known_server_db->select_dbentry( {table=>'known_server', hostname=>$target} ); - if( 1 == keys %{$query_res} ) { - $host_key = $query_res->{1}->{host_key}; - &send_msg_hash2address($msg_hash, $target, $host_key); - return; + } else { + # msg is for one host + my $host_key; + + + if( not defined $host_key ) { + my $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients', hostname=>$target} ); + if( 1 == keys %{$query_res} ) { + $host_key = $query_res->{1}->{host_key}; + } + } + + if( not defined $host_key ) { + my $query_res = $main::known_server_db->select_dbentry( {table=>'known_server', hostname=>$target} ); + if( 1 == keys %{$query_res} ) { + $host_key = $query_res->{1}->{host_key}; + } + } + + if( not defined $host_key ) { + &main::daemon_log("ERROR: ServerPackages: target '".$target. + "' is not known neither in known_clients nor in known_server",1); + } else { + &send_msg_hash2address($msg_hash, $target, $host_key); + } } - - &main::daemon_log("ERROR: ServerPackages: target '$target' is not known neither in known_clients nor in known_server",1); - return; } } elsif ($len_targets > 1 ) { # we have more than one target - return; + # TODO to be implemented } return ; @@ -630,7 +609,7 @@ sub here_i_am { my $out_hash; # number of known clients - my $nu_clients = keys %{$main::known_clients_db->select_dbentry( {table=>'known_clients'} )}; + my $nu_clients = keys %{ $main::known_clients_db->select_dbentry( {table=>'known_clients'} ) }; # check wether client address or mac address is already known if (exists $main::known_clients->{$source}) {