From 1f2c12fc33368474d725876bd58a9da7d527e75c Mon Sep 17 00:00:00 2001 From: Mario Fetka Date: Fri, 22 May 2026 14:45:54 +0200 Subject: [PATCH] add print test page v2 --- apply.pl | 199 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 134 insertions(+), 65 deletions(-) diff --git a/apply.pl b/apply.pl index eefb450..a79aa71 100644 --- a/apply.pl +++ b/apply.pl @@ -636,7 +636,7 @@ sub bindery_error_page( $$$ ) $title = apply_html_escape( $title ); $cmd = apply_html_escape( $cmd ); - $output = apply_html_escape( $output ); + $output = queue_test_html_escape( $output ); print </>/g; + $s =~ s/"/"/g; + + return $s; +} + + sub queue_test_result_page( $$$$ ) { my( $queue, $ok, $message, $output ) = @_; - $queue = apply_html_escape( $queue ); - $message = apply_html_escape( $message ); - $output = apply_html_escape( $output ); + $queue = queue_test_html_escape( $queue ); + $message = queue_test_html_escape( $message ); + $output = queue_test_html_escape( $output ); my $title = $ok ? 'Print queue test sent' : 'Print queue test failed'; my $color = $ok ? '#2f5b24' : '#8a1f16'; @@ -1575,89 +1589,144 @@ A.button { display:inline-block; margin-top:14px; padding:9px 14px; border-radiu EOF } +our $queue_test_command_rc = 0; + +sub queue_test_shell_log_quote( $ ) +{ + my $s = $_[0]; + + $s = '' unless defined( $s ); + $s =~ s/'/'"'"'/g; + + return "'" . $s . "'"; +} + +sub run_queue_test_command( @ ) +{ + my @cmd = @_; + my $output = ''; + my $pid; + + $queue_test_command_rc = 0; + + $pid = open( my $fh, '-|' ); + + if( ! defined( $pid ) ) + { + $queue_test_command_rc = 255 << 8; + return 'could not fork for nprint: ' . $!; + } + + if( $pid == 0 ) + { + open( STDERR, '>&', STDOUT ); + exec( @cmd ); + print STDOUT 'could not exec ' . $cmd[0] . ': ' . $! . "\n"; + exit( 127 ); + } + + while( my $line = <$fh> ) + { + $output .= $line; + } + + close( $fh ); + $queue_test_command_rc = $?; + + $output = '' unless defined( $output ); + $output =~ s/\r//g; + + return $output; +} + sub test_print_queue( $$ ) { my( $server, $queue ) = @_; - $server = '' unless defined( $server ); - $queue = normalize_queue_name( $queue ); - - if( $queue eq '' ) + my $eval_error = ''; + my $ok = eval { - queue_test_result_page( $queue, 0, 'Queue name is empty.', '' ); - return 0; - } + $server = '' unless defined( $server ); + $queue = normalize_queue_name( $queue ); - my $nprint = defined( $smart_nprint_path ) && $smart_nprint_path ne '' ? $smart_nprint_path : '/usr/bin/nprint'; + if( $queue eq '' ) + { + queue_test_result_page( $queue, 0, 'Queue name is empty.', '' ); + return 1; + } - if( ! -x $nprint ) - { - my $msg = 'nprint helper not found or not executable: ' . $nprint; - apply_log_line( 'ERROR', 'print queue test failed queue=' . $queue . ' error=' . $msg ); - queue_test_result_page( $queue, 0, $msg, '' ); - return 0; - } + my $nprint = defined( $smart_nprint_path ) && $smart_nprint_path ne '' ? $smart_nprint_path : '/usr/bin/nprint'; - my $tmpdir = '/run/mars-nwe-webui'; - $tmpdir = '/tmp' if ! -d $tmpdir || ! -w $tmpdir; + if( ! -x $nprint ) + { + my $msg = 'nprint helper not found or not executable: ' . $nprint; + apply_log_line( 'ERROR', 'print queue test failed queue=' . $queue . ' error=' . $msg ); + queue_test_result_page( $queue, 0, $msg, '' ); + return 1; + } - my $tmp = $tmpdir . '/smart-test-print-' . $$ . '-' . int( rand( 1000000 ) ) . '.txt'; + my $tmpdir = '/run/mars-nwe-webui'; + $tmpdir = '/tmp' if ! -d $tmpdir || ! -w $tmpdir; - if( ! open( my $fh, '>', $tmp ) ) - { - my $msg = 'Could not create test print file ' . $tmp . ': ' . $!; - apply_log_line( 'ERROR', 'print queue test failed queue=' . $queue . ' error=' . $msg ); - queue_test_result_page( $queue, 0, $msg, '' ); - return 0; - } + my $tmp = $tmpdir . '/smart-test-print-' . $$ . '-' . int( rand( 1000000 ) ) . '.txt'; - my $ts = scalar( localtime( time() ) ); + if( ! open( my $fh, '>', $tmp ) ) + { + my $msg = 'Could not create test print file ' . $tmp . ': ' . $!; + apply_log_line( 'ERROR', 'print queue test failed queue=' . $queue . ' error=' . $msg ); + queue_test_result_page( $queue, 0, $msg, '' ); + return 1; + } - print( $fh "SMArT / MARS_NWE print queue test\n" ); - print( $fh "Server: $server\n" ); - print( $fh "Queue: $queue\n" ); - print( $fh "Time: $ts\n" ); - print( $fh "\nIf you can read this, the NetWare print queue accepted a test job.\n" ); + my $ts = scalar( localtime( time() ) ); + + print( $fh "SMArT / MARS_NWE print queue test\n" ); + print( $fh "Server: $server\n" ); + print( $fh "Queue: $queue\n" ); + print( $fh "Time: $ts\n" ); + print( $fh "\nIf you can read this, the NetWare print queue accepted a test job.\n" ); + + if( ! close( $fh ) ) + { + my $msg = 'Could not close test print file ' . $tmp . ': ' . $!; + unlink( $tmp ); + apply_log_line( 'ERROR', 'print queue test failed queue=' . $queue . ' error=' . $msg ); + queue_test_result_page( $queue, 0, $msg, '' ); + return 1; + } + + my @cmd = ( $nprint, '-S', $server, '-q', $queue, '-d', 'SMArT test print', $tmp ); + apply_log_line( 'INFO', 'print queue test command: ' . join( ' ', map { queue_test_shell_log_quote( $_ ) } @cmd ) ); + + my $output = run_queue_test_command( @cmd ); - if( ! close( $fh ) ) - { - my $msg = 'Could not close test print file ' . $tmp . ': ' . $!; unlink( $tmp ); - apply_log_line( 'ERROR', 'print queue test failed queue=' . $queue . ' error=' . $msg ); - queue_test_result_page( $queue, 0, $msg, '' ); - return 0; - } - my $cmd = join( ' ', - apply_shell_quote( $nprint ), - '-S', apply_shell_quote( $server ), - '-q', apply_shell_quote( $queue ), - '-d', apply_shell_quote( 'SMArT test print' ), - apply_shell_quote( $tmp ) - ); + if( $queue_test_command_rc != 0 ) + { + my $exit = $queue_test_command_rc >> 8; + apply_log_line( 'ERROR', 'print queue test failed rc=' . $exit . ' queue=' . $queue . ' output=' . $output ); + queue_test_result_page( $queue, 0, 'nprint returned an error.', $output ); + return 1; + } - apply_log_line( 'INFO', 'print queue test command: ' . $cmd ); + apply_log_line( 'INFO', 'print queue test ok queue=' . $queue . ( $output ne '' ? ' output=' . $output : '' ) ); + queue_test_result_page( $queue, 1, 'Test job was submitted with nprint.', $output ); - my $output = `$cmd 2>&1`; - my $rc = $?; + return 1; + }; - unlink( $tmp ); - - $output = '' unless defined( $output ); - $output =~ s/\r//g; - - if( $rc != 0 ) + if( ! $ok ) { - my $exit = $rc >> 8; - apply_log_line( 'ERROR', 'print queue test failed rc=' . $exit . ' queue=' . $queue . ' output=' . $output ); - queue_test_result_page( $queue, 0, 'nprint returned an error.', $output ); + $eval_error = $@ if defined( $@ ) && $@ ne ''; + $eval_error = 'unknown internal error' if $eval_error eq ''; + + apply_log_line( 'ERROR', 'print queue test died queue=' . ( defined( $queue ) ? $queue : '' ) . ' error=' . $eval_error ); + queue_test_result_page( defined( $queue ) ? $queue : '', 0, 'Internal error while running queue test.', $eval_error ); return 0; } - apply_log_line( 'INFO', 'print queue test ok queue=' . $queue . ( $output ne '' ? ' output=' . $output : '' ) ); - queue_test_result_page( $queue, 1, 'Test job was submitted with nprint.', $output ); - return 1; }