]>
Commit | Line | Data |
---|---|---|
fa59b86f | 1 | /* $Id$ |
e2a67c78 | 2 | * |
7ac48069 | 3 | * This routine is part of the client library. It handles |
4 | * creating a connection to the moira server. | |
5eaef520 | 5 | * |
7ac48069 | 6 | * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology |
7 | * For copying and distribution information, please see the file | |
8 | * <mit-copyright.h>. | |
e2a67c78 | 9 | */ |
10 | ||
babbc197 | 11 | #include <mit-copyright.h> |
7ac48069 | 12 | #include <moira.h> |
8defc06b | 13 | #include <moira_site.h> |
7ac48069 | 14 | #include "mr_private.h" |
15 | ||
16 | #include <errno.h> | |
a43ce477 | 17 | #include <stdlib.h> |
7ac48069 | 18 | #include <string.h> |
19 | ||
a43ce477 | 20 | #include <hesiod.h> |
05fb8281 | 21 | |
7ac48069 | 22 | RCSID("$Header$"); |
23 | ||
8defc06b | 24 | static char *mr_server_host = 0; |
e2a67c78 | 25 | |
26 | /* | |
8defc06b | 27 | * Open a connection to the mr server. Looks for the server name |
5450e3a1 | 28 | * 1) passed as an argument, 2) in environment variable, 3) by hesiod |
59ec8dae | 29 | * 4) compiled in default (from moira_site.h). |
e2a67c78 | 30 | */ |
31 | ||
5eaef520 | 32 | int mr_connect(char *server) |
e2a67c78 | 33 | { |
a3bbcc2b | 34 | char *p, **pp, *sbuf = NULL; |
5eaef520 | 35 | |
36 | if (!mr_inited) | |
37 | mr_init(); | |
38 | if (_mr_conn) | |
39 | return MR_ALREADY_CONNECTED; | |
40 | ||
41 | if (!server || (strlen(server) == 0)) | |
42 | server = getenv("MOIRASERVER"); | |
5450e3a1 | 43 | |
44 | #ifdef HESIOD | |
5eaef520 | 45 | if (!server || (strlen(server) == 0)) |
46 | { | |
47 | pp = hes_resolve("moira", "sloc"); | |
48 | if (pp) | |
49 | server = *pp; | |
5450e3a1 | 50 | } |
a43ce477 | 51 | #endif |
5450e3a1 | 52 | |
5eaef520 | 53 | if (!server || (strlen(server) == 0)) |
54 | server = MOIRA_SERVER; | |
5450e3a1 | 55 | |
5eaef520 | 56 | if (!strchr(server, ':')) |
57 | { | |
58 | p = strchr(MOIRA_SERVER, ':'); | |
59 | p++; | |
a3bbcc2b | 60 | sbuf = malloc(strlen(server) + strlen(p) + 2); |
61 | if (!sbuf) | |
62 | return ENOMEM; | |
5eaef520 | 63 | sprintf(sbuf, "%s:%s", server, p); |
64 | server = sbuf; | |
5450e3a1 | 65 | } |
66 | ||
5eaef520 | 67 | errno = 0; |
68 | _mr_conn = start_server_connection(server, ""); | |
69 | if (_mr_conn == NULL) | |
70 | return errno; | |
71 | if (connection_status(_mr_conn) == CON_STOPPED) | |
72 | { | |
44d12d58 | 73 | int status = connection_errno(_mr_conn); |
5eaef520 | 74 | if (!status) |
75 | status = MR_CANT_CONNECT; | |
76 | mr_disconnect(); | |
77 | return status; | |
83e80378 | 78 | } |
05fb8281 | 79 | |
5eaef520 | 80 | /* |
81 | * stash hostname for later use | |
82 | */ | |
05fb8281 | 83 | |
a3bbcc2b | 84 | if (!sbuf) |
85 | sbuf = strdup(server); | |
86 | mr_server_host = sbuf; | |
5eaef520 | 87 | if ((p = strchr(mr_server_host, ':'))) |
88 | *p = '\0'; | |
89 | mr_server_host = canonicalize_hostname(mr_server_host); | |
90 | return 0; | |
e2a67c78 | 91 | } |
5eaef520 | 92 | |
93 | int mr_disconnect(void) | |
e2a67c78 | 94 | { |
5eaef520 | 95 | CHECK_CONNECTED; |
96 | _mr_conn = sever_connection(_mr_conn); | |
97 | free(mr_server_host); | |
98 | mr_server_host = 0; | |
99 | return 0; | |
e2a67c78 | 100 | } |
101 | ||
5eaef520 | 102 | int mr_host(char *host, int size) |
05fb8281 | 103 | { |
5eaef520 | 104 | CHECK_CONNECTED; |
05fb8281 | 105 | |
5eaef520 | 106 | /* If we are connected, mr_server_host points to a valid string. */ |
107 | strncpy(host, mr_server_host, size); | |
108 | return 0; | |
05fb8281 | 109 | } |
110 | ||
5eaef520 | 111 | int mr_noop(void) |
e2a67c78 | 112 | { |
5eaef520 | 113 | int status; |
114 | mr_params param_st; | |
115 | struct mr_params *params = NULL; | |
116 | struct mr_params *reply = NULL; | |
117 | ||
118 | CHECK_CONNECTED; | |
119 | params = ¶m_st; | |
120 | params->mr_version_no = sending_version_no; | |
121 | params->mr_procno = MR_NOOP; | |
122 | params->mr_argc = 0; | |
123 | params->mr_argl = NULL; | |
124 | params->mr_argv = NULL; | |
125 | ||
126 | if ((status = mr_do_call(params, &reply)) == 0) | |
127 | status = reply->mr_status; | |
128 | ||
129 | mr_destroy_reply(reply); | |
130 | ||
131 | return status; | |
6c00b7c4 | 132 | } |