Return mdp inet & http port numbers in servald start & status

This commit is contained in:
Jeremy Lakeman 2014-02-25 12:42:36 +10:30
parent 86e01cb87d
commit a6f408f013
10 changed files with 104 additions and 16 deletions

View File

@ -936,6 +936,15 @@ int app_server_start(const struct cli_parsed *parsed, struct cli_context *contex
cli_put_string(context, serval_instancepath(), "\n");
cli_field_name(context, "pid", ":");
cli_put_long(context, pid, "\n");
char buff[256];
if (server_get_proc_state("http_port", buff, sizeof buff)!=-1){
cli_field_name(context, "http_port", ":");
cli_put_string(context, buff, "\n");
}
if (server_get_proc_state("mdp_inet_port", buff, sizeof buff)!=-1){
cli_field_name(context, "mdp_inet_port", ":");
cli_put_string(context, buff, "\n");
}
cli_flush(context);
/* Sleep before returning if env var is set. This is used in testing, to simulate the situation
on Android phones where the "start" command is invoked via the JNI interface and the calling
@ -1017,6 +1026,15 @@ int app_server_status(const struct cli_parsed *parsed, struct cli_context *conte
if (pid > 0) {
cli_field_name(context, "pid", ":");
cli_put_long(context, pid, "\n");
char buff[256];
if (server_get_proc_state("http_port", buff, sizeof buff)!=-1){
cli_field_name(context, "http_port", ":");
cli_put_string(context, buff, "\n");
}
if (server_get_proc_state("mdp_inet_port", buff, sizeof buff)!=-1){
cli_field_name(context, "mdp_inet_port", ":");
cli_put_string(context, buff, "\n");
}
}
return pid > 0 ? 0 : 1;
}

View File

@ -205,7 +205,7 @@ error:
return WHY("Failed to start HTTP server");
success:
server_write_proc_state("http_port", "%d\n", port);
server_write_proc_state("http_port", "%d", port);
INFOF("HTTP SERVER START port=%"PRIu16" fd=%d services=RESTful%s%s",
port,
httpd_server_socket,

View File

@ -45,9 +45,9 @@ public class MdpDnaLookup extends ChannelSelector.Handler{
throw new IOException("Expected 4 result fields");
ServalDCommand.LookupResult result = new ServalDCommand.LookupResult();
result.subscriberId = new SubscriberId(fields[0]);
result.did = fields[1];
result.name = fields[2];
result.uri = fields[3];
result.uri = fields[1];
result.did = fields[2];
result.name = fields[3];
results.result(result);
} catch (IOException e) {
e.printStackTrace();

View File

@ -60,6 +60,7 @@ public class MdpSocket{
packet.setFlags(MdpPacket.MDP_FLAG_BIND);
packet.payload.flip();
packet.send(channel);
channel.socket().setSoTimeout(5000);
// should throw MdpError on bind failures
receive(packet);
try {

View File

@ -99,6 +99,8 @@ public class ServalDCommand
public int tries;
public String instancePath;
public String status;
public int mdpInetPort;
public int httpPort;
@Override
public void putString(String value) {
@ -106,6 +108,10 @@ public class ServalDCommand
instancePath=value;
if (columnName.equals("status"))
status=value;
if (columnName.equals("mdp_inet_port"))
mdpInetPort=Integer.parseInt(value);
if (columnName.equals("http_port"))
httpPort=Integer.parseInt(value);
}
@Override
@ -123,6 +129,8 @@ public class ServalDCommand
", tries=" + tries +
", instancePath='" + instancePath + '\'' +
", status='" + status + '\'' +
", mdpInetPort='" + mdpInetPort + '\'' +
", httpPort='" + httpPort + '\'' +
'}';
}
}

View File

@ -34,7 +34,12 @@ public class CommandLine {
}
static void lookup(String did) throws IOException, InterruptedException, ServalDFailureException {
MdpSocket.loopbackMdpPort = Integer.parseInt(System.getenv("SERVAL_MDP_INET_PORT"));
ServalDCommand.Status s = ServalDCommand.serverStatus();
System.out.println(s);
if (s.getResult()!=0)
throw new ServalDFailureException("Serval daemon isn't running");
System.out.println(s);
MdpSocket.loopbackMdpPort = s.mdpInetPort;
ChannelSelector selector = new ChannelSelector();
MdpDnaLookup lookup = new MdpDnaLookup(selector, new AsyncResult<ServalDCommand.LookupResult>() {
@Override

View File

@ -202,7 +202,7 @@ int overlay_mdp_setup_sockets()
fd = -1;
mdp_sock2_inet.poll.events = POLLIN;
watch(&mdp_sock2_inet);
server_write_proc_state("mdp_inet_port", "%d\n", port);
server_write_proc_state("mdp_inet_port", "%d", port);
INFOF("Socket mdp.2.inet: fd=%d %s", fd, alloca_socket_address(&addr));
break;
}

View File

@ -307,6 +307,7 @@ void server_save_argv(int argc, const char *const *argv);
int server(const struct cli_parsed *parsed);
int server_write_pid();
int server_write_proc_state(const char *path, const char *fmt, ...);
int server_get_proc_state(const char *path, char *buff, size_t buff_len);
int server_create_stopfile();
int server_remove_stopfile();
int server_check_stopfile();

View File

@ -17,6 +17,7 @@ along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <dirent.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>
@ -136,24 +137,31 @@ int server_write_pid()
return 0;
}
int server_write_proc_state(const char *path, const char *fmt, ...)
static int get_proc_path(const char *path, char *buff, size_t buff_len)
{
char path_buf[400];
strbuf sbname = strbuf_local(path_buf, sizeof path_buf);
strbuf sbname = strbuf_local(buff, buff_len);
strbuf_path_join(sbname, serval_instancepath(), "proc", path, NULL);
if (strbuf_overrun(sbname))
return WHY("Buffer overrun building proc filename");
char *name = strbuf_str(sbname);
size_t dirsiz = strlen(name) + 1;
return 0;
}
int server_write_proc_state(const char *path, const char *fmt, ...)
{
char path_buf[400];
if (get_proc_path(path, path_buf, sizeof path_buf)==-1)
return -1;
size_t dirsiz = strlen(path_buf) + 1;
char dir_buf[dirsiz];
strcpy(dir_buf, name);
strcpy(dir_buf, path_buf);
const char *dir = dirname(dir_buf); // modifies dir_buf[]
if (mkdirs(dir, 0700) == -1)
return WHY_perror("mkdirs()");
FILE *f = fopen(name, "w");
FILE *f = fopen(path_buf, "w");
if (!f)
return WHY_perror("fopen()");
va_list ap;
va_start(ap, fmt);
@ -164,6 +172,25 @@ int server_write_proc_state(const char *path, const char *fmt, ...)
return 0;
}
int server_get_proc_state(const char *path, char *buff, size_t buff_len)
{
char path_buf[400];
if (get_proc_path(path, path_buf, sizeof path_buf)==-1)
return -1;
FILE *f = fopen(path_buf, "r");
if (!f)
return -1;
int ret=0;
if (!fgets(buff, buff_len, f))
ret = WHY_perror("fgets");
fclose(f);
return ret;
}
/* Called periodically by the server process in its main loop.
*/
void server_config_reload(struct sched_ent *alarm)
@ -262,6 +289,33 @@ int server_check_stopfile()
return -1;
}
static void clean_proc()
{
char path_buf[400];
strbuf sbname = strbuf_local(path_buf, sizeof path_buf);
strbuf_path_join(sbname, serval_instancepath(), "proc", NULL);
DIR *dir;
struct dirent *dp;
if ((dir = opendir(path_buf)) == NULL) {
WARNF_perror("opendir(%s)", alloca_str_toprint(path_buf));
return;
}
while ((dp = readdir(dir)) != NULL) {
strbuf_reset(sbname);
strbuf_path_join(sbname, serval_instancepath(), "proc", dp->d_name, NULL);
struct stat st;
if (lstat(path_buf, &st)) {
WARNF_perror("stat(%s)", path_buf);
continue;
}
if (S_ISREG(st.st_mode))
unlink(path_buf);
}
}
void serverCleanUp()
{
if (serverMode){
@ -272,6 +326,8 @@ void serverCleanUp()
overlay_mdp_clean_socket_files();
clean_proc();
/* Try to remove shutdown and PID files and exit */
server_remove_stopfile();
}

View File

@ -124,7 +124,6 @@ setup_DnaLookup() {
set_instance +A
}
test_DnaLookup() {
export SERVAL_MDP_INET_PORT=$(<"$SERVALINSTANCE_PATH/proc/mdp_inet_port")
execute --timeout=10 --core-backtrace java "-Djava.library.path=$LD_LIBRARY_PATH" -classpath "$PWD/classes" org.servalproject.test.CommandLine 'lookup'
assertStdoutGrep "$SIDB"
tfw_cat --stdout --stderr