iPXE
Functions | Variables
monojob.c File Reference

Single foreground job. More...

#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <ipxe/process.h>
#include <ipxe/console.h>
#include <ipxe/keys.h>
#include <ipxe/job.h>
#include <ipxe/monojob.h>
#include <ipxe/timer.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static void monojob_close (struct interface *intf, int rc)
 
static void monojob_clear (size_t len)
 Clear previously displayed message. More...
 
int monojob_wait (const char *string, unsigned long timeout)
 Wait for single foreground job to complete. More...
 

Variables

static int monojob_rc
 
static struct interface_operation monojob_intf_op []
 
static struct interface_descriptor monojob_intf_desc
 
struct interface monojob = INTF_INIT ( monojob_intf_desc )
 

Detailed Description

Single foreground job.

Definition in file monojob.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ monojob_close()

static void monojob_close ( struct interface intf,
int  rc 
)
static

Definition at line 44 of file monojob.c.

44  {
45  monojob_rc = rc;
46  intf_restart ( intf, rc );
47 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void intf_restart(struct interface *intf, int rc)
Shut down and restart an object interface.
Definition: interface.c:343
static int monojob_rc
Definition: monojob.c:42

References intf_restart(), monojob_rc, and rc.

Referenced by monojob_wait().

◆ monojob_clear()

static void monojob_clear ( size_t  len)
static

Clear previously displayed message.

Parameters
lenLength of previously displayed message

Definition at line 63 of file monojob.c.

63  {
64  unsigned int i;
65 
66  for ( i = 0 ; i < len ; i++ )
67  putchar ( '\b' );
68  for ( i = 0 ; i < len ; i++ )
69  putchar ( ' ' );
70  for ( i = 0 ; i < len ; i++ )
71  putchar ( '\b' );
72 }
uint32_t len
Length.
Definition: ena.h:14
int putchar(int character)
Write a single character to each console device.
Definition: console.c:28

References len, and putchar().

Referenced by monojob_wait().

◆ monojob_wait()

int monojob_wait ( const char *  string,
unsigned long  timeout 
)

Wait for single foreground job to complete.

Parameters
stringJob description to display, or NULL to be silent
timeoutTimeout period, in ticks (0=indefinite)
Return values
rcJob final status code

Definition at line 81 of file monojob.c.

81  {
82  struct job_progress progress;
83  unsigned long last_check;
84  unsigned long last_progress;
85  unsigned long last_display;
86  unsigned long now;
87  unsigned long elapsed;
88  unsigned long completed = 0;
89  unsigned long scaled_completed;
90  unsigned long scaled_total;
91  unsigned int percentage;
92  size_t clear_len = 0;
93  int ongoing_rc;
94  int key;
95  int rc;
96 
97  if ( string )
98  printf ( "%s...", string );
100  last_check = last_progress = last_display = currticks();
101  while ( monojob_rc == -EINPROGRESS ) {
102 
103  /* Allow job to progress */
104  step();
105  now = currticks();
106 
107  /* Continue until a timer tick occurs (to minimise
108  * time wasted checking for progress and keypresses).
109  */
110  elapsed = ( now - last_check );
111  if ( ! elapsed )
112  continue;
113  last_check = now;
114 
115  /* Check for keypresses */
116  if ( iskey() ) {
117  key = getchar();
118  if ( key == CTRL_C ) {
120  break;
121  }
122  }
123 
124  /* Monitor progress */
125  ongoing_rc = job_progress ( &monojob, &progress );
126 
127  /* Reset timeout if progress has been made */
128  if ( completed != progress.completed )
129  last_progress = now;
130  completed = progress.completed;
131 
132  /* Check for timeout, if applicable */
133  elapsed = ( now - last_progress );
134  if ( timeout && ( elapsed >= timeout ) ) {
135  monojob_rc = ( ongoing_rc ? ongoing_rc : -ETIMEDOUT );
136  break;
137  }
138 
139  /* Display progress, if applicable */
140  elapsed = ( now - last_display );
141  if ( string && ( elapsed >= TICKS_PER_SEC ) ) {
142  monojob_clear ( clear_len );
143  /* Normalise progress figures to avoid overflow */
144  scaled_completed = ( progress.completed / 128 );
145  scaled_total = ( progress.total / 128 );
146  if ( scaled_total ) {
147  percentage = ( ( 100 * scaled_completed ) /
148  scaled_total );
149  clear_len = printf ( "%3d%%", percentage );
150  } else {
151  printf ( "." );
152  clear_len = 0;
153  }
154  if ( progress.message[0] ) {
155  clear_len += printf ( " [%s]",
156  progress.message );
157  }
158  last_display = now;
159  }
160  }
161  rc = monojob_rc;
162  monojob_close ( &monojob, rc );
163 
164  monojob_clear ( clear_len );
165  if ( string ) {
166  if ( rc ) {
167  printf ( " %s\n", strerror ( rc ) );
168  } else {
169  printf ( " ok\n" );
170  }
171  }
172 
173  return rc;
174 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define TICKS_PER_SEC
Number of ticks per second.
Definition: timer.h:15
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
#define ECANCELED
Operation canceled.
Definition: errno.h:343
#define CTRL_C
Definition: keys.h:20
unsigned long completed
Amount of operation completed so far.
Definition: job.h:23
struct interface monojob
Definition: monojob.c:56
#define EINPROGRESS
Operation in progress.
Definition: errno.h:418
int getchar(void)
Read a single character from any console.
Definition: console.c:85
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
Job progress.
Definition: job.h:15
static void monojob_close(struct interface *intf, int rc)
Definition: monojob.c:44
static int monojob_rc
Definition: monojob.c:42
void step(void)
Single-step a single process.
Definition: process.c:98
int job_progress(struct interface *intf, struct job_progress *progress)
Get job progress.
Definition: job.c:43
static void monojob_clear(size_t len)
Clear previously displayed message.
Definition: monojob.c:63
void timeout(int)
unsigned long currticks(void)
Get current system time in ticks.
Definition: timer.c:42
#define ETIMEDOUT
Connection timed out.
Definition: errno.h:669
int iskey(void)
Check for available input on any console.
Definition: console.c:130
union @383 key
Sense key.
Definition: scsi.h:18

References job_progress::completed, CTRL_C, currticks(), ECANCELED, EINPROGRESS, ETIMEDOUT, getchar(), iskey(), job_progress(), key, job_progress::message, monojob, monojob_clear(), monojob_close(), monojob_rc, printf(), rc, step(), strerror(), TICKS_PER_SEC, timeout(), and job_progress::total.

Referenced by fcels(), ifpoller_wait(), imgdownload(), imgverify(), nslookup(), ntp(), ping(), pxebs(), and sync().

Variable Documentation

◆ monojob_rc

int monojob_rc
static

Definition at line 42 of file monojob.c.

Referenced by monojob_close(), and monojob_wait().

◆ monojob_intf_op

struct interface_operation monojob_intf_op[]
static
Initial value:
= {
}
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:249
An object interface.
Definition: interface.h:124
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32
static void monojob_close(struct interface *intf, int rc)
Definition: monojob.c:44

Definition at line 49 of file monojob.c.

◆ monojob_intf_desc

struct interface_descriptor monojob_intf_desc
static
Initial value:
=
#define INTF_DESC_PURE(operations)
Define an object interface descriptor for a pure-interface object.
Definition: interface.h:115
static struct interface_operation monojob_intf_op[]
Definition: monojob.c:49

Definition at line 53 of file monojob.c.

◆ monojob

struct interface monojob = INTF_INIT ( monojob_intf_desc )