Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libcoshell/coclose.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1990-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#pragma prototyped
21
/*
22
* Glenn Fowler
23
* AT&T Research
24
*
25
* close a coshell
26
*/
27
28
#include "colib.h"
29
30
/*
31
* called when coshell is hung
32
*/
33
34
static void
35
hung(int sig)
36
{
37
NoP(sig);
38
kill(state.current->pid, SIGKILL);
39
}
40
41
/*
42
* shut down one coshell
43
*/
44
45
static int
46
shut(register Coshell_t* co)
47
{
48
register Coshell_t* cs;
49
int n;
50
int status;
51
Coshell_t* ps;
52
Coservice_t* sv;
53
Sig_handler_t handler;
54
55
sfclose(co->msgfp);
56
close(co->cmdfd);
57
if (co->pid)
58
{
59
if (co->running > 0)
60
killpg(co->pid, SIGTERM);
61
state.current = co;
62
handler = signal(SIGALRM, hung);
63
n = alarm(3);
64
if (waitpid(co->pid, &status, 0) != co->pid)
65
status = -1;
66
alarm(n);
67
signal(SIGALRM, handler);
68
killpg(co->pid, SIGTERM);
69
}
70
else
71
status = 0;
72
if (co->flags & CO_DEBUG)
73
errormsg(state.lib, 2, "coshell %d jobs %d user %s sys %s", co->index, co->total, fmtelapsed(co->user, CO_QUANT), fmtelapsed(co->sys, CO_QUANT));
74
for (sv = co->service; sv; sv = sv->next)
75
{
76
if (sv->fd > 0)
77
close(sv->fd);
78
if (sv->pid)
79
waitpid(sv->pid, &status, 0);
80
}
81
cs = state.coshells;
82
ps = 0;
83
while (cs)
84
{
85
if (cs == co)
86
{
87
cs = cs->next;
88
if (ps)
89
ps->next = cs;
90
else
91
state.coshells = cs;
92
vmclose(co->vm);
93
break;
94
}
95
ps = cs;
96
cs = cs->next;
97
}
98
return status;
99
}
100
101
/*
102
* close coshell co
103
*/
104
105
int
106
coclose(register Coshell_t* co)
107
{
108
if (co)
109
return shut(co);
110
while (state.coshells)
111
shut(state.coshells);
112
return 0;
113
}
114
115