You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

monojob.c 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <errno.h>
  27. #include <ipxe/process.h>
  28. #include <ipxe/console.h>
  29. #include <ipxe/keys.h>
  30. #include <ipxe/job.h>
  31. #include <ipxe/monojob.h>
  32. #include <ipxe/timer.h>
  33. /** @file
  34. *
  35. * Single foreground job
  36. *
  37. */
  38. static int monojob_rc;
  39. static void monojob_close ( struct interface *intf, int rc ) {
  40. monojob_rc = rc;
  41. intf_restart ( intf, rc );
  42. }
  43. static struct interface_operation monojob_intf_op[] = {
  44. INTF_OP ( intf_close, struct interface *, monojob_close ),
  45. };
  46. static struct interface_descriptor monojob_intf_desc =
  47. INTF_DESC_PURE ( monojob_intf_op );
  48. struct interface monojob = INTF_INIT ( monojob_intf_desc );
  49. /**
  50. * Clear previously displayed message
  51. *
  52. * @v len Length of previously displayed message
  53. */
  54. static void monojob_clear ( size_t len ) {
  55. unsigned int i;
  56. for ( i = 0 ; i < len ; i++ )
  57. putchar ( '\b' );
  58. for ( i = 0 ; i < len ; i++ )
  59. putchar ( ' ' );
  60. for ( i = 0 ; i < len ; i++ )
  61. putchar ( '\b' );
  62. }
  63. /**
  64. * Wait for single foreground job to complete
  65. *
  66. * @v string Job description to display, or NULL to be silent
  67. * @v timeout Timeout period, in ticks (0=indefinite)
  68. * @ret rc Job final status code
  69. */
  70. int monojob_wait ( const char *string, unsigned long timeout ) {
  71. struct job_progress progress;
  72. unsigned long last_check;
  73. unsigned long last_progress;
  74. unsigned long last_display;
  75. unsigned long now;
  76. unsigned long elapsed;
  77. unsigned long completed = 0;
  78. unsigned long scaled_completed;
  79. unsigned long scaled_total;
  80. unsigned int percentage;
  81. size_t clear_len = 0;
  82. int ongoing_rc;
  83. int key;
  84. int rc;
  85. if ( string )
  86. printf ( "%s...", string );
  87. monojob_rc = -EINPROGRESS;
  88. last_check = last_progress = last_display = currticks();
  89. while ( monojob_rc == -EINPROGRESS ) {
  90. /* Allow job to progress */
  91. step();
  92. now = currticks();
  93. /* Continue until a timer tick occurs (to minimise
  94. * time wasted checking for progress and keypresses).
  95. */
  96. elapsed = ( now - last_check );
  97. if ( ! elapsed )
  98. continue;
  99. last_check = now;
  100. /* Check for keypresses */
  101. if ( iskey() ) {
  102. key = getchar();
  103. if ( key == CTRL_C ) {
  104. monojob_rc = -ECANCELED;
  105. break;
  106. }
  107. }
  108. /* Monitor progress */
  109. ongoing_rc = job_progress ( &monojob, &progress );
  110. /* Reset timeout if progress has been made */
  111. if ( completed != progress.completed )
  112. last_progress = now;
  113. completed = progress.completed;
  114. /* Check for timeout, if applicable */
  115. elapsed = ( now - last_progress );
  116. if ( timeout && ( elapsed >= timeout ) ) {
  117. monojob_rc = ( ongoing_rc ? ongoing_rc : -ETIMEDOUT );
  118. break;
  119. }
  120. /* Display progress, if applicable */
  121. elapsed = ( now - last_display );
  122. if ( string && ( elapsed >= TICKS_PER_SEC ) ) {
  123. monojob_clear ( clear_len );
  124. /* Normalise progress figures to avoid overflow */
  125. scaled_completed = ( progress.completed / 128 );
  126. scaled_total = ( progress.total / 128 );
  127. if ( scaled_total ) {
  128. percentage = ( ( 100 * scaled_completed ) /
  129. scaled_total );
  130. clear_len = printf ( "%3d%%", percentage );
  131. } else {
  132. printf ( "." );
  133. clear_len = 0;
  134. }
  135. if ( progress.message[0] ) {
  136. clear_len += printf ( " [%s]",
  137. progress.message );
  138. }
  139. last_display = now;
  140. }
  141. }
  142. rc = monojob_rc;
  143. monojob_close ( &monojob, rc );
  144. monojob_clear ( clear_len );
  145. if ( string ) {
  146. if ( rc ) {
  147. printf ( " %s\n", strerror ( rc ) );
  148. } else {
  149. printf ( " ok\n" );
  150. }
  151. }
  152. return rc;
  153. }