選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

monojob.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. * Wait for single foreground job to complete
  51. *
  52. * @v string Job description to display, or NULL to be silent
  53. * @v timeout Timeout period, in ticks (0=indefinite)
  54. * @ret rc Job final status code
  55. */
  56. int monojob_wait ( const char *string, unsigned long timeout ) {
  57. struct job_progress progress;
  58. unsigned long last_check;
  59. unsigned long last_progress;
  60. unsigned long last_display;
  61. unsigned long now;
  62. unsigned long elapsed;
  63. unsigned long completed = 0;
  64. unsigned long scaled_completed;
  65. unsigned long scaled_total;
  66. unsigned int percentage;
  67. int shown_percentage = 0;
  68. int ongoing_rc;
  69. int key;
  70. int rc;
  71. if ( string )
  72. printf ( "%s...", string );
  73. monojob_rc = -EINPROGRESS;
  74. last_check = last_progress = last_display = currticks();
  75. while ( monojob_rc == -EINPROGRESS ) {
  76. /* Allow job to progress */
  77. step();
  78. now = currticks();
  79. /* Continue until a timer tick occurs (to minimise
  80. * time wasted checking for progress and keypresses).
  81. */
  82. elapsed = ( now - last_check );
  83. if ( ! elapsed )
  84. continue;
  85. last_check = now;
  86. /* Check for keypresses */
  87. if ( iskey() ) {
  88. key = getchar();
  89. if ( key == CTRL_C ) {
  90. monojob_rc = -ECANCELED;
  91. break;
  92. }
  93. }
  94. /* Monitor progress */
  95. ongoing_rc = job_progress ( &monojob, &progress );
  96. /* Reset timeout if progress has been made */
  97. if ( completed != progress.completed )
  98. last_progress = now;
  99. completed = progress.completed;
  100. /* Check for timeout, if applicable */
  101. elapsed = ( now - last_progress );
  102. if ( timeout && ( elapsed >= timeout ) ) {
  103. monojob_rc = ( ongoing_rc ? ongoing_rc : -ETIMEDOUT );
  104. break;
  105. }
  106. /* Display progress, if applicable */
  107. elapsed = ( now - last_display );
  108. if ( string && ( elapsed >= TICKS_PER_SEC ) ) {
  109. if ( shown_percentage )
  110. printf ( "\b\b\b\b \b\b\b\b" );
  111. /* Normalise progress figures to avoid overflow */
  112. scaled_completed = ( progress.completed / 128 );
  113. scaled_total = ( progress.total / 128 );
  114. if ( scaled_total ) {
  115. percentage = ( ( 100 * scaled_completed ) /
  116. scaled_total );
  117. printf ( "%3d%%", percentage );
  118. shown_percentage = 1;
  119. } else {
  120. printf ( "." );
  121. shown_percentage = 0;
  122. }
  123. last_display = now;
  124. }
  125. }
  126. rc = monojob_rc;
  127. monojob_close ( &monojob, rc );
  128. if ( shown_percentage )
  129. printf ( "\b\b\b\b \b\b\b\b" );
  130. if ( string ) {
  131. if ( rc ) {
  132. printf ( " %s\n", strerror ( rc ) );
  133. } else {
  134. printf ( " ok\n" );
  135. }
  136. }
  137. return rc;
  138. }