* Audit pipe read. Read one or more partial or complete records to user * memory. */
| 897 | * memory. |
| 898 | */ |
| 899 | static int |
| 900 | audit_pipe_read(struct cdev *dev, struct uio *uio, int flag) |
| 901 | { |
| 902 | struct audit_pipe_entry *ape; |
| 903 | struct audit_pipe *ap; |
| 904 | u_int toread; |
| 905 | int error; |
| 906 | |
| 907 | error = devfs_get_cdevpriv((void **)&ap); |
| 908 | if (error != 0) |
| 909 | return (error); |
| 910 | |
| 911 | /* |
| 912 | * We hold an sx(9) lock over read and flush because we rely on the |
| 913 | * stability of a record in the queue during uiomove(9). |
| 914 | */ |
| 915 | if (AUDIT_PIPE_SX_XLOCK_SIG(ap) != 0) |
| 916 | return (EINTR); |
| 917 | AUDIT_PIPE_LOCK(ap); |
| 918 | while (TAILQ_EMPTY(&ap->ap_queue)) { |
| 919 | if (ap->ap_flags & AUDIT_PIPE_NBIO) { |
| 920 | AUDIT_PIPE_UNLOCK(ap); |
| 921 | AUDIT_PIPE_SX_XUNLOCK(ap); |
| 922 | return (EAGAIN); |
| 923 | } |
| 924 | error = cv_wait_sig(&ap->ap_cv, AUDIT_PIPE_MTX(ap)); |
| 925 | if (error) { |
| 926 | AUDIT_PIPE_UNLOCK(ap); |
| 927 | AUDIT_PIPE_SX_XUNLOCK(ap); |
| 928 | return (error); |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | /* |
| 933 | * Copy as many remaining bytes from the current record to userspace |
| 934 | * as we can. Keep processing records until we run out of records in |
| 935 | * the queue, or until the user buffer runs out of space. |
| 936 | * |
| 937 | * Note: we rely on the SX lock to maintain ape's stability here. |
| 938 | */ |
| 939 | ap->ap_reads++; |
| 940 | while ((ape = TAILQ_FIRST(&ap->ap_queue)) != NULL && |
| 941 | uio->uio_resid > 0) { |
| 942 | AUDIT_PIPE_LOCK_ASSERT(ap); |
| 943 | |
| 944 | KASSERT(ape->ape_record_len > ap->ap_qoffset, |
| 945 | ("audit_pipe_read: record_len > qoffset (1)")); |
| 946 | toread = MIN(ape->ape_record_len - ap->ap_qoffset, |
| 947 | uio->uio_resid); |
| 948 | AUDIT_PIPE_UNLOCK(ap); |
| 949 | error = uiomove((char *)ape->ape_record + ap->ap_qoffset, |
| 950 | toread, uio); |
| 951 | if (error) { |
| 952 | AUDIT_PIPE_SX_XUNLOCK(ap); |
| 953 | return (error); |
| 954 | } |
| 955 | |
| 956 | /* |
nothing calls this directly
no test coverage detected