Index: channels/chan_dahdi.c
===================================================================
--- channels/chan_dahdi.c	(revision 139400)
+++ channels/chan_dahdi.c	(working copy)
@@ -952,6 +952,18 @@
 		close(fd);
 }
 
+static void dahdi_close_sub(struct dahdi_pvt *chan_pvt, int sub_num)
+{
+	dahdi_close(chan_pvt->subs[sub_num].dfd);
+	chan_pvt->subs[sub_num].dfd = -1;
+}
+
+static void dahdi_close_pri_fd(struct dahdi_pri *pri, int fd_num)
+{
+	dahdi_close(pri->fds[fd_num]);
+	pri->fds[fd_num] = -1;
+}
+
 static int dahdi_setlinear(int dfd, int linear)
 {
 	int res;
@@ -986,8 +998,7 @@
 				ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d: %s\n", x, strerror(errno));
 			if (ioctl(p->subs[x].dfd, DAHDI_CHANNO, &p->subs[x].chan) == 1) {
 				ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d: %s\n", p->subs[x].dfd, strerror(errno));
-				dahdi_close(p->subs[x].dfd);
-				p->subs[x].dfd = -1;
+				dahdi_close_sub(p, x);
 				return -1;
 			}
 			if (option_debug)
@@ -1008,10 +1019,7 @@
 		return -1;
 	}
 	ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
-	if (p->subs[x].dfd > -1) {
-		dahdi_close(p->subs[x].dfd);
-	}
-	p->subs[x].dfd = -1;
+	dahdi_close_sub(p, x);
 	p->subs[x].linear = 0;
 	p->subs[x].chan = 0;
 	p->subs[x].owner = NULL;
@@ -2261,6 +2269,7 @@
 	if (p->use_smdi)
 		ast_smdi_interface_unref(p->smdi_iface);
 	ast_mutex_destroy(&p->lock);
+	dahdi_close_sub(p, SUB_REAL);
 	if (p->owner)
 		p->owner->tech_pvt = NULL;
 	free(p);
@@ -2296,9 +2305,6 @@
 				else
 					ifend = NULL;
 			}
-			if (cur->subs[SUB_REAL].dfd > -1) {
-				dahdi_close(cur->subs[SUB_REAL].dfd);
-			}
 			destroy_dahdi_pvt(&cur);
 		}
 	} else {
@@ -2315,9 +2321,6 @@
 			else
 				ifend = NULL;
 		}
-		if (cur->subs[SUB_REAL].dfd > -1) {
-			dahdi_close(cur->subs[SUB_REAL].dfd);
-		}
 		destroy_dahdi_pvt(&cur);
 	}
 	return 0;
@@ -2340,8 +2343,6 @@
 		if (p->cidspill)
 			ast_free(p->cidspill);
 		/* Close the DAHDI thingy */
-		if (p->subs[SUB_REAL].dfd > -1)
-			dahdi_close(p->subs[SUB_REAL].dfd);
 		pl = p;
 		p = p->next;
 		x = pl->channel;
@@ -7232,7 +7233,7 @@
 		x = channels[y];
 		if (ioctl(fd, DAHDI_SPECIFY, &x)) {
 			ast_log(LOG_WARNING, "Failed to specify channel %d: %s\n", channels[y], strerror(errno));
-			dahdi_close(fd);
+			close(fd);
 			return -1;
 		}
 		if (ioctl(fd, DAHDI_GET_PARAMS, &p)) {
@@ -7241,18 +7242,18 @@
 		}
 		if (ioctl(fd, DAHDI_SPANSTAT, &si)) {
 			ast_log(LOG_WARNING, "Failed go get span information on channel %d (span %d): %s\n", channels[y], p.spanno, strerror(errno));
-			dahdi_close(fd);
+			close(fd);
 			return -1;
 		}
 		span = p.spanno - 1;
 		if (pris[span].trunkgroup) {
 			ast_log(LOG_WARNING, "Span %d is already provisioned for trunk group %d\n", span + 1, pris[span].trunkgroup);
-			dahdi_close(fd);
+			close(fd);
 			return -1;
 		}
 		if (pris[span].pvts[0]) {
 			ast_log(LOG_WARNING, "Span %d is already provisioned with channels (implicit PRI maybe?)\n", span + 1);
-			dahdi_close(fd);
+			close(fd);
 			return -1;
 		}
 		if (!y) {
@@ -7263,7 +7264,7 @@
 		pris[ospan].dchannels[y] = channels[y];
 		pris[ospan].dchanavail[y] |= DCHAN_PROVISIONED;
 		pris[span].span = span + 1;
-		dahdi_close(fd);
+		close(fd);
 	}
 	return 0;	
 }
@@ -8273,7 +8274,7 @@
 			else {
 				/* Looks good.  Drop the pseudo channel now, clear up the assignment, and
 				   wakeup the potential sleeper */
-				dahdi_close(crv->subs[SUB_REAL].dfd);
+				dahdi_close_sub(crv,SUB_REAL);
 				pri->pvts[principle]->call = crv->call;
 				pri_assign_bearer(crv, pri, pri->pvts[principle]);
 				ast_log(LOG_DEBUG, "Assigning bearer %d/%d to CRV %d:%d\n",
@@ -9642,22 +9643,19 @@
 		}
 		res = ioctl(pri->fds[i], DAHDI_GET_PARAMS, &p);
 		if (res) {
-			dahdi_close(pri->fds[i]);
-			pri->fds[i] = -1;
+			dahdi_close_pri_fd(pri, i);
 			ast_log(LOG_ERROR, "Unable to get parameters for D-channel %d (%s)\n", x, strerror(errno));
 			return -1;
 		}
 		if ((p.sigtype != DAHDI_SIG_HDLCFCS) && (p.sigtype != DAHDI_SIG_HARDHDLC)) {
-			dahdi_close(pri->fds[i]);
-			pri->fds[i] = -1;
+			dahdi_close_pri_fd(pri, i);
 			ast_log(LOG_ERROR, "D-channel %d is not in HDLC/FCS mode.  See /etc/DAHDI.conf\n", x);
 			return -1;
 		}
 		memset(&si, 0, sizeof(si));
 		res = ioctl(pri->fds[i], DAHDI_SPANSTAT, &si);
 		if (res) {
-			dahdi_close(pri->fds[i]);
-			pri->fds[i] = -1;
+			dahdi_close_pri_fd(pri, i);
 			ast_log(LOG_ERROR, "Unable to get span state for D-channel %d (%s)\n", x, strerror(errno));
 		}
 		if (!si.alarms)
@@ -9670,8 +9668,7 @@
 		bi.bufsize = 1024;
 		if (ioctl(pri->fds[i], DAHDI_SET_BUFINFO, &bi)) {
 			ast_log(LOG_ERROR, "Unable to set appropriate buffering on channel %d: %s\n", x, strerror(errno));
-			dahdi_close(pri->fds[i]);
-			pri->fds[i] = -1;
+			dahdi_close_pri_fd(pri, i);
 			return -1;
 		}
 		pri->dchans[i] = pri_new(pri->fds[i], pri->nodetype, pri->switchtype);
@@ -9686,8 +9683,7 @@
 		if (i)
 			pri_enslave(pri->dchans[0], pri->dchans[i]);
 		if (!pri->dchans[i]) {
-			dahdi_close(pri->fds[i]);
-			pri->fds[i] = -1;
+			dahdi_close_pri_fd(pri, i);
 			ast_log(LOG_ERROR, "Unable to create PRI structure\n");
 			return -1;
 		}
@@ -9707,8 +9703,7 @@
 		for (i = 0; i < NUM_DCHANS; i++) {
 			if (!pri->dchannels[i])
 				break;
-			dahdi_close(pri->fds[i]);
-			pri->fds[i] = -1;
+			dahdi_close_pri_fd(pri, i);
 		}
 		ast_log(LOG_ERROR, "Unable to spawn D-channel: %s\n", strerror(errno));
 		return -1;
@@ -10144,7 +10139,7 @@
 	#ifdef HAVE_PRI
 	for (i = 0; i < NUM_SPANS; i++) {
 		for (j = 0; j < NUM_DCHANS; j++)
-        	dahdi_close(pris[i].fds[j]);
+        		dahdi_close_pri_fd(&(pris[i]), j);
 	}
 
 	memset(pris, 0, sizeof(pris));
@@ -10861,8 +10856,7 @@
 		if (p->cidspill)
 			free(p->cidspill);
 		/* Close the DAHDI thingy */
-		if (p->subs[SUB_REAL].dfd > -1)
-			dahdi_close(p->subs[SUB_REAL].dfd);
+		dahdi_close_sub(p, SUB_REAL);
 		pl = p;
 		p = p->next;
 		x = pl->channel;
@@ -10879,7 +10873,7 @@
 		if (pris[i].master && (pris[i].master != AST_PTHREADT_NULL))
 			pthread_join(pris[i].master, NULL);
 		for (j = 0; j < NUM_DCHANS; j++) {
-			dahdi_close(pris[i].fds[j]);
+			dahdi_close_pri_fd(&(pris[i]), j);
 		}
 	}
 #endif
