2 * $Id: pamc_client.c,v 1.1.1.1 2000/06/20 22:11:25 agmorgan Exp $
4 * Copyright (c) Andrew G. Morgan <morgan@ftp.kernel.org>
6 * pamc_start and pamc_end
15 static void __pamc_delete_path_list(pamc_handle_t pch)
19 for (i=0; pch->agent_paths[i]; ++i) {
20 free(pch->agent_paths[i]);
21 pch->agent_paths[i] = NULL;
24 free(pch->agent_paths);
25 pch->agent_paths = NULL;
29 * open the pamc library
32 pamc_handle_t pamc_start(void)
34 int i, count, last, this;
35 const char *default_path;
38 pch = calloc(1, sizeof(struct pamc_handle_s));
40 D(("no memory for *pch"));
44 pch->highest_fd_to_close = _PAMC_DEFAULT_TOP_FD;
46 default_path = getenv("PAMC_AGENT_PATH");
47 if (default_path == NULL) {
48 default_path = PAMC_SYSTEM_AGENT_PATH;
51 /* number of individual paths */
52 for (count=1, i=0; default_path[i]; ++i) {
53 if (default_path[i] == PAMC_SYSTEM_AGENT_SEPARATOR) {
58 pch->agent_paths = calloc(count+1, sizeof(char *));
59 if (pch->agent_paths == NULL) {
60 D(("no memory for path list"));
65 while ( default_path[i] || (i != last) ) {
66 if ( default_path[i] == PAMC_SYSTEM_AGENT_SEPARATOR
67 || !default_path[i] ) {
70 pch->agent_paths[this] = malloc(length = 1+i-last);
72 if (pch->agent_paths[this] == NULL) {
73 D(("no memory for next path"));
77 memcpy(pch->agent_paths[this], default_path + last, i-last);
78 pch->agent_paths[this][i-last] = '\0';
79 if (length > pch->max_path) {
80 pch->max_path = length;
83 if (++this == count) {
96 __pamc_delete_path_list(pch);
105 * shutdown each of the loaded agents and
108 static int __pamc_shutdown_agents(pamc_handle_t pch)
110 int retval = PAM_BPC_TRUE;
120 D(("cleaning up agent %p", this));
121 pch->chain = pch->chain->next;
123 D(("cleaning up agent: %s", this->id));
125 /* close off contact with agent and wait for it to shutdown */
132 pid = waitpid(this->pid, &status, 0);
133 if (pid == this->pid) {
135 D(("is exit:%d, exit val:%d",
136 WIFEXITED(status), WEXITSTATUS(status)));
138 if (!(WIFEXITED(status) && (WEXITSTATUS(status) == 0))) {
139 retval = PAM_BPC_FALSE;
142 D(("problem shutting down agent (%s): pid(%d) != waitpid(%d)!?",
143 this->id, this->pid, pid));
144 retval = PAM_BPC_FALSE;
148 memset(this->id, 0, this->id_length);
161 * close the pamc library
164 int pamc_end(pamc_handle_t *pch_p)
169 D(("called with no pch_p"));
170 return PAM_BPC_FALSE;
173 if (*pch_p == NULL) {
174 D(("called with no *pch_p"));
175 return PAM_BPC_FALSE;
178 D(("removing path_list"));
179 __pamc_delete_path_list(*pch_p);
181 D(("shutting down agents"));
182 retval = __pamc_shutdown_agents(*pch_p);
184 D(("freeing *pch_p"));