From owner-freebsd-current@FreeBSD.ORG Tue Mar 4 13:09:07 2008 Return-Path: Delivered-To: freebsd-current@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id ADCF1106567D; Tue, 4 Mar 2008 13:09:07 +0000 (UTC) (envelope-from jh@saunalahti.fi) Received: from gw03.mail.saunalahti.fi (gw03.mail.saunalahti.fi [195.197.172.111]) by mx1.freebsd.org (Postfix) with ESMTP id 3C5BF8FC1D; Tue, 4 Mar 2008 13:09:07 +0000 (UTC) (envelope-from jh@saunalahti.fi) Received: from ws64.jh.dy.fi (GMMDCCCXXIII.dsl.saunalahti.fi [85.76.243.24]) by gw03.mail.saunalahti.fi (Postfix) with ESMTP id 9524021688D; Tue, 4 Mar 2008 14:53:04 +0200 (EET) Received: from ws64.jh.dy.fi (localhost [127.0.0.1]) by ws64.jh.dy.fi (8.14.2/8.14.2) with ESMTP id m24Cr4LF002868; Tue, 4 Mar 2008 14:53:04 +0200 (EET) (envelope-from jh@saunalahti.fi) Received: (from jaakko@localhost) by ws64.jh.dy.fi (8.14.2/8.14.2/Submit) id m24Cr3QI002867; Tue, 4 Mar 2008 14:53:03 +0200 (EET) (envelope-from jh@saunalahti.fi) Date: Tue, 4 Mar 2008 14:53:03 +0200 From: Jaakko Heinonen To: sos@FreeBSD.org, freebsd-current@FreeBSD.org Message-ID: <20080304125303.GA2619@ws64.jh.dy.fi> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="liOOAslEiF7prFVr" Content-Disposition: inline User-Agent: Mutt/1.5.17 (2007-11-01) Cc: Subject: [patch] burncd(8) signal and error handling improvements X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 04 Mar 2008 13:09:07 -0000 --liOOAslEiF7prFVr Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi, burncd(8) doesn't handle signals and interrupting burncd during operation for example with SIGINT (^C) may leave the drive spinning and locked. This may happen also if you try to write a too large image to a disc and burncd(8) exits with an I/O error. Attached patch implements signal handling for burncd(8). It does CDRIOCFLUSH ioctl to attempt leave burner sane state when burning is interrupted with SIGHUP, SIGINT, SIGTERM or in case an I/O error occurs during write. Blanking will still continue after interrupt but it seems to finish correctly even after burncd(8) has quit. Relevant PR is bin/48730 which has an older version of the patch. -- Jaakko --liOOAslEiF7prFVr Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="burncd-signal-handling.diff" Index: burncd.c =================================================================== RCS file: /home/ncvs/src/usr.sbin/burncd/burncd.c,v retrieving revision 1.45 diff -p -u -r1.45 burncd.c --- burncd.c 13 May 2005 20:06:44 -0000 1.45 +++ burncd.c 3 Mar 2008 10:16:48 -0000 @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -67,6 +68,8 @@ int write_file(int fd, struct track_info int roundup_blocks(struct track_info *); void cue_ent(struct cdr_cue_entry *, int, int, int, int, int, int, int); void cleanup(int); +void cleanup_flush(void); +void cleanup_signal(int); void usage(void); int @@ -157,6 +160,9 @@ main(int argc, char **argv) global_fd_for_cleanup = fd; err_set_exit(cleanup); + signal(SIGHUP, cleanup_signal); + signal(SIGINT, cleanup_signal); + signal(SIGTERM, cleanup_signal); for (arg = 0; arg < argc; arg++) { if (!strcasecmp(argv[arg], "fixate")) { @@ -319,6 +325,10 @@ main(int argc, char **argv) if (eject) if (ioctl(fd, CDIOCEJECT) < 0) err(EX_IOERR, "ioctl(CDIOCEJECT)"); + + signal(SIGHUP, SIG_DFL); + signal(SIGINT, SIG_DFL); + signal(SIGTERM, SIG_DFL); close(fd); exit(EX_OK); } @@ -469,8 +479,10 @@ do_DAO(int fd, int test_write, int multi err(EX_IOERR, "ioctl(CDRIOCSENDCUE)"); for (i = 0; i < notracks; i++) { - if (write_file(fd, &tracks[i])) + if (write_file(fd, &tracks[i])) { + cleanup_flush(); err(EX_IOERR, "write_file"); + } } ioctl(fd, CDRIOCFLUSH); @@ -499,8 +511,10 @@ do_TAO(int fd, int test_write, int preem if (!quiet) fprintf(stderr, "next writeable LBA %d\n", tracks[i].addr); - if (write_file(fd, &tracks[i])) + if (write_file(fd, &tracks[i])) { + cleanup_flush(); err(EX_IOERR, "write_file"); + } if (ioctl(fd, CDRIOCFLUSH) < 0) err(EX_IOERR, "ioctl(CDRIOCFLUSH)"); } @@ -630,9 +644,11 @@ write_file(int fd, struct track_info *tr track_info->block_size; } if ((res = write(fd, buf, count)) != count) { - if (res == -1) - fprintf(stderr, "\n%s\n", strerror(errno)); - else + if (res == -1) { + fprintf(stderr, "\n"); + close(track_info->file); + return errno; + } else fprintf(stderr, "\nonly wrote %d of %jd" " bytes\n", res, (intmax_t)count); break; @@ -690,6 +706,21 @@ cleanup(int dummy __unused) if (ioctl(global_fd_for_cleanup, CDRIOCSETBLOCKSIZE, &saved_block_size) < 0) err(EX_IOERR, "ioctl(CDRIOCSETBLOCKSIZE)"); +} + +void +cleanup_flush(void) +{ + if (ioctl(global_fd_for_cleanup, CDRIOCFLUSH) < 0) + err(EX_IOERR, "ioctl(CDRIOCFLUSH)"); +} + +void +cleanup_signal(int sig __unused) +{ + cleanup_flush(); + fprintf(stderr, "\n"); + errx(EXIT_FAILURE, "Aborted"); } void --liOOAslEiF7prFVr--