6262
6363#include " config.h"
6464
65- #include " lib.error.h "
65+ #include " lib.error.hpp "
6666#include " lib.misc.h"
6767
6868#include < algorithm>
@@ -88,13 +88,13 @@ using namespace std;
8888
8989using fd_t = int ;
9090
91- const char * progname;
91+ std::string_view progname;
9292
9393// Set the NONBLOCK flag for a file descriptor.
9494void set_non_blocking (fd_t fd) {
9595 int flags = fcntl (fd, F_GETFL, 0 );
9696 if (fcntl (fd, F_SETFL, flags | O_NONBLOCK)) {
97- error (errno, " failed to set fd %d to non blocking" , fd);
97+ error (errno, " failed to set fd {} to non blocking" , fd);
9898 }
9999}
100100
@@ -117,28 +117,28 @@ void resize_pipe(int fd) {
117117 FILE *f = nullptr ;
118118 if ((f = fopen (PROC_MAX_PIPE_SIZE, " r" )) == nullptr ) {
119119 max_pipe_size = FAILED;
120- warning (errno, " could not open '%s '" , PROC_MAX_PIPE_SIZE);
120+ warning (errno, " could not open '{} '" , PROC_MAX_PIPE_SIZE);
121121 return ;
122122 }
123123 if (fscanf (f, " %d" , &max_pipe_size) != 1 ) {
124124 max_pipe_size = FAILED;
125- warning (errno, " could not read from '%s '" , PROC_MAX_PIPE_SIZE);
125+ warning (errno, " could not read from '{} '" , PROC_MAX_PIPE_SIZE);
126126 if (fclose (f) != 0 ) {
127- warning (errno, " could not close '%s '" , PROC_MAX_PIPE_SIZE);
127+ warning (errno, " could not close '{} '" , PROC_MAX_PIPE_SIZE);
128128 }
129129 return ;
130130 }
131131 if (fclose (f) != 0 ) {
132- warning (errno, " could not close '%s '" , PROC_MAX_PIPE_SIZE);
132+ warning (errno, " could not close '{} '" , PROC_MAX_PIPE_SIZE);
133133 }
134134 }
135135
136136 int new_size = fcntl (fd, F_SETPIPE_SZ, max_pipe_size);
137137 if (new_size == -1 ) {
138- warning (errno, " could not change pipe size of %d " , fd);
138+ warning (errno, " could not change pipe size of {} " , fd);
139139 }
140140
141- logmsg (LOG_DEBUG, " set pipe fd %d to size %d " , fd, new_size);
141+ logmsg (LOG_DEBUG, " set pipe fd {} to size {} " , fd, new_size);
142142}
143143
144144// Write all the data into the file descriptor. It is assumed that the file
@@ -245,9 +245,9 @@ struct process_t {
245245
246246 pid = execute (cmd, exec_args, stdio, false );
247247 if (pid < 0 ) {
248- error (errno, " failed to execute command #%ld " , index);
248+ error (errno, " failed to execute command #{} " , index);
249249 }
250- logmsg (LOG_DEBUG, " started #%ld , pid %d " , index, pid);
250+ logmsg (LOG_DEBUG, " started #{} , pid {} " , index, pid);
251251 // Do not leak these file descriptors, otherwise we cannot detect if the
252252 // process has closed stdout.
253253 close (stdin_fd);
@@ -264,7 +264,7 @@ struct process_t {
264264 // (i.e. proxy -> process).
265265 void close_input_fd () {
266266 if (proxy_to_process != -1 ) {
267- logmsg (LOG_DEBUG, " closing fd: %d (proxy -> process) of %d " ,
267+ logmsg (LOG_DEBUG, " closing fd: {} (proxy -> process) of {} " ,
268268 proxy_to_process, pid);
269269 close (proxy_to_process);
270270 }
@@ -274,7 +274,7 @@ struct process_t {
274274 // (i.e. process -> proxy).
275275 void close_output_fd () {
276276 if (process_to_proxy != -1 ) {
277- logmsg (LOG_DEBUG, " closing fd: %d (process -> proxy) of %d " ,
277+ logmsg (LOG_DEBUG, " closing fd: {} (process -> proxy) of {} " ,
278278 process_to_proxy, pid);
279279 close (process_to_proxy);
280280 }
@@ -318,7 +318,7 @@ struct output_file_t {
318318 output_file = open (path.c_str (), O_CREAT | O_CLOEXEC | O_WRONLY | O_TRUNC,
319319 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
320320 if (output_file == -1 ) {
321- error (errno, " failed to create proxy output file at %s " , path. c_str () );
321+ error (errno, " failed to create proxy output file at {} " , path);
322322 }
323323 }
324324
@@ -362,7 +362,7 @@ struct output_file_t {
362362 time_millis, size, direction);
363363 // Check that snprintf didn't truncate the header.
364364 if (header_len >= static_cast <int >(HEADER_SIZE)) {
365- error (0 , " header size too small: %d > %ld " , header_len, HEADER_SIZE);
365+ error (0 , " header size too small: {} > {} " , header_len, HEADER_SIZE);
366366 }
367367
368368 write_all (output_file, header, header_len);
@@ -376,7 +376,7 @@ void usage() {
376376Usage: %s [OPTION]... COMMAND1 [ARGS...] = COMMAND2 [ARGS...]\n \
377377Run two commands with stdin/stdout bi-directionally connected.\n \
378378\n " ,
379- progname);
379+ progname. data () );
380380 printf (" \
381381 -o, --outprog=FILE write stdout from second program to FILE\n \
382382 -M, --outmeta=FILE write metadata (runtime, exit_code, etc.) of first program to FILE\n \
@@ -456,22 +456,21 @@ struct state_t {
456456 break ;
457457 case ' o' : /* outprog option */
458458 args.output_file = optarg;
459- logmsg (LOG_DEBUG, " writing interactions to '%s'" ,
460- args.output_file .c_str ());
459+ logmsg (LOG_DEBUG, " writing interactions to '{}'" , args.output_file );
461460 break ;
462461 case ' M' : /* outmeta option */
463462 args.meta_file = optarg;
464- logmsg (LOG_DEBUG, " writing metadata to '%s '" , args.meta_file . c_str () );
463+ logmsg (LOG_DEBUG, " writing metadata to '{} '" , args.meta_file );
465464 break ;
466465 case ' h' :
467466 args.show_help = 1 ;
468467 break ;
469468 case ' :' : /* getopt error */
470469 case ' ?' :
471- error (0 , " unknown option or missing argument `%c '" , optopt);
470+ error (0 , " unknown option or missing argument `{:c} '" , optopt);
472471 break ;
473472 default :
474- error (0 , " getopt returned character code `%c ' ??" , (char )opt);
473+ error (0 , " getopt returned character code `{:c} ' ??" , (char )opt);
475474 }
476475 }
477476
@@ -534,7 +533,7 @@ struct state_t {
534533 if (args.verbose ) {
535534 logmsg (LOG_DEBUG, " Processes:" );
536535 for (size_t i = 0 ; i < processes.size (); i++) {
537- logmsg (LOG_DEBUG, " #%ld: %s " , i, processes[i].debug (). c_str ());
536+ logmsg (LOG_DEBUG, " #{}: {} " , i, processes[i].debug ());
538537 }
539538 }
540539 }
@@ -602,18 +601,18 @@ struct state_t {
602601 fd_t read_end = fds[0 ];
603602 static fd_t write_end = -1 ;
604603 if (write_end != -1 ) {
605- error (0 , " attempted to install signal handler for %d twice" , signum);
604+ error (0 , " attempted to install signal handler for {} twice" , signum);
606605 }
607606 write_end = fds[1 ];
608607
609- logmsg (LOG_DEBUG, " exit handler will send event using %d -> %d " , write_end,
608+ logmsg (LOG_DEBUG, " exit handler will send event using {} -> {} " , write_end,
610609 read_end);
611610
612611 signal (signum, [](int ) {
613612 // TODO: Decide whether to keep some logging as the line below. We can't
614613 // use logmsg here since that will in turn call syslog which is not safe
615614 // to do in a signal handler (see also `man signal-safety`).
616- // logmsg(LOG_DEBUG, "caught signal %d ", signum);
615+ // logmsg(LOG_DEBUG, "caught signal {} ", signum);
617616
618617 // Notify the main loop that a child exited by sending a message via
619618 // the pipe.
@@ -668,21 +667,21 @@ struct state_t {
668667 // Use two pipes for the given direction with the
669668 // proxy in between.
670669 tie (read_end, write_end) = make_pipe ();
671- logmsg (LOG_DEBUG, " setting up pipe #%ld (fd %d ) -> proxy (fd %d )" , i,
670+ logmsg (LOG_DEBUG, " setting up pipe #{} (fd {} ) -> proxy (fd {} )" , i,
672671 write_end, read_end);
673672 process.stdout_fd = write_end;
674673 process.process_to_proxy = read_end;
675674 set_non_blocking (process.process_to_proxy );
676675
677676 tie (read_end, write_end) = make_pipe ();
678- logmsg (LOG_DEBUG, " setting up pipe proxy (fd %d ) -> #%ld (fd %d )" ,
677+ logmsg (LOG_DEBUG, " setting up pipe proxy (fd {} ) -> #{} (fd {} )" ,
679678 write_end, j, read_end);
680679 other.proxy_to_process = write_end;
681680 other.stdin_fd = read_end;
682681 } else {
683682 // No proxy: direct communication.
684683 tie (read_end, write_end) = make_pipe ();
685- logmsg (LOG_DEBUG, " setting up pipe #%ld (fd %d ) -> #%ld (fd %d )" , i,
684+ logmsg (LOG_DEBUG, " setting up pipe #{} (fd {} ) -> #{} (fd {} )" , i,
686685 write_end, j, read_end);
687686 process.stdout_fd = write_end;
688687 other.stdin_fd = read_end;
@@ -698,12 +697,12 @@ struct state_t {
698697 }
699698
700699 auto add_fd = [&](fd_t fd) {
701- logmsg (LOG_DEBUG, " epoll will listen for fd %d " , fd);
700+ logmsg (LOG_DEBUG, " epoll will listen for fd {} " , fd);
702701 epoll_event ev{};
703702 ev.data .fd = fd;
704703 ev.events = EPOLLIN;
705704 if (epoll_ctl (epoll_fd, EPOLL_CTL_ADD, fd, &ev)) {
706- error (errno, " failed to add fd %d to epoll" , fd);
705+ error (errno, " failed to add fd {} to epoll" , fd);
707706 }
708707 };
709708
@@ -751,7 +750,7 @@ struct state_t {
751750 return false ;
752751 }
753752
754- logmsg (LOG_DEBUG, " child with pid %d exited" , pid);
753+ logmsg (LOG_DEBUG, " child with pid {} exited" , pid);
755754
756755 // Only set the first process if runguard didn't tell us about a TLE.
757756 if (first_process_exit_id == -1 && !child_indicated_timelimit) {
@@ -777,7 +776,7 @@ struct state_t {
777776 }
778777
779778 if (!found) {
780- error (0 , " unknown child with pid %d exited" , pid);
779+ error (0 , " unknown child with pid {} exited" , pid);
781780 }
782781
783782 return true ;
@@ -811,7 +810,7 @@ struct state_t {
811810 sprintf (eofbuf, " [%3d.%03ds/%ld]%c" , time_sec, time_millis, 0L , direction);
812811 write_all (output_file.output_file , eofbuf, strlen (eofbuf));
813812
814- warning (0 , " EOF from process #%ld " , from.index );
813+ warning (0 , " EOF from process #{} " , from.index );
815814 // The process closed stdout, we need to close the pipe's file
816815 // descriptors as well.
817816 to.close_input_fd ();
@@ -823,7 +822,7 @@ struct state_t {
823822 if (errno == EAGAIN || errno == EWOULDBLOCK) {
824823 return ;
825824 }
826- error (errno, " failed to read from pipe of #%ld " , from.index );
825+ error (errno, " failed to read from pipe of #{} " , from.index );
827826 }
828827 // We've read nread bytes, write them to the other process' pipe.
829828 write_all (to.proxy_to_process , buffer, nread);
@@ -873,8 +872,8 @@ struct state_t {
873872 // happening and the communication with the other process may be very
874873 // broken.
875874 if (main_process ().has_exited_with_signal ()) {
876- logmsg (LOG_WARNING, " the first process crashed! %s " ,
877- processes[0 ].exit_info_to_string (). c_str () );
875+ logmsg (LOG_WARNING, " the first process crashed! {} " ,
876+ processes[0 ].exit_info_to_string ());
878877 }
879878 continue ;
880879 }
@@ -910,7 +909,7 @@ struct state_t {
910909 finish:
911910 logmsg (LOG_DEBUG, " all processes exited" );
912911 if (!args.output_file .empty ()) {
913- logmsg (LOG_INFO, " total communication amount: %ld KiB" ,
912+ logmsg (LOG_INFO, " total communication amount: {} KiB" ,
914913 total_bytes_transferred / 1024 );
915914 }
916915 }
@@ -925,7 +924,7 @@ struct state_t {
925924
926925 ofstream meta (args.meta_file );
927926 if (meta.fail ()) {
928- error (errno, " failed to open meta file at %s " , args.meta_file . c_str () );
927+ error (errno, " failed to open meta file at {} " , args.meta_file );
929928 }
930929 meta << " exitcode: " << main_process ().exit_code () << endl;
931930 meta << " bytes-transferred: " << total_bytes_transferred << endl;
@@ -964,8 +963,8 @@ int main(int argc, char **argv) {
964963 if (state.args .verbose ) {
965964 logmsg (LOG_DEBUG, " Exit statuses:" );
966965 for (const auto &proc : state.processes ) {
967- logmsg (LOG_DEBUG, " #%ld: %s " , proc.index ,
968- proc.exit_info_to_string (). c_str () );
966+ logmsg (LOG_DEBUG, " #{}: {} " , proc.index ,
967+ proc.exit_info_to_string ());
969968 }
970969 }
971970
@@ -977,6 +976,6 @@ int main(int argc, char **argv) {
977976 }
978977
979978 // The first command exited with a signal.
980- error (0 , " the first process crashed! %s " ,
981- main_process.exit_info_to_string (). c_str () );
979+ error (0 , " the first process crashed! {} " ,
980+ main_process.exit_info_to_string ());
982981}
0 commit comments