From patchwork Wed Jun 12 06:48:27 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adam Miartus X-Patchwork-Id: 10988829 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BF4641515 for ; Wed, 12 Jun 2019 07:29:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A2E46289D3 for ; Wed, 12 Jun 2019 07:29:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A1300289DA; Wed, 12 Jun 2019 07:29:06 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id D66E8289D3 for ; Wed, 12 Jun 2019 07:29:03 +0000 (UTC) Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id BE03B1717; Wed, 12 Jun 2019 08:49:47 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz BE03B1717 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1560322237; bh=ETsdjMRXowIqA9u52jkTUu1RCtn2SVID+sZxAS59lak=; h=From:To:Date:In-Reply-To:References:Cc:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=vLmRuooNKc5YWqhfJGNe86h+SaK5DhjiTwji2boraR45rRIPPqAxF7k8TziUIxv+5 BkpgkUAgjXTSfmpagVv6pgy5/q9JLZ7+GaQrjDUZKV5x282CxJbP1POIHe50uZIvwq 4O6rmMP8jl3JUILc56HEd7xcTyFoQ4yPnOnNxg6M= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id D806DF8970C; Wed, 12 Jun 2019 08:48:53 +0200 (CEST) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa1.perex.cz (Postfix, from userid 50401) id EECA4F896E3; Wed, 12 Jun 2019 08:48:49 +0200 (CEST) Received: from smtp1.de.adit-jv.com (smtp1.de.adit-jv.com [93.241.18.167]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 92CD8F806F5; Wed, 12 Jun 2019 08:48:43 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 92CD8F806F5 Received: from localhost (smtp1.de.adit-jv.com [127.0.0.1]) by smtp1.de.adit-jv.com (Postfix) with ESMTP id 304E43C00BE; Wed, 12 Jun 2019 08:48:41 +0200 (CEST) Received: from smtp1.de.adit-jv.com ([127.0.0.1]) by localhost (smtp1.de.adit-jv.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id SYr4e9bqJsHL; Wed, 12 Jun 2019 08:48:35 +0200 (CEST) Received: from HI2EXCH01.adit-jv.com (hi2exch01.adit-jv.com [10.72.92.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by smtp1.de.adit-jv.com (Postfix) with ESMTPS id 95AA03C00C6; Wed, 12 Jun 2019 08:48:35 +0200 (CEST) Received: from vmlxhi-082.adit-jv.com (10.72.93.164) by HI2EXCH01.adit-jv.com (10.72.92.24) with Microsoft SMTP Server (TLS) id 14.3.439.0; Wed, 12 Jun 2019 08:48:35 +0200 From: Adam Miartus To: Date: Wed, 12 Jun 2019 08:48:27 +0200 Message-ID: <1560322108-17074-2-git-send-email-amiartus@de.adit-jv.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1560322108-17074-1-git-send-email-amiartus@de.adit-jv.com> References: <1560322108-17074-1-git-send-email-amiartus@de.adit-jv.com> MIME-Version: 1.0 X-Originating-IP: [10.72.93.164] Cc: alsa-devel@alsa-project.org Subject: [alsa-devel] [PATCH 1/2] pcm_file: do not disrupt playback on output file write fail X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" X-Virus-Scanned: ClamAV using ClamSMTP previously playback could be interrupted by snd_pcm_file_add_frames: assert(file->wbuf_used_bytes < file->wbuf_size_bytes) in case snd_pcm_file_write_bytes fails to write full amount of bytes to file, variable wbuf_used_bytes would not be fully decremented by requested amount of bytes function was called with for the assert to trigger, multiple write fails need to happen, so that wbuf_used_bytes overflows wbuf_size_bytes, this patch will allow application to report error code to api user who might have an idea how to recover, before assert is triggered, also reporting error along with the print out message might give user a better idea of what is going on, where previously reason for mentioned assert was not immediately clear Signed-off-by: Adam Miartus Reviewed-by: Timo Wischer --- src/pcm/pcm_file.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/pcm/pcm_file.c b/src/pcm/pcm_file.c index 1ef80b5..0a8d98d 100644 --- a/src/pcm/pcm_file.c +++ b/src/pcm/pcm_file.c @@ -346,7 +346,7 @@ static int write_wav_header(snd_pcm_t *pcm) sizeof(file->wav_header) || write(file->fd, header2, sizeof(header2)) != sizeof(header2)) { int err = errno; - SYSERR("Write error.\n"); + SYSERR("%s write header failed, file data may be corrupt", file->fname); return -err; } return 0; @@ -381,27 +381,29 @@ static void fixup_wav_header(snd_pcm_t *pcm) -static void snd_pcm_file_write_bytes(snd_pcm_t *pcm, size_t bytes) +/* return error code in case write failed */ +static int snd_pcm_file_write_bytes(snd_pcm_t *pcm, size_t bytes) { snd_pcm_file_t *file = pcm->private_data; + snd_pcm_sframes_t err = 0; assert(bytes <= file->wbuf_used_bytes); if (file->format == SND_PCM_FILE_FORMAT_WAV && !file->wav_header.fmt) { - if (write_wav_header(pcm) < 0) - return; + err = write_wav_header(pcm); + if (err < 0) + return err; } while (bytes > 0) { - snd_pcm_sframes_t err; size_t n = bytes; size_t cont = file->wbuf_size_bytes - file->file_ptr_bytes; if (n > cont) n = cont; err = write(file->fd, file->wbuf + file->file_ptr_bytes, n); if (err < 0) { - SYSERR("write failed"); - break; + SYSERR("%s write failed, file data may be corrupt", file->fname); + return err; } bytes -= err; file->wbuf_used_bytes -= err; @@ -412,15 +414,17 @@ static void snd_pcm_file_write_bytes(snd_pcm_t *pcm, size_t bytes) if ((snd_pcm_uframes_t)err != n) break; } + return 0; } -static void snd_pcm_file_add_frames(snd_pcm_t *pcm, - const snd_pcm_channel_area_t *areas, - snd_pcm_uframes_t offset, - snd_pcm_uframes_t frames) +static int snd_pcm_file_add_frames(snd_pcm_t *pcm, + const snd_pcm_channel_area_t *areas, + snd_pcm_uframes_t offset, + snd_pcm_uframes_t frames) { snd_pcm_file_t *file = pcm->private_data; while (frames > 0) { + int err = 0; snd_pcm_uframes_t n = frames; snd_pcm_uframes_t cont = file->wbuf_size - file->appl_ptr; snd_pcm_uframes_t avail = file->wbuf_size - snd_pcm_bytes_to_frames(pcm, file->wbuf_used_bytes); @@ -437,10 +441,14 @@ static void snd_pcm_file_add_frames(snd_pcm_t *pcm, if (file->appl_ptr == file->wbuf_size) file->appl_ptr = 0; file->wbuf_used_bytes += snd_pcm_frames_to_bytes(pcm, n); - if (file->wbuf_used_bytes > file->buffer_bytes) - snd_pcm_file_write_bytes(pcm, file->wbuf_used_bytes - file->buffer_bytes); + if (file->wbuf_used_bytes > file->buffer_bytes) { + err = snd_pcm_file_write_bytes(pcm, file->wbuf_used_bytes - file->buffer_bytes); + if (err < 0) + return err; + } assert(file->wbuf_used_bytes < file->wbuf_size_bytes); } + return 0; } static int snd_pcm_file_close(snd_pcm_t *pcm) From patchwork Wed Jun 12 06:48:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adam Miartus X-Patchwork-Id: 10988827 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 487D114E5 for ; Wed, 12 Jun 2019 07:29:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2D83C28426 for ; Wed, 12 Jun 2019 07:29:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 22142289D8; Wed, 12 Jun 2019 07:29:06 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id D6C67289D6 for ; Wed, 12 Jun 2019 07:29:03 +0000 (UTC) Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 51D781728; Wed, 12 Jun 2019 08:50:35 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 51D781728 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1560322285; bh=B1940Q+TyJP+rttYHzXbhxGBzB/IdwqZE8ZjLft7w+s=; h=From:To:Date:In-Reply-To:References:Cc:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=Oky8tukfwjZDypQFK0xEjJ/3T8a7LYUFjaw5/TwN8YXmiG10sPNs/D9FvhC4RrKVF r3sSEH+IhfQXHTUwtqiBnrpce/xP1PQ0wM8Extbkye6FQoG264UeT7cmBjUNPw04b0 saOUn1iOGoIgKD29u1/1FjSZNzYIkMGMluZOx2wo= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id 5EA5CF89734; Wed, 12 Jun 2019 08:48:56 +0200 (CEST) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa1.perex.cz (Postfix, from userid 50401) id 9C76AF896FA; Wed, 12 Jun 2019 08:48:52 +0200 (CEST) Received: from smtp1.de.adit-jv.com (smtp1.de.adit-jv.com [93.241.18.167]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 52A9FF80791; Wed, 12 Jun 2019 08:48:46 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 52A9FF80791 Received: from localhost (smtp1.de.adit-jv.com [127.0.0.1]) by smtp1.de.adit-jv.com (Postfix) with ESMTP id 0F4703C00BE; Wed, 12 Jun 2019 08:48:46 +0200 (CEST) Received: from smtp1.de.adit-jv.com ([127.0.0.1]) by localhost (smtp1.de.adit-jv.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id S8HneIzyG3JX; Wed, 12 Jun 2019 08:48:40 +0200 (CEST) Received: from HI2EXCH01.adit-jv.com (hi2exch01.adit-jv.com [10.72.92.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by smtp1.de.adit-jv.com (Postfix) with ESMTPS id CF4A43C00DD; Wed, 12 Jun 2019 08:48:35 +0200 (CEST) Received: from vmlxhi-082.adit-jv.com (10.72.93.164) by HI2EXCH01.adit-jv.com (10.72.92.24) with Microsoft SMTP Server (TLS) id 14.3.439.0; Wed, 12 Jun 2019 08:48:35 +0200 From: Adam Miartus To: Date: Wed, 12 Jun 2019 08:48:28 +0200 Message-ID: <1560322108-17074-3-git-send-email-amiartus@de.adit-jv.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1560322108-17074-1-git-send-email-amiartus@de.adit-jv.com> References: <1560322108-17074-1-git-send-email-amiartus@de.adit-jv.com> MIME-Version: 1.0 X-Originating-IP: [10.72.93.164] Cc: alsa-devel@alsa-project.org Subject: [alsa-devel] [PATCH 2/2] pcm_file: report write output file error to api user X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" X-Virus-Scanned: ClamAV using ClamSMTP when writing to output file fails, api user is notified and can handle recovery Signed-off-by: Adam Miartus Reviewed-by: Timo Wischer --- src/pcm/pcm_file.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/pcm/pcm_file.c b/src/pcm/pcm_file.c index 0a8d98d..ca8e0c8 100644 --- a/src/pcm/pcm_file.c +++ b/src/pcm/pcm_file.c @@ -572,7 +572,10 @@ static snd_pcm_sframes_t snd_pcm_file_writei(snd_pcm_t *pcm, const void *buffer, if (n > 0) { snd_pcm_areas_from_buf(pcm, areas, (void*) buffer); __snd_pcm_lock(pcm); - snd_pcm_file_add_frames(pcm, areas, 0, n); + if (snd_pcm_file_add_frames(pcm, areas, 0, n) < 0) { + __snd_pcm_unlock(pcm); + return -EPIPE; + } __snd_pcm_unlock(pcm); } return n; @@ -587,7 +590,10 @@ static snd_pcm_sframes_t snd_pcm_file_writen(snd_pcm_t *pcm, void **bufs, snd_pc if (n > 0) { snd_pcm_areas_from_bufs(pcm, areas, bufs); __snd_pcm_lock(pcm); - snd_pcm_file_add_frames(pcm, areas, 0, n); + if (snd_pcm_file_add_frames(pcm, areas, 0, n) < 0) { + __snd_pcm_unlock(pcm); + return -EPIPE; + } __snd_pcm_unlock(pcm); } return n; @@ -608,6 +614,11 @@ static snd_pcm_sframes_t snd_pcm_file_readi(snd_pcm_t *pcm, void *buffer, snd_pc snd_pcm_file_areas_read_infile(pcm, areas, 0, frames); __snd_pcm_lock(pcm); snd_pcm_file_add_frames(pcm, areas, 0, frames); + if (snd_pcm_file_add_frames(pcm, areas, 0, frames) < 0) { + __snd_pcm_unlock(pcm); + return -EPIPE; + } + __snd_pcm_unlock(pcm); return frames; @@ -627,7 +638,11 @@ static snd_pcm_sframes_t snd_pcm_file_readn(snd_pcm_t *pcm, void **bufs, snd_pcm snd_pcm_areas_from_bufs(pcm, areas, bufs); snd_pcm_file_areas_read_infile(pcm, areas, 0, frames); __snd_pcm_lock(pcm); - snd_pcm_file_add_frames(pcm, areas, 0, frames); + if (snd_pcm_file_add_frames(pcm, areas, 0, frames) < 0) { + __snd_pcm_unlock(pcm); + return -EPIPE; + } + __snd_pcm_unlock(pcm); return frames; @@ -649,8 +664,10 @@ static snd_pcm_sframes_t snd_pcm_file_mmap_commit(snd_pcm_t *pcm, if (result >= 0) { assert(ofs == offset && siz == size); result = snd_pcm_mmap_commit(file->gen.slave, ofs, siz); - if (result > 0) - snd_pcm_file_add_frames(pcm, areas, ofs, result); + if (result > 0) { + if (snd_pcm_file_add_frames(pcm, areas, ofs, result) < 0) + return -EPIPE; + } } return result; }