Changeset 87822ce in mainline for uspace/lib/c/generic/io/console.c


Ignore:
Timestamp:
2021-03-04T19:14:30Z (4 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d6c4d40
Parents:
760a392
Message:

Avoid infinite loop when console communication is broken

Need to make sure callers of console_get_event_timeout() can distinguish
between timeout and I/O error. Fix all callers of console_get_event()
and console_get_event_timeout() not to enter infinite loop when console
connection is broken. Also avoid setting of errno variable.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/io/console.c

    r760a392 r87822ce  
    180180}
    181181
    182 bool console_get_event(console_ctrl_t *ctrl, cons_event_t *event)
     182/** Get console event.
     183 *
     184 * @param ctrl Console
     185 * @param event Place to store event
     186 * @return EOK on success, EIO on failure
     187 */
     188errno_t console_get_event(console_ctrl_t *ctrl, cons_event_t *event)
    183189{
    184190        if (ctrl->input_aid == 0) {
     
    192198                async_wait_for(aid, &rc);
    193199
    194                 if (rc != EOK) {
    195                         errno = rc;
    196                         return false;
    197                 }
     200                if (rc != EOK)
     201                        return EIO;
    198202
    199203                rc = console_ev_decode(&result, event);
    200                 if (rc != EOK) {
    201                         errno = rc;
    202                         return false;
    203                 }
     204                if (rc != EOK)
     205                        return EIO;
    204206        } else {
    205207                errno_t retval;
     
    208210                ctrl->input_aid = 0;
    209211
    210                 if (retval != EOK) {
    211                         errno = retval;
    212                         return false;
    213                 }
     212                if (retval != EOK)
     213                        return EIO;
    214214
    215215                errno_t rc = console_ev_decode(&ctrl->input_call, event);
    216                 if (rc != EOK) {
    217                         errno = rc;
    218                         return false;
    219                 }
    220         }
    221 
    222         return true;
    223 }
    224 
    225 bool console_get_event_timeout(console_ctrl_t *ctrl, cons_event_t *event,
     216                if (rc != EOK)
     217                        return EIO;
     218        }
     219
     220        return EOK;
     221}
     222
     223/** Get console event with timeout.
     224 *
     225 * @param ctrl Console
     226 * @param event Place to store event
     227 * @param timeout Pointer to timeout. This will be updated to reflect
     228 *                the remaining time in case of timeout.
     229 * @return EOK on success (event received), ETIMEOUT on time out,
     230 *         EIO on I/O error (e.g. lost console connection), ENOMEM
     231 *         if out of memory
     232 */
     233errno_t console_get_event_timeout(console_ctrl_t *ctrl, cons_event_t *event,
    226234    usec_t *timeout)
    227235{
     
    239247        errno_t rc = async_wait_timeout(ctrl->input_aid, &retval, *timeout);
    240248        if (rc != EOK) {
     249                if (rc == ENOMEM)
     250                        return ENOMEM;
    241251                *timeout = 0;
    242                 errno = rc;
    243                 return false;
     252                return ETIMEOUT;
    244253        }
    245254
    246255        ctrl->input_aid = 0;
    247256
    248         if (retval != EOK) {
    249                 errno = retval;
    250                 return false;
    251         }
     257        if (retval != EOK)
     258                return EIO;
    252259
    253260        rc = console_ev_decode(&ctrl->input_call, event);
    254         if (rc != EOK) {
    255                 errno = rc;
    256                 return false;
    257         }
     261        if (rc != EOK)
     262                return EIO;
    258263
    259264        /* Update timeout */
     
    262267        *timeout -= NSEC2USEC(ts_sub_diff(&t1, &t0));
    263268
    264         return true;
     269        return EOK;
    265270}
    266271
Note: See TracChangeset for help on using the changeset viewer.