diff --git a/backends/server/.cvsignore b/backends/server/.cvsignore index 8a712463..97da6c0f 100644 --- a/backends/server/.cvsignore +++ b/backends/server/.cvsignore @@ -7,3 +7,4 @@ Makefile.in marshal.c demarshal.c libgtop_server +command.h diff --git a/backends/server/ChangeLog b/backends/server/ChangeLog index 706bc6f0..6cad3e97 100644 --- a/backends/server/ChangeLog +++ b/backends/server/ChangeLog @@ -1,5 +1,10 @@ 2000-01-02 Martin Baulig + * command.pl: New file. Creates `command.h' from `command.h.in'. + * command.h.in: New file. Template file for `command.h'. + + * io.c, version.c: New files. Copied here from `src/daemon'. + * demarshal.pl: New file. Automatically creates `demarshal.c'. * demarshal.c: This automatically created file handles the demarshalling in the libgtop_server. diff --git a/backends/server/Makefile.am b/backends/server/Makefile.am index d199861f..1b06dd44 100644 --- a/backends/server/Makefile.am +++ b/backends/server/Makefile.am @@ -31,7 +31,9 @@ libgtop_backend_server_la_LDFLAGS = \ libgtop_server_SOURCES = \ server.c \ server.h \ - demarshal.c + demarshal.c \ + version.c \ + io.c libgtop_server_LDADD = \ $(top_builddir)/sysdeps/@sysdeps_dir@/libgtop_sysdeps_suid.la \ @@ -44,7 +46,14 @@ endif BUILT_SOURCES = \ marshal.c \ - demarshal.c + demarshal.c \ + command.h + +EXTRA_DIST = \ + marshal.pl \ + demarshal.pl \ + command.pl \ + command.h.in CLEANFILES = $(BUILT_SOURCES) @@ -56,3 +65,7 @@ demarshal.c: demarshal.pl $(top_builddir)/config.h $(top_srcdir)/features.def $( $(PERL) -I $(top_srcdir)/scripts $(srcdir)/demarshal.pl < $(top_srcdir)/features.def > tmp-t mv tmp-t demarshal.c +command.h: command.pl command.h.in $(top_builddir)/config.h $(top_srcdir)/features.def $(top_srcdir)/scripts/c_types.pl + $(PERL) -I $(top_srcdir)/scripts $(srcdir)/command.pl $(top_srcdir)/features.def $(srcdir)/command.h.in > tmp-t + mv tmp-t command.h + diff --git a/backends/server/command.c b/backends/server/command.c index 5ef4afc5..9dfefdae 100644 --- a/backends/server/command.c +++ b/backends/server/command.c @@ -23,7 +23,6 @@ Boston, MA 02111-1307, USA. */ -#include #include #include @@ -32,13 +31,16 @@ #include +#include "command.h" + void * glibtop_call_i (glibtop *server, glibtop_backend *backend, unsigned command, - size_t send_size, const void *send_buf, size_t recv_size, - void *recv_buf, int *retval_ptr) + size_t send_size, const void *send_buf, + size_t data_size, const void *data_buf, + size_t recv_size, void *recv_ptr, + int *retval_ptr) { glibtop_command cmnd; - glibtop_response response; int retval; glibtop_init_r (&server, 0, 0); @@ -53,13 +55,14 @@ glibtop_call_i (glibtop *server, glibtop_backend *backend, unsigned command, if (send_size <= _GLIBTOP_PARAM_SIZE) { memcpy (cmnd.parameter, send_buf, send_size); - cmnd.size = send_size; + cmnd.param_size = send_size; } else { - cmnd.data_size = send_size; + cmnd.send_size = send_size; } glibtop_write_i (server, backend, sizeof (glibtop_command), &cmnd); +#if 0 glibtop_read_i (server, backend, sizeof (glibtop_response), &response); #ifdef DEBUG @@ -82,6 +85,7 @@ glibtop_call_i (glibtop *server, glibtop_backend *backend, unsigned command, return ptr; } +#endif return NULL; } diff --git a/backends/server/command.h.in b/backends/server/command.h.in new file mode 100644 index 00000000..e5aad0f6 --- /dev/null +++ b/backends/server/command.h.in @@ -0,0 +1,51 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */ + +/* $Id$ */ + +@@AUTOGEN_COMMENT@@ + +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by Martin Baulig , April 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#ifndef __GLIBTOP_COMMAND_H__ +#define __GLIBTOP_COMMAND_H__ + +#include +#include + +BEGIN_LIBGTOP_DECLS + +@@GLIBTOP_COMMAND_CONSTANTS@@ + +#define _GLIBTOP_PARAM_SIZE 16 + +typedef struct _glibtop_command glibtop_command; + +struct _glibtop_command +{ + u_int64_t command; + u_int64_t param_size, send_size, data_size; + char parameter [_GLIBTOP_PARAM_SIZE]; +}; + +END_LIBGTOP_DECLS + +#endif diff --git a/backends/server/command.pl b/backends/server/command.pl new file mode 100644 index 00000000..f1e6429b --- /dev/null +++ b/backends/server/command.pl @@ -0,0 +1,76 @@ +#!/usr/bin/perl + +require 'c_types.pl'; + +die "Usage: $0 features.def command.h.in" unless $#ARGV == 1; + +$[ = 1; # set array base to 1 +$, = ' '; # set output field separator +$\ = "\n"; # set output record separator + +sub toupper { + local($_) = @_; + tr/a-z/A-Z/; + return $_; +} + +sub tolower { + local($_) = @_; + tr/A-Z/a-z/; + return $_; +} + +$constants_decl_code = ''; + +open FEATURESDEF, $ARGV[1] or + die "open ($ARGV[1]): $!"; + +while () { + chop; # strip record separator + + if (/^[^\#]/) { + &parse_features_def ($_); + } +} + +close FEATURESDEF; + +sub parse_features_def { + local($line) = @_; + @line_fields = split(/\|/, $line, 9999); + $retval = $line_fields[1]; + $element_def = $line_fields[3]; + $feature = $line_fields[2]; + $param_def = $line_fields[4]; + + $orig = $feature; + $feature =~ s/^@//; + $space = $feature; + $space =~ s/./ /g; + + $constants_decl_code .= sprintf + (qq[\#define %-40s %d\n], 'GLIBTOP_CMND_'.&toupper($feature), + ++$feature_count); + + $features{$feature_count} = $feature; +} + +chop $constants_decl_code; + +$auto_gen_comment = sprintf + (qq[/*\n * This file is automatically generated.\n * Please modify `command.pl' and `command.h.in'.\n */]); + +open COMMAND, $ARGV[2] or + die "open ($ARGV[2]): $!"; + +while () { + chop; + + s/^\s*\@\@GLIBTOP_COMMAND_CONSTANTS\@\@\s*$/$constants_decl_code/; + + s/^\s*\@\@AUTOGEN_COMMENT\@\@\s*$/$auto_gen_comment/; +} continue { + print $_; +} + +close COMMAND; diff --git a/backends/server/demarshal.pl b/backends/server/demarshal.pl index 1051a931..844f7d73 100644 --- a/backends/server/demarshal.pl +++ b/backends/server/demarshal.pl @@ -31,10 +31,10 @@ print ''; print '#include '; print '#include '; print ''; -print '#include '; print '#include '; print ''; print '#include '; +print '#include "command.h"'; print ''; $feature_count = 0; @@ -251,3 +251,25 @@ sub output { print $total_code; } +$func_decl_code = sprintf + (qq[int\nglibtop_demarshal_func_i (glibtop *server, glibtop_backend *backend, unsigned command, const void *send_ptr, size_t send_size, void *data_ptr, size_t data_size, int *retval_ptr)]); + +$switch_body_code = ''; + +for ($nr = 1; $nr <= $feature_count; $nr++) { + $feature = $features{$nr}; + + $switch_body_code .= sprintf + (qq[\tcase GLIBTOP_CMND_%s:\n\t\treturn _glibtop_demarshal_%s_i\n\t\t\t(server, backend, send_ptr, send_size,\n\t\t\t data_ptr, data_size, retval_ptr);\n], + &toupper ($feature), $feature); +} + +$switch_code = sprintf + (qq[\tswitch (command) {\n%s\tdefault:\n\t\treturn -GLIBTOP_ERROR_INVALID_ARGUMENT;\n\t}\n], $switch_body_code); + +$total_code = sprintf + (qq[%s\n{\n%s}\n\n], $func_decl_code, $switch_code); + +print $total_code; + +1; diff --git a/backends/server/glibtop-backend-private.h b/backends/server/glibtop-backend-private.h index 055d107b..b7aee9cb 100644 --- a/backends/server/glibtop-backend-private.h +++ b/backends/server/glibtop-backend-private.h @@ -36,8 +36,10 @@ struct _glibtop_backend_private void * glibtop_call_i (glibtop *server, glibtop_backend *backend, unsigned command, - size_t send_size, const void *send_buf, size_t recv_size, - void *recv_buf, int *retval_ptr); + size_t send_size, const void *send_ptr, + size_t data_size, const void *data_ptr, + size_t recv_size, void *recv_ptr, + int *retval_ptr); void glibtop_read_i (glibtop *server, glibtop_backend *backend, diff --git a/backends/server/io.c b/backends/server/io.c new file mode 100644 index 00000000..98fe3bac --- /dev/null +++ b/backends/server/io.c @@ -0,0 +1,113 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */ + +/* $Id$ */ + +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by Martin Baulig , April 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include "server.h" + +#if 0 + +void +do_output (int s, glibtop_response *resp, off_t offset, + size_t data_size, const void *data, int retval) +{ +#ifdef REAL_DEBUG + fprintf (stderr, "Really writing %d bytes at offset %lu.\n", + sizeof (glibtop_response), offset); +#endif + + resp->offset = offset; + resp->data_size = data_size; + + if (s == 0) { + if (write (1, (const void *) resp, sizeof (glibtop_response)) < 0) + glibtop_warn_io ("write"); + } else { + if (send (s, (const void *) resp, sizeof (glibtop_response), 0) < 0) + glibtop_warn_io ("send"); + } + + + if (s == 0) { + if (write (1, &retval, sizeof (int)) < 0) + glibtop_warn_io ("write retval"); + } else { + if (send (s, &retval, sizeof (int), 0) < 0) + glibtop_warn_io ("send retval"); + } + + if (resp->data_size) { +#ifdef REAL_DEBUG + fprintf (stderr, "Writing %d bytes of data.\n", resp->data_size); +#endif + + if (s == 0) { + if (write (1, data, resp->data_size) < 0) + glibtop_warn_io ("write"); + } else { + if (send (s, data, resp->data_size, 0) < 0) + glibtop_warn_io ("send"); + } + } +} + +#endif + +int +do_read (int s, void *ptr, size_t total_size) +{ + int nread; + char *tmp_ptr; + size_t already_read = 0, remaining = total_size; + + while (already_read < total_size) { + if (s) + nread = recv (s, ptr, remaining, 0); + else + nread = read (0, ptr, remaining); + + if ((already_read == 0) && (nread == 0)) { + glibtop_warn ("pid %d received eof.", getpid ()); + return 0; + } + + if (nread <= 0) { + glibtop_warn_io ("recv"); + return 0; + } + + already_read += nread; + remaining -= nread; + /* (char *) ptr += nread; */ + tmp_ptr = ptr; + tmp_ptr += nread; + ptr = tmp_ptr; + +#ifdef REAL_DEBUG + fprintf (stderr, "READ (%d): %d - %d - %d\n", + nread, already_read, remaining, total_size); +#endif + } + + return already_read; +} diff --git a/backends/server/marshal.pl b/backends/server/marshal.pl index 36e15958..e2842b92 100644 --- a/backends/server/marshal.pl +++ b/backends/server/marshal.pl @@ -31,7 +31,7 @@ print ''; print '#include '; print '#include '; print ''; -print '#include '; +print '#include "command.h"'; print '#include '; print ''; print '#include '; @@ -59,7 +59,8 @@ sub output { $space = $feature; $space =~ s/./ /g; - $features{++$feature_count} = $feature; + $features{++$feature_count} = $orig; + return if $orig =~ /^@/; if ($retval eq 'retval') { $retval_param = '&retval'; @@ -89,12 +90,16 @@ sub output { } $need_temp_storage = $always_use_temp_storage; - $size_code = "\t_LIBGTOP_SEND_len = 0;\n"; - $marshal_code = "\t_LIBGTOP_SEND_offset = 0;\n"; - $marshal_code .= "\tmemset (_LIBGTOP_SEND_buf, 0, _LIBGTOP_SEND_len);\n"; - $marshal_code .= "\t_LIBGTOP_SEND_ptr = ". + $local_var_init_code = "\t/* variable initialization */\n"; + $local_var_init_code .= "\t_LIBGTOP_SEND_len = 0;\n"; + $local_var_init_code .= "\t_LIBGTOP_DATA_len = 0;\n"; + $local_var_init_code .= "\t_LIBGTOP_SEND_offset = 0;\n"; + $local_var_init_code .= "\t_LIBGTOP_SEND_ptr = ". "(char *) _LIBGTOP_SEND_buf;\n"; + $marshal_code = "\t/* marshal start */\n"; + $marshal_code .= "\tmemset (_LIBGTOP_SEND_buf, 0, _LIBGTOP_SEND_len);\n"; $first_param_name = ''; + $size_code = ''; $call_param = ''; $param_decl = ''; @@ -128,13 +133,19 @@ sub output { $call_param = $call_param . ', ' . $fields[$field]; $size_code .= "\t_LIBGTOP_SEND_len += "; - if (defined $sizeof_funcs->{$type}) { - $size_code .= $sizeof_funcs->{$type}->($fields[$field]); + if ($typeinfo->{$type}->[2]) { + $size_code .= sprintf ("sizeof (size_t)"); } else { $size_code .= sprintf ("sizeof (%s)", $c_type); } $size_code .= ";\n"; + if (defined $sizeof_funcs->{$type}) { + $size_code .= "\t_LIBGTOP_DATA_len += "; + $size_code .= $sizeof_funcs->{$type}->($fields[$field]); + $size_code .= ";\n"; + } + $marshal_code .= "\t_LIBGTOP_SEND_ptr = ". "(char *) _LIBGTOP_SEND_buf + _LIBGTOP_SEND_offset;\n"; @@ -146,8 +157,8 @@ sub output { } $marshal_code .= "\t_LIBGTOP_SEND_offset += "; - if (defined $sizeof_funcs->{$type}) { - $marshal_code .= $sizeof_funcs->{$type}->($fields[$field]); + if ($typeinfo->{$type}->[2]) { + $marshal_code .= sprintf ("sizeof (size_t)"); } else { $marshal_code .= sprintf ("sizeof (%s)", $c_type); } @@ -157,11 +168,12 @@ sub output { $local_var_code = ""; $local_var_code .= "\tunsigned _LIBGTOP_SEND_offset, _LIBGTOP_SEND_len;\n"; + $local_var_code .= "\tunsigned _LIBGTOP_DATA_len;\n"; if ($need_temp_len) { $local_var_code .= "\tunsigned _LIBGTOP_SEND_temp_len;\n"; } - $local_var_code .= "\tvoid *_LIBGTOP_SEND_buf;\n"; - $local_var_code .= "\tchar *_LIBGTOP_SEND_ptr;\n"; + $local_var_code .= "\tvoid *_LIBGTOP_SEND_buf, *_LIBGTOP_DATA_buf;\n"; + $local_var_code .= "\tchar *_LIBGTOP_SEND_ptr, *_LIBGTOP_DATA_ptr;\n"; if ($retval !~ /^void$/) { $local_var_code .= sprintf ("\t%s retval = (%s) 0;\n", $retval, $retval); @@ -169,19 +181,32 @@ sub output { $total_code = ''; - $send_buf_code = "\t_LIBGTOP_SEND_buf = "; + $send_buf_code = "\t/* send buffer */\n"; + $send_buf_code .= "\t_LIBGTOP_SEND_buf = "; if ($need_temp_storage) { $send_buf_code .= "glibtop_malloc_r (server, _LIBGTOP_SEND_len+1)"; } else { $send_buf_code .= '(void *) &'.$first_param_name; } + $send_buf_code .= ";\n\n"; + + $send_buf_code .= "\t/* data buffer */\n"; + $send_buf_code .= "\t_LIBGTOP_DATA_buf = "; + if ($need_temp_storage) { + $send_buf_code .= "glibtop_malloc_r (server, _LIBGTOP_DATA_len+1)"; + } else { + $send_buf_code .= 'NULL'; + } $send_buf_code .= ";\n"; + $send_buf_code .= "\t_LIBGTOP_DATA_ptr = _LIBGTOP_DATA_buf;\n"; $call_code = ''; $call_code .= sprintf ("\t%sglibtop_call_i (server, backend, GLIBTOP_CMND_%s,\n", $call_prefix, &toupper($feature)); $call_code .= sprintf ("\t\t\t%s%s, %s,\n", $call_prefix_space, "_LIBGTOP_SEND_len", "_LIBGTOP_SEND_buf"); + $call_code .= sprintf ("\t\t\t%s%s, %s,\n", $call_prefix_space, + "_LIBGTOP_DATA_len", "_LIBGTOP_DATA_buf"); if ($line_fields[3] eq '') { $call_code .= sprintf ("\t\t\t%s0, NULL,\n", $call_prefix_space); } elsif ($line_fields[3] eq 'array') { @@ -195,11 +220,16 @@ sub output { if ($need_temp_storage) { $send_buf_free_code = "\tglibtop_free_r (server, _LIBGTOP_SEND_buf);\n"; + $send_buf_free_code .= "\tglibtop_free_r (server, _LIBGTOP_DATA_buf);\n"; } else { $send_buf_free_code = ""; } - $total_code .= sprintf ("%s\n%s\n%s\n%s\n%s\n", + if (!($size_code eq '')) { + $size_code = sprintf (qq[\t/* send size */\n%s\n], $size_code); + } + + $total_code .= sprintf ("%s%s\n%s\n%s\n%s\n", $size_code, $send_buf_code, $marshal_code, $call_code, $send_buf_free_code); @@ -227,8 +257,9 @@ sub output { $feature, 'glibtop_'.$feature, $param_decl); } - $total_code = sprintf ("%s{\n%s\n%s\n%s}\n", $func_decl, - $local_var_code, $total_code); + $total_code = sprintf ("%s{\n%s\n%s\n%s\n%s}\n", $func_decl, + $local_var_code, $local_var_init_code, + $total_code); $total_code = sprintf ("#if GLIBTOP_SUID_%s\n\n%s\n#endif /* GLIBTOP_SUID_%s */\n\n", @@ -242,9 +273,16 @@ $call_vector_code = ''; for ($nr = 1; $nr <= $feature_count; $nr++) { $feature = $features{$nr}; - $call_vector_code .= sprintf - (qq[\#if GLIBTOP_SUID_%s\n\t_glibtop_get_%s_c,\n\#else\n\tNULL,\n\#endif\n], - &toupper($feature), $feature); + $orig = $feature; + $feature =~ s/^@//; + + if ($orig =~ /^@/) { + $call_vector_code .= sprintf (qq[\tNULL,\n]); + } else { + $call_vector_code .= sprintf + (qq[\#if GLIBTOP_SUID_%s\n\t_glibtop_get_%s_c,\n\#else\n\tNULL,\n\#endif\n], + &toupper($feature), $feature); + } } print 'glibtop_call_vector glibtop_backend_server_call_vector = {'; diff --git a/backends/server/server.c b/backends/server/server.c index 12e2ffaf..74edf71e 100644 --- a/backends/server/server.c +++ b/backends/server/server.c @@ -107,4 +107,44 @@ main(int argc, char *argv[]) void handle_slave_connection (int input, int output) { + glibtop_command _cmnd, *cmnd = &_cmnd; + char parameter [BUFSIZ]; + size_t send_size = 0; + + glibtop_send_version_i (glibtop_global_server, output); + + while (do_read (input, cmnd, sizeof (glibtop_command))) { + fprintf (stderr, "Slave %d received command " + "%d from client.\n", getpid (), cmnd->command); + + if (cmnd->send_size >= BUFSIZ) + glibtop_error ("Client sent %d bytes, but buffer is %d", + cmnd->send_size, BUFSIZ); + else if (cmnd->param_size >= BUFSIZ) + glibtop_error ("Client sent %d bytes, but buffer is %d", + cmnd->param_size, BUFSIZ); + + memset (parameter, 0, sizeof (parameter)); + + if (cmnd->send_size) { +#ifdef SLAVE_DEBUG + fprintf (stderr, "Client has %d bytes of data.\n", + cmnd->send_size); +#endif + + send_size = cmnd->send_size; + do_read (input, parameter, send_size); + } else if (cmnd->param_size) { +#ifdef SLAVE_DEBUG + fprintf (stderr, "Client has %d bytes of parameter data.\n", + cmnd->param_size); +#endif + send_size = cmnd->param_size; + memcpy (parameter, cmnd->parameter, send_size); + } + + glibtop_demarshal_func_i (glibtop_global_server, NULL, + cmnd->command, parameter, + send_size, NULL, 0, NULL); + } } diff --git a/backends/server/server.h b/backends/server/server.h index b71236cf..4a350597 100644 --- a/backends/server/server.h +++ b/backends/server/server.h @@ -33,8 +33,6 @@ #include #include #include -#include -#include #include #include @@ -42,13 +40,28 @@ #include #include +#include +#include + +#include "command.h" + BEGIN_LIBGTOP_DECLS +#ifndef DEBUG +#define DEBUG +#endif + +#ifndef SLAVE_DEBUG +#define SLAGE_DEBUG +#endif + /* Some don't have LOG_PERROR */ #ifndef LOG_PERROR #define LOG_PERROR 0 #endif +#define LIBGTOP_VERSION_STRING "Libgtop %s server version %s (%u,%u,%u,%u)." + #if defined(HAVE_GETDTABLESIZE) #define GET_MAX_FDS() getdtablesize() #else @@ -57,10 +70,14 @@ BEGIN_LIBGTOP_DECLS #define GET_MAX_FDS() 256 #endif -void handle_slave_connection (int input, int output); +void +handle_slave_connection (int input, int output); -void syslog_message (int priority, char *format, ...); -void syslog_io_message (int priority, char *format, ...); +int +glibtop_demarshal_func_i (glibtop *server, glibtop_backend *backend, unsigned command, const void *send_ptr, size_t send_size, void *data_ptr, size_t data_size, int *retval_ptr); + +void +glibtop_send_version_i (glibtop *server, int fd); extern int enable_debug; extern int verbose_output; diff --git a/backends/server/version.c b/backends/server/version.c new file mode 100644 index 00000000..9925016e --- /dev/null +++ b/backends/server/version.c @@ -0,0 +1,62 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */ + +/* $Id$ */ + +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by Martin Baulig , April 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include "server.h" + +void +glibtop_send_version_i (glibtop *server, int fd) +{ + char buffer [BUFSIZ]; + size_t size; + + sprintf (buffer, LIBGTOP_VERSION_STRING, + LIBGTOP_VERSION, LIBGTOP_SERVER_VERSION, + sizeof (glibtop_command), + 0, + sizeof (glibtop_union), + sizeof (glibtop_sysdeps)); + + size = strlen (buffer) + 1; + +#ifdef DEBUG + fprintf (stderr, "SERVER ID: |%s|\n", buffer); +#endif + + if (fd == 0) { + if (write (1, (const void *) &size, sizeof (size)) < 0) + glibtop_warn_io_r (server, "write"); + } else { + if (send (fd, (const void *) &size, sizeof (size), 0) < 0) + glibtop_warn_io_r (server, "send"); + } + + if (fd == 0) { + if (write (1, (const void *) buffer, size) < 0) + glibtop_warn_io_r (server, "write"); + } else { + if (send (fd, (const void *) buffer, size, 0) < 0) + glibtop_warn_io_r (server, "send"); + } +}