iPXE
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)
 FILE_SECBOOT (PERMITTED)
static void monojob_close (struct interface *intf, int rc)
static void monojob_clear (size_t len)
 Clear previously displayed message.
int monojob_wait (const char *string, unsigned long timeout)
 Wait for single foreground job to complete.

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 )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ monojob_close()

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

Definition at line 45 of file monojob.c.

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

References intf_restart(), monojob_rc, and rc.

Referenced by monojob_wait().

◆ monojob_clear()

void monojob_clear ( size_t len)
static

Clear previously displayed message.

Parameters
lenLength of previously displayed message

Definition at line 64 of file monojob.c.

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

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 82 of file monojob.c.

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

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 43 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:250
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition interface.h:33
An object interface.
Definition interface.h:125

Definition at line 50 of file monojob.c.

50 {
52};

◆ 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:116
static struct interface_operation monojob_intf_op[]
Definition monojob.c:50

Definition at line 54 of file monojob.c.

◆ monojob