<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[119844] trunk/base/src/pextlib1.0/flock.c</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="https://trac.macports.org/changeset/119844">119844</a></dd>
<dt>Author</dt> <dd>cal@macports.org</dd>
<dt>Date</dt> <dd>2014-05-07 16:52:03 -0700 (Wed, 07 May 2014)</dd>
</dl>
<h3>Log Message</h3>
<pre>base: pextlib1.0/flock.c: Handle SIGINT/SIGTERM while waiting for a lock, closes #43388</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbasesrcpextlib10flockc">trunk/base/src/pextlib1.0/flock.c</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbasesrcpextlib10flockc"></a>
<div class="modfile"><h4>Modified: trunk/base/src/pextlib1.0/flock.c (119843 => 119844)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/base/src/pextlib1.0/flock.c        2014-05-07 23:42:31 UTC (rev 119843)
+++ trunk/base/src/pextlib1.0/flock.c        2014-05-07 23:52:03 UTC (rev 119844)
</span><span class="lines">@@ -40,24 +40,33 @@
</span><span class="cx">
</span><span class="cx"> #include <errno.h>
</span><span class="cx"> #include <inttypes.h>
</span><ins>+#include <signal.h>
</ins><span class="cx"> #include <string.h>
</span><ins>+#include <unistd.h>
</ins><span class="cx">
</span><span class="cx"> #include <tcl.h>
</span><span class="cx">
</span><span class="cx"> #include "flock.h"
</span><span class="cx">
</span><ins>+volatile int alarmReceived = 0;
+
+static void alarmHandler(int sig UNUSED) {
+ alarmReceived = 1;
+}
+
</ins><span class="cx"> int
</span><span class="cx"> FlockCmd(ClientData clientData UNUSED, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
</span><span class="cx"> static const char errorstr[] = "use one of \"-shared\", \"-exclusive\", or \"-unlock\", and optionally \"-noblock\"";
</span><del>- int operation = 0, fd, i, ret;
</del><ins>+ int operation = 0, fd, i, ret, sigret;
</ins><span class="cx"> int errnoval = 0;
</span><del>- int oshared = 0, oexclusive = 0, ounlock = 0, onoblock = 0;
</del><ins>+ int oshared = 0, oexclusive = 0, ounlock = 0, onoblock = 0, retry = 0;
</ins><span class="cx"> #if defined(HAVE_LOCKF) && !defined(HAVE_FLOCK)
</span><span class="cx"> off_t curpos;
</span><span class="cx"> #endif
</span><span class="cx"> char *res;
</span><span class="cx"> Tcl_Channel channel;
</span><span class="cx"> ClientData handle;
</span><ins>+ struct sigaction sa_oldalarm, sa_alarm;
</ins><span class="cx">
</span><span class="cx"> if (objc < 3 || objc > 4) {
</span><span class="cx"> Tcl_WrongNumArgs(interp, 1, objv, "channelId switches");
</span><span class="lines">@@ -104,76 +113,132 @@
</span><span class="cx"> return TCL_ERROR;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+ /* (re-)enable SIGALRM so we can use alarm(3) to specify a timeout for the
+ * locking, do some Tcl signal processing and restart the locking to solve
+ * #43388. */
+ memset(&sa_alarm, 0, sizeof(struct sigaction));
+ sigemptyset(&sa_alarm.sa_mask);
+ sa_alarm.sa_flags = 0; /* explicitly don't specify SA_RESTART, we want the
+ following alarm(3) to interrupt the locking. */
+ sa_alarm.sa_handler = alarmHandler;
+ sigaction(SIGALRM, &sa_alarm, &sa_oldalarm);
+
+ do {
+ /* use a delay of one second */
+ retry = 0;
+ alarmReceived = 0;
+ alarm(1);
</ins><span class="cx"> #if HAVE_FLOCK
</span><del>- /* prefer flock if present */
- if (oshared) {
- operation |= LOCK_SH;
- }
</del><ins>+ /* prefer flock if present */
+ if (oshared) {
+ operation |= LOCK_SH;
+ }
</ins><span class="cx">
</span><del>- if (oexclusive) {
- operation |= LOCK_EX;
- }
</del><ins>+ if (oexclusive) {
+ operation |= LOCK_EX;
+ }
</ins><span class="cx">
</span><del>- if (ounlock) {
- operation |= LOCK_UN;
- }
</del><ins>+ if (ounlock) {
+ operation |= LOCK_UN;
+ }
</ins><span class="cx">
</span><del>- if (onoblock) {
- operation |= LOCK_NB;
- }
</del><ins>+ if (onoblock) {
+ operation |= LOCK_NB;
+ }
</ins><span class="cx">
</span><del>- ret = flock(fd, operation);
- if (ret == -1) {
- errnoval = errno;
- }
</del><ins>+ ret = flock(fd, operation);
+ if (ret == -1) {
+ errnoval = errno;
+ }
</ins><span class="cx"> #else
</span><span class="cx"> #if HAVE_LOCKF
</span><del>- if (ounlock) {
- operation = F_ULOCK;
- }
</del><ins>+ if (ounlock) {
+ operation = F_ULOCK;
+ }
</ins><span class="cx">
</span><del>- /* lockf semantics don't map to shared locks. */
- if (oshared || oexclusive) {
- if (onoblock) {
- operation = F_TLOCK;
</del><ins>+ /* lockf semantics don't map to shared locks. */
+ if (oshared || oexclusive) {
+ if (onoblock) {
+ operation = F_TLOCK;
+ }
+ else {
+ operation = F_LOCK;
+ }
</ins><span class="cx"> }
</span><del>- else {
- operation = F_LOCK;
</del><ins>+
+ curpos = lseek(fd, 0, SEEK_CUR);
+ if (curpos == -1) {
+ Tcl_SetResult(interp, (void *) "Seek error", TCL_STATIC);
+ return TCL_ERROR;
</ins><span class="cx"> }
</span><del>- }
</del><span class="cx">
</span><del>- curpos = lseek(fd, 0, SEEK_CUR);
- if (curpos == -1) {
- Tcl_SetResult(interp, (void *) "Seek error", TCL_STATIC);
- return TCL_ERROR;
- }
</del><ins>+ ret = lockf(fd, operation, 0); /* lock entire file */
+ if (ret == -1) {
+ errnoval = errno;
+ }
</ins><span class="cx">
</span><del>- ret = lockf(fd, operation, 0); /* lock entire file */
</del><ins>+ curpos = lseek(fd, curpos, SEEK_SET);
+ if (curpos == -1) {
+ Tcl_SetResult(interp, (void *) "Seek error", TCL_STATIC);
+ return TCL_ERROR;
+ }
</ins><span class="cx">
</span><del>- curpos = lseek(fd, curpos, SEEK_SET);
- if (curpos == -1) {
- Tcl_SetResult(interp, (void *) "Seek error", TCL_STATIC);
- return TCL_ERROR;
- }
-
- if (ret == -1) {
- errnoval = errno;
- if ((oshared || oexclusive)) {
- /* map the errno val to what we would expect for flock */
- if (onoblock && errnoval == EAGAIN) {
- /* on some systems, EAGAIN=EWOULDBLOCK, but lets be safe */
- errnoval = EWOULDBLOCK;
</del><ins>+ if (ret == -1) {
+ errnoval = errno;
+ if ((oshared || oexclusive)) {
+ /* map the errno val to what we would expect for flock */
+ if (onoblock && errnoval == EAGAIN) {
+ /* on some systems, EAGAIN=EWOULDBLOCK, but lets be safe */
+ errnoval = EWOULDBLOCK;
+ }
+ else if (errnoval == EINVAL) {
+ errnoval = EOPNOTSUPP;
+ }
</ins><span class="cx"> }
</span><del>- else if (errnoval == EINVAL) {
- errnoval = EOPNOTSUPP;
- }
</del><span class="cx"> }
</span><del>- }
</del><span class="cx"> #else
</span><span class="cx"> #error no available locking implementation
</span><span class="cx"> #endif /* HAVE_LOCKF */
</span><span class="cx"> #endif /* HAVE_FLOCK */
</span><ins>+ /* disable the alarm timer */
+ alarm(0);
</ins><span class="cx">
</span><ins>+ if (ret == -1) {
+ if (oshared || oexclusive) {
+ if (!onoblock && alarmReceived && errnoval == EINTR) {
+ /* We were trying to lock, the lock was supposed to block,
+ * it failed with EINTR and we processed a SIGALRM. This
+ * probably means the call was interrupted by the timer.
+ * Call Tcl signal processing functions and try again. */
+ if (Tcl_AsyncReady()) {
+ sigret = Tcl_AsyncInvoke(interp, TCL_OK);
+ if (sigret != TCL_OK) {
+ break;
+ }
+ }
+ retry = 1;
+ continue;
+ }
+
+ if (onoblock && errnoval == EAGAIN) {
+ /* The lock wasn't supposed to block, and the lock wasn't
+ * successful because the lock is taken. On some systems
+ * EAGAIN == EWOULDBLOCK, but let's play it safe. */
+ errnoval = EWOULDBLOCK;
+ }
+ }
+ }
+ } while (retry);
+
+ /* Restore the previous handler for SIGALRM */
+ sigaction(SIGALRM, &sa_oldalarm, NULL);
+
+ if (sigret != TCL_OK) {
+ /* We received a signal that raised an error. The file hasn't been
+ * locked. */
+ return sigret;
+ }
+
</ins><span class="cx"> if (ret != 0) {
</span><span class="cx"> switch (errnoval) {
</span><span class="cx"> case EAGAIN:
</span></span></pre>
</div>
</div>
</body>
</html>