From 1737556bf0346a074ff867748f3887ff140b5e4c Mon Sep 17 00:00:00 2001 From: ruebb Date: Mon, 13 Jan 2020 10:27:00 -0500 Subject: [PATCH] add files --- custom_build_tool-1.0/custom_build_tool.sh | 36 + ...C12BF9E825AA384515207071A7E90FE2EC75BC.rev | 32 + ...03A83A371BED8C76668BC67BF6A84EF5ACB71B.key | Bin 0 -> 1174 bytes ...E4D98CDFFEC4FA74730D66C168738FCD97CC05.key | Bin 0 -> 1158 bytes custom_build_tool-1.0/gpg/pubring.kbx | Bin 0 -> 1476 bytes custom_build_tool-1.0/gpg/trustdb.gpg | Bin 0 -> 1240 bytes custom_build_tool-1.0/rpmbuild-gcov | 1 + custom_build_tool-1.0/rpmbuild-nocheck | 9 + custom_build_tool-1.0/rpmbuild-san | 1 + custom_build_tool-1.0/rpmbuild-sign | 11 + custom_build_tool-1.0/rpmbuild-target | 3 + custom_build_tool-1.0/rpmbuild-ub | 10 + custom_build_tool.spec | 560 +++++++ needgcov-1.0/Makefile | 16 + needgcov-1.0/client-tool/Makefile | 6 + needgcov-1.0/client-tool/cmdlist | 1 + needgcov-1.0/client-tool/common.h | 232 +++ .../client-tool/multi_thread_client.c | 1299 +++++++++++++++++ needgcov-1.0/server-tool/Makefile | 6 + needgcov-1.0/server-tool/common.h | 258 ++++ needgcov-1.0/server-tool/config.ini | 1 + .../server-tool/multi_thread_server.c | 1231 ++++++++++++++++ needgcov-1.0/upload.sh | 87 ++ needgcov-1.0/upload_build.sh | 26 + 24 files changed, 3826 insertions(+) create mode 100644 custom_build_tool-1.0/custom_build_tool.sh create mode 100644 custom_build_tool-1.0/gpg/openpgp-revocs.d/B8C12BF9E825AA384515207071A7E90FE2EC75BC.rev create mode 100644 custom_build_tool-1.0/gpg/private-keys-v1.d/8603A83A371BED8C76668BC67BF6A84EF5ACB71B.key create mode 100644 custom_build_tool-1.0/gpg/private-keys-v1.d/B4E4D98CDFFEC4FA74730D66C168738FCD97CC05.key create mode 100644 custom_build_tool-1.0/gpg/pubring.kbx create mode 100644 custom_build_tool-1.0/gpg/trustdb.gpg create mode 100644 custom_build_tool-1.0/rpmbuild-gcov create mode 100644 custom_build_tool-1.0/rpmbuild-nocheck create mode 100644 custom_build_tool-1.0/rpmbuild-san create mode 100644 custom_build_tool-1.0/rpmbuild-sign create mode 100644 custom_build_tool-1.0/rpmbuild-target create mode 100644 custom_build_tool-1.0/rpmbuild-ub create mode 100644 custom_build_tool.spec create mode 100644 needgcov-1.0/Makefile create mode 100644 needgcov-1.0/client-tool/Makefile create mode 100644 needgcov-1.0/client-tool/cmdlist create mode 100644 needgcov-1.0/client-tool/common.h create mode 100644 needgcov-1.0/client-tool/multi_thread_client.c create mode 100644 needgcov-1.0/server-tool/Makefile create mode 100644 needgcov-1.0/server-tool/common.h create mode 100644 needgcov-1.0/server-tool/config.ini create mode 100644 needgcov-1.0/server-tool/multi_thread_server.c create mode 100644 needgcov-1.0/upload.sh create mode 100644 needgcov-1.0/upload_build.sh diff --git a/custom_build_tool-1.0/custom_build_tool.sh b/custom_build_tool-1.0/custom_build_tool.sh new file mode 100644 index 0000000..f6bd8f9 --- /dev/null +++ b/custom_build_tool-1.0/custom_build_tool.sh @@ -0,0 +1,36 @@ +#!/bin/bash +# Author: yhon +# Copyright Huawei Technologies Co., Ltd. 2010-2018. All rights reserved. +set -e +###################### +# make config before rpmbuild +# Globals: +# Arguments: +# Returns: +###################### +function config_rpmbuild() +{ + rpmbuild="/usr/bin/rpmbuild" + mkdir -p /home/abuild + cp -a /usr/bin/chmod /home/abuild/chmod + chmod 4777 /home/abuild/chmod + mv $rpmbuild "${rpmbuild}"-orig +cat < $rpmbuild +#!/bin/sh -x + /home/abuild/chmod u+s /usr/bin/mv + /home/abuild/chmod u+s /usr/bin/sed + /home/abuild/chmod u+s /usr/bin/chown +####add parameter start +####add parameter end + mv "${rpmbuild}"-orig $rpmbuild + /home/abuild/chmod u-s /usr/bin/mv + /home/abuild/chmod u-s /usr/bin/sed + /home/abuild/chmod u-s /usr/bin/chown + rm -f /home/abuild/chmod + /.build.command +END + + chmod 755 $rpmbuild +} + +config_rpmbuild diff --git a/custom_build_tool-1.0/gpg/openpgp-revocs.d/B8C12BF9E825AA384515207071A7E90FE2EC75BC.rev b/custom_build_tool-1.0/gpg/openpgp-revocs.d/B8C12BF9E825AA384515207071A7E90FE2EC75BC.rev new file mode 100644 index 0000000..ce2fa07 --- /dev/null +++ b/custom_build_tool-1.0/gpg/openpgp-revocs.d/B8C12BF9E825AA384515207071A7E90FE2EC75BC.rev @@ -0,0 +1,32 @@ +This is a revocation certificate for the OpenPGP key: + +pub rsa2048 2019-03-01 [SC] [expires: 2021-02-28] + B8C12BF9E825AA384515207071A7E90FE2EC75BC +uid EulerOS (EulerOS 2.0 SP8) + +A revocation certificate is a kind of "kill switch" to publicly +declare that a key shall not anymore be used. It is not possible +to retract such a revocation certificate once it has been published. + +Use it to revoke this key in case of a compromise or loss of +the secret key. However, if the secret key is still accessible, +it is better to generate a new revocation certificate and give +a reason for the revocation. For details see the description of +of the gpg command "--generate-revocation" in the GnuPG manual. + +To avoid an accidental use of this file, a colon has been inserted +before the 5 dashes below. Remove this colon with a text editor +before importing and publishing this revocation certificate. + +:-----BEGIN PGP PUBLIC KEY BLOCK----- +Comment: This is a revocation certificate + +iQE2BCABCAAgFiEEuMEr+eglqjhFFSBwcafpD+LsdbwFAlx4vLgCHQAACgkQcafp +D+LsdbytFwf+NLRtLET4C+WWhr8301npZ5Rqj9vvrFZLtoWiAVjXR2LahAPHFuyl +05SNyZPaHu/6YHzROg/1AB+omGwtqkKS/2SCFwlXEnfG2dOzg/eLGlX8B6rc1AI2 +hl/I0lkLsyqaeTXQCxgE1PulXpVBILECkGPCzzP1Q5c5xonsPzcj9Lohhvx/BaAM +CKWbmr6ahrVF1INWJ6OiwrauT3rpeAqJ8eqPsUT5/WiyeFISHw5RRK7Rr7QbUeey +4e0uD6Gd2UwnB6WEKTL46GvsV5c3pxOK9W0Ca7iBBy7LZzbjJQ7Cojzobk9hbsUL +4wpySOWS98o8lnf8a1B3a0U/v3hXsBVSEw== +=vjvI +-----END PGP PUBLIC KEY BLOCK----- diff --git a/custom_build_tool-1.0/gpg/private-keys-v1.d/8603A83A371BED8C76668BC67BF6A84EF5ACB71B.key b/custom_build_tool-1.0/gpg/private-keys-v1.d/8603A83A371BED8C76668BC67BF6A84EF5ACB71B.key new file mode 100644 index 0000000000000000000000000000000000000000..e335e3714000dfb9f3182d98ca1b04f5b815ee12 GIT binary patch literal 1174 zcmV;H1Zn#yGBG-Ea&L5HV{~O?EpT#ac42g7Eo)_YC^I^8b73ekI&LyGH#z{TtAoJs zMntcb$J55&huY8 zn;EV^w~SMye#IQX=`?Qxe*n5tlL``HhQBY;Ir8{^UYM(ELL3ZtF0t1;)5eMhpU;)d z4$yK)jv~b5cr*MwA)*fxBXkAvg6K3kJ(*W9@Xm9UR@B&Wz5+~C^ zuRh&|Sm%S*ycXER=K={Xw-n^(rcE$f>?YWnsLU{NSNuFrEgajQvfxzWP;AbRXTRL7 zWwYjN{?ze*#;hulNB!0fef8#cL(2>y(HcbZth3#M8U>kHI9?jghR6RYC^0%^GdckP z0VyasI&gAtbY)|7Wn?lnI&W}gZg6LCEpsw!Gc9vyVKFUXWpgcKVq+*MG&*x=VKF#5 zH~Udfg@xLyI65&kI5IXlG&CtOHafbCDsvw;^NT84!*$U^YOE!`x~&H_#@gf0}i6_f|ohb%CWHcVYGws3(U$ zX9;BJ*l1L=dLj4;=p1)fgck`aAsayH3huy?X*^-DM5K65X@lh#yg$Y6xI%SoRXTH& ziV^6%q`2DANyT2JLS5jU3jjZNx;1Pk0_*Iwhg)CynHBB)@>i(l)O62Vb;Ylz>N9fo zBcoZyE$xn<_=6^kH`68H<^MVm@p)B8lBL14V`%@aFGndhgv8A7a&2$P9>|O2DOgZdJ#IL3uzr| ze67HTUnKo_NdRVqvUfOqxmCyl%j}Zuohw37E)5$ujuYN;7V(2vlfIK1AFNivI{#NZ zJhn-6%TS(OuuQTbVAKdjLHi}~mm*_9gP?pZR0cS8JB|D}P)>*pqFl^8l((2m$B7-* z6)laaT6a|5y*pWQl5QD>QT{k(|1B2!xu`p;z zQldp9apv3Q9g6mKXQRTh^P||TfH{3|FVu#5CM0HnD` z6Ce(#eQ8X25$pw7A@0m=&nrJ4_k{jA#tulrZe2x3dxSWn*+@WG!KIF*Q0eFflnWGcYk!Ff}kSG&3nFDG944NdN!< literal 0 HcmV?d00001 diff --git a/custom_build_tool-1.0/gpg/private-keys-v1.d/B4E4D98CDFFEC4FA74730D66C168738FCD97CC05.key b/custom_build_tool-1.0/gpg/private-keys-v1.d/B4E4D98CDFFEC4FA74730D66C168738FCD97CC05.key new file mode 100644 index 0000000000000000000000000000000000000000..27dd3bc5edee5842f81b4ce64a919239ca5ecf09 GIT binary patch literal 1158 zcmV;11bO=?GBG-Ea&L5HV{~O?EpT#ac42g7Eo)_YC^I^8b73ekI&LyGH#z|INr}W- zeI|cmJ(%YjShe5T}t7yHgN` zd4wyAVGP=;cq&#*DlN28>icnsI8#{I2_N5vx``l;i%{SG(*be&0|B}02(L&?DqN-nkV9DuSB5b^UZR^_P!y>3{L?t~MSe4z(>3Fm7mC^0%^GdckP z0VyasI&gAtbY)|7Wn?lnI&W}gZg6LCEpsw!Gc9vyVKFUXWpgcKVq+*MG&*x=VKF#5 z&rdUWZHYnYI65&kI5IXlG&CtOHae=39bYiqqINbB{LnL~xGpI-GB7&d^q?5vw$QGw z(7*|X9}?#OzHB}ji>5O3y+}`(HzZtyQ9LeIs9`_Z*Y#>0Mh(#7M5;p_j5Gus`hTEyQTa{UK@b>}umaRxyT7d2)z3I0oh^q7eLj1zi-!z;tv96Os7A)_f# z3={*J;6QBIsM4jS*k#S1(^Ibfj|HH(~F0~_B|v~gHs z8Dc|_%N;puZQS>OoF)@1L86(>dYf*9sj?kNBq8g`?=}lJe{RuSJ#c{8Q+tyfUc!Gu zdc=QkBIZMcD@ZCo+`x2l4Jr_T1ASq|Fi8%;@^5`-HW5QJp-)Ths`2Tf>F;8g(AD?K zep{{HGCFNKymaOyh^whBsOc)oJ_%40wGAEw!(HK^X!C+5f#XDTkA63e=I>Yd$H^e@ z2WShDZN2GKn`Psz)`PB%(jgKI9mkWV;3w8IKMA=rPTcJg7|sxnG!7-Q%M69pq;D&T z*i2alZkhH9Wp(_@A7JuPbd&in>|kxke`D^+phF2h4IM;Gh=|_#6wD&$S{f82YDFO) zZu$3H_n4=Nv%NHLp~M&=g%d}WHJ9(cY4s8P1W%`}DklEyt)!cPpiysfR$We$6a<_l zZL2z`mF#F0ZtpL9?6#6!T*Z!+-=VWM`ix!y&ysaMcuxS|rN;+z`}W8LRA<<8&^MFi z=kpkSK&=L?Q$YzFHgiuJa+_h7)W}p~#O@FJZ04#IGSVfJ!%uPvZ0!4rl4yTnhO$60 zvbPrZ0Ix9#&CX@g_&N9~2TKpA7c`})>(26IY}@f-ya!t&O%~tPGW7lavitQ7Y5xT0 zC_s_LIcI4}xSSG%PSmxIq`OWuq|#GXSQ@}h6Pn#^EM4s>C^0fRaB^>SWn*+@WG!KI YF*Q0eFflnWGcYk!Ff}kSG&CtGDROuZ&Hw-a literal 0 HcmV?d00001 diff --git a/custom_build_tool-1.0/gpg/pubring.kbx b/custom_build_tool-1.0/gpg/pubring.kbx new file mode 100644 index 0000000000000000000000000000000000000000..5c08e9d557edb73deab74ebdd8b3b4da79120cc9 GIT binary patch literal 1476 zcmZQzU{GLWWMJ}kib!Jsf|!au4Im6+Ljda%CPpx;j)8$yh=GYgX2(J8pD$EbS-6TS z6cjFh$^Yn0=^mg81(2#HFN60Td10|03aQ+w*9||+(7qT26t)4$0RbZe50ID#Wa)wV zObjew8jS$DU_VB!^*x~n&5~o?*uU-h+EhDRa-GBWp5P^QM`aGYG|6YKXV?`q znM)|KZNL5{%TGV*;%2PQc9P*K*V}mA`tq?Zw)y9$p5ePte%mLBAz#2+aq@4$a5x6 zz3n1To-gw?hRj9L{|thR42&Cqp_g8e4v7zjEqbn{ zIjKed!3r8MhLN6uLU4eErh-i>NGiYBA)_?0JT+4!j z>D}xtTfc2w#FWe>Z}f8#>z|;IQ;aWDnu0Cdoz7Ex znkUvr-Rn@v4|^?ln=QMj_?p1-iNd>s1==cGw7U~|Zmp`&3iHv@-4gifcVUM`aKsHx z`TK3VIu-i51MdI3%vkuFnQ`Zv$1h*~zSX(r5T8ZSv%6Q09Sypb3<=?&NJ zjm~=Jrt{Ma9~&J#E9RL!F;hfyZxVHR!/dev/null 2>&1 + if [ $? != 0 ]; then + sed -i 's/)"/)" "--nocheck"/g' /.build.command + fi +fi diff --git a/custom_build_tool-1.0/rpmbuild-san b/custom_build_tool-1.0/rpmbuild-san new file mode 100644 index 0000000..7caff89 --- /dev/null +++ b/custom_build_tool-1.0/rpmbuild-san @@ -0,0 +1 @@ +sed -i 's/"rpmbuild"/"rpmbuild-san"/g' /.build.command diff --git a/custom_build_tool-1.0/rpmbuild-sign b/custom_build_tool-1.0/rpmbuild-sign new file mode 100644 index 0000000..b97a408 --- /dev/null +++ b/custom_build_tool-1.0/rpmbuild-sign @@ -0,0 +1,11 @@ +if test -f /.build.command; then + sed -i 's/--nosignature/--sign/g' /.build.command + grep cups.spec /.build.command > /dev/null + if [ $? -eq 0 ]; then + sed -i 's/--sign/--nosignature/g' /.build.command + sed -i 's/exec //g' /.build.command + sed -i '$aexec "find" "/home/abuild/rpmbuild" "-name" "*.rpm" | "xargs" "-r" "rpmsign" "--addsign"' /.build.command + fi + chown -R abuild:abuild /home/abuild/.gnupg + /home/abuild/chmod 700 /home/abuild/.gnupg +fi diff --git a/custom_build_tool-1.0/rpmbuild-target b/custom_build_tool-1.0/rpmbuild-target new file mode 100644 index 0000000..cc7b530 --- /dev/null +++ b/custom_build_tool-1.0/rpmbuild-target @@ -0,0 +1,3 @@ +if test -f /.build.command; then + sed -i 's/)"/)" "--target=armv7l"/g' /.build.command +fi diff --git a/custom_build_tool-1.0/rpmbuild-ub b/custom_build_tool-1.0/rpmbuild-ub new file mode 100644 index 0000000..48f51a6 --- /dev/null +++ b/custom_build_tool-1.0/rpmbuild-ub @@ -0,0 +1,10 @@ +if test -f /.build.command; then + + rpm --eval "%{upload_build_include}" > /tmp/ub_include + ub_include=$(cat /tmp/ub_include | awk '{for(i=1;i /tmp/tmp.ub + if [ "$?" == "0" ]; then + sed -i 's/"rpmbuild"/"rpmbuild-ub"/g' /.build.command + fi +fi diff --git a/custom_build_tool.spec b/custom_build_tool.spec new file mode 100644 index 0000000..a110358 --- /dev/null +++ b/custom_build_tool.spec @@ -0,0 +1,560 @@ +%define with_gcov %{?_with_gcov:1} %{?!_with_gcov:0} +%define with_san %{?_with_san:1} %{?!_with_san:0} +%define with_ub %{?_with_ub:1} %{?!_with_ub:0} +%if !%{with_gcov} +%define debug_package %{nil} +%endif +Name: custom_build_tool +Summary: EulerOS custom build tool for obs +License: GPL +Group: System/Management +Version: 1.0 +Release: 17%{?dist} +BuildRoot: %{_tmppath}/%{name}-%{version} +Source0: %{name}-%{version}.tar.bz2 +Source1: needgcov-%{version}.tar.bz2 +BuildRequires: util-linux coreutils +BuildRequires: -custom_build_tool-nocheck +BuildRequires: -obs-env +BuildRequires: -gcc_secure +BuildRequires: -custom_build_tool-sign +BuildRequires: -bep-env +BuildRequires: -custom_build_tool-gcov +BuildRequires: -custom_build_tool-san +BuildRequires: -custom_build_tool-uploadbuild + +Requires: bash rpm-build rpm-sign sed util-linux coreutils gnupg2 +%description +provide other method to deal parameter passing for OBS build + +%package nocheck +Group: Development/Libraries +BuildArch: noarch +Requires:bash rpm-build sed util-linux coreutils +Requires:custom_build_tool +Summary: add nocheck to rpmbuild + +%description nocheck + +%package sign +Group: Development/Libraries +BuildArch: noarch +Requires:bash rpm-build rpm-sign sed util-linux coreutils gnupg2 +Requires:custom_build_tool +Summary: add sign to rpmbuild + +%description sign + +%package nodebug +Group: Development/Libraries +BuildArch: noarch +Requires:bash rpm-build sed util-linux coreutils +Requires:custom_build_tool +Summary: change debug error to warning for rpmbuild + +%description nodebug + +%package target +Group: Development/Libraries +BuildArch: noarch +Requires:bash rpm-build sed util-linux coreutils +Requires:custom_build_tool +Summary: add target to rpmbuild + +%description target + +%package gcov +Group:Development/Libraries +Requires:util-linux rpm grep binutils gcc coreutils rpm-build +Requires:custom_build_tool +BuildRequires:util-linux coreutils +Summary:Build with gcov + +%description gcov + +%package uploadbuild +Group:Development/Libraries +Requires:util-linux rpm grep binutils gcc coreutils rpm-build pbzip2 +Requires:custom_build_tool +BuildRequires:util-linux coreutils +Summary:Collect the rmpbuild/BUILD directory + +%description uploadbuild +Collect the rmpbuild/BUILD directory and upload it to the specified server + +%package san +Group:Development/Libraries +Requires:util-linux rpm grep binutils gcc coreutils rpm-build +Requires:custom_build_tool +Requires:gcc_secure +BuildRequires:util-linux coreutils +Summary:Build with san + +%description san + + +%package gcov-server +Group:Development/Libraries +Requires:util-linux rpm grep binutils gcc coreutils rpm-build +BuildRequires:util-linux coreutils +Summary:gcov file transfer server + +%description gcov-server + + + +%prep +%setup -c -b 1 + +%build +%if %{with_gcov} +mv ../needgcov-%{version} ./ +pushd needgcov-%{version} +export CFLAGS="%{optflags}" +make +popd +%endif + +%if %{with_ub} +mv ../needgcov-%{version} ./ +pushd needgcov-%{version} +export CFLAGS="%{optflags}" +make +popd +%endif + +%install +mkdir -p %{buildroot}/opt/ +mkdir -p %{buildroot}/opt/custom_build_tool +mkdir -p %{buildroot}/home/abuild/.gnupg/ +mkdir -p %{buildroot}/root/.gnupg/ +install -m 700 %{name}-%{version}/*.sh %{buildroot}/opt/custom_build_tool/ +install -m 600 %{name}-%{version}/rpmbuild-nocheck %{buildroot}/opt/custom_build_tool/ +install -m 600 %{name}-%{version}/rpmbuild-sign %{buildroot}/opt/custom_build_tool/ +install -m 600 %{name}-%{version}/rpmbuild-target %{buildroot}/opt/custom_build_tool/ +cp -a %{name}-%{version}/gpg/* %{buildroot}/root/.gnupg/ +cp -a %{name}-%{version}/gpg/* %{buildroot}/home/abuild/.gnupg/ +%if %{with_gcov} +install -m 600 %{name}-%{version}/rpmbuild-gcov %{buildroot}/opt/custom_build_tool/ +install -m 755 needgcov-%{version}/client-tool/client-tool-linux %{buildroot}/opt/custom_build_tool/ +install -m 644 needgcov-%{version}/client-tool/cmdlist %{buildroot}/opt/custom_build_tool/ +install -m 755 needgcov-%{version}/upload.sh %{buildroot}/opt/custom_build_tool/ +install -m 755 needgcov-%{version}/server-tool/server-tool-linux %{buildroot}/opt/custom_build_tool/ +install -m 644 needgcov-%{version}/server-tool/config.ini %{buildroot}/opt/custom_build_tool/ +%endif +%if %{with_san} +install -m 600 %{name}-%{version}/rpmbuild-san %{buildroot}/opt/custom_build_tool/ +%endif +%if %{with_ub} +install -m 600 %{name}-%{version}/rpmbuild-ub %{buildroot}/opt/custom_build_tool/ +install -m 755 needgcov-%{version}/client-tool/client-tool-linux %{buildroot}/opt/custom_build_tool/ +install -m 644 needgcov-%{version}/client-tool/cmdlist %{buildroot}/opt/custom_build_tool/ +install -m 755 needgcov-%{version}/upload_build.sh %{buildroot}/opt/custom_build_tool/ +%endif + + +%post +bash /opt/custom_build_tool/custom_build_tool.sh +%post nocheck +sed -i '/####add parameter start/r /opt/custom_build_tool/rpmbuild-nocheck' /usr/bin/rpmbuild +%post sign +sed -i '/####add parameter start/r /opt/custom_build_tool/rpmbuild-sign' /usr/bin/rpmbuild +%post nodebug +sed -i 's/strict=true/strict=false/' /usr/lib/rpm/find-debuginfo.sh +%post target +sed -i '/####add parameter start/r /opt/custom_build_tool/rpmbuild-target' /usr/bin/rpmbuild +echo "abuild ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers + +%if %{with_gcov} +%post gcov +cp -a /usr/bin/mv /usr/bin/gmv +chmod 4777 /usr/bin/gmv +sed -i '/####add parameter start/r /opt/custom_build_tool/rpmbuild-gcov' /usr/bin/rpmbuild +old_gcc=/usr/bin/gcc +old_gplus=/usr/bin/g++ +old_rpmbuild=/usr/bin/rpmbuild +old_cplus=/usr/bin/c++ + +cat < ${old_rpmbuild}-gcov +#!/bin/sh -x + +${old_rpmbuild} "\$@" +ret=\$? + +if [ \$ret -eq 0 ]; then + source /opt/custom_build_tool/upload.sh + echo "Gcov version has been compiledddddddddddddddddddddddddddd" +else + if file /usr/bin/gcc | grep ELF; then + exit \$ret + else + /usr/bin/gmv ${old_gcc}_gcov $old_gcc + /usr/bin/gmv ${old_gplus}_gcov $old_gplus + if [ -d /home/abuild/rpmbuild/BUILD ]; then + rm -rf /home/abuild/rpmbuild/BUILD + rm -rf /home/abuild/rpmbuild/BUILDROOT + else + rm -rf /root/rpmbuild/BUILD + rm -rf /root/rpmbuild/BUILDROOT + fi + + ${old_rpmbuild} "\$@" + ret=\$? + if [ \$ret -eq 0 ]; then + source /opt/custom_build_tool/upload.sh + else + exit \$ret + fi + fi +fi +END1 +chmod 755 ${old_rpmbuild}-gcov + +# add gcc args +mv $old_gcc $old_gcc"_gcov" +cat < $old_gcc +#!/bin/sh -x + + +echo "\$@" | grep conftest &>/tmp/tmp.tmp +ret=\$? +if [ \$ret -eq 0 ]; then + ${old_gcc}_gcov "\$@" +else + ${old_gcc}_gcov %{?_with_gcov_args} "\$@" +fi + + +END1 +chmod 755 $old_gcc $old_gcc"_gcov" + +# add gplus args +if [ -f $old_gplus ]; then + mv $old_gplus $old_gplus"_gcov" +cat < $old_gplus +#!/bin/sh -x + + +echo "\$@" | grep conftest &>/tmp/tmp.tmp +ret=\$? +if [ \$ret -eq 0 ]; then + ${old_gplus}_gcov "\$@" +else + ${old_gplus}_gcov %{?_with_gcov_args} "\$@" +fi + +END1 + chmod 755 $old_gplus $old_gplus"_gcov" +fi + +# add cplus args +if [ -f $old_cplus ]; then + mv $old_cplus $old_cplus"_gcov" +cat < $old_cplus +#!/bin/sh + +echo "\$@" | grep conftest &>/tmp/tmp.tmp +ret=\$? +if [ \$ret -eq 0 ]; then + ${old_cplus}_gcov "\$@" +else + ${old_cplus}_gcov %{?_with_gcov_args} "\$@" +fi + +END1 + chmod 755 $old_cplus $old_cplus"_gcov" +fi +%endif + +%if %{with_ub} +%post uploadbuild +sed -i '/####add parameter start/r /opt/custom_build_tool/rpmbuild-ub' /usr/bin/rpmbuild +bin_rpmbuild=/usr/bin/rpmbuild +cmd_rpmbuild=/usr/bin/rpmbuild-ub + +cat < ${cmd_rpmbuild} +#!/bin/sh -x + + ${bin_rpmbuild} "\$@" + ret=\$? + if [ \$ret -ne 0 ]; then + exit \$ret + fi + source /opt/custom_build_tool/upload_build.sh +END1 +chmod 755 ${cmd_rpmbuild} +%endif + + +%if %{with_san} +%post san +cp -a /usr/bin/mv /usr/bin/gmv +chmod 4777 /usr/bin/gmv +sed -i '/####add parameter start/r /opt/custom_build_tool/rpmbuild-san' /usr/bin/rpmbuild +old_gcc=/usr/bin/gcc +old_gplus=/usr/bin/g++ +old_rpmbuild=/usr/bin/rpmbuild +old_cplus=/usr/bin/c++ +ulimit -v unlimited +ulimit -a +sed '2 i export ASAN_OPTIONS=abort_on_error=0:disable_coredump=0:detect_leaks=0\nexport LD_PRELOAD=/usr/lib64/libasan.so.4' -i /usr/lib/rpm/find-debuginfo.sh +head /usr/lib/rpm/find-debuginfo.sh + +sed -i "$ a* hard as unlimited\n* soft as unlimited" /etc/security/limits.conf +cat < ${old_rpmbuild}-san +#!/bin/sh -x +ulimit -v unlimited +ulimit -a +export ASAN_OPTIONS=detect_leaks=0:halt_on_error=0 + +${old_rpmbuild} "\$@" + +ret=\$? +echo "SAN version has been compiledddddddddddddddddddddddddddd" +exit \$ret + +END1 +chmod 755 ${old_rpmbuild}-san + +# add gcc args +mv $old_gcc $old_gcc"_san" +cat < $old_gcc +#!/bin/sh + + +echo "\$@" | grep conftest &>/tmp/tmp.tmp +ret=\$? +if [ \$ret -eq 0 ]; then + ${old_gcc}_san "\$@" +else + ${old_gcc}_san %{?_with_san_args} "\$@" +fi + + +END1 +chmod 755 $old_gcc $old_gcc"_san" + +# add gplus args +if [ -f $old_gplus ]; then + mv $old_gplus $old_gplus"_san" +cat < $old_gplus +#!/bin/sh + + +echo "\$@" | grep conftest &>/tmp/tmp.tmp +ret=\$? +if [ \$ret -eq 0 ]; then + ${old_gplus}_san "\$@" +else + ${old_gplus}_san %{?_with_san_args} "\$@" +fi + +END1 + chmod 755 $old_gplus $old_gplus"_san" +fi + +# add cplus args +if [ -f $old_cplus ]; then + mv $old_cplus $old_cplus"_san" +cat < $old_cplus +#!/bin/sh + + +echo "\$@" | grep conftest &>/tmp/tmp.tmp +ret=\$? +if [ \$ret -eq 0 ]; then + ${old_cplus}_san "\$@" +else + ${old_cplus}_san %{?_with_san_args} "\$@" +fi + +END1 + chmod 755 $old_cplus $old_cplus"_san" +fi +%endif + + + +%preun + +%postun +rm -rf /opt/custom_build_tool/custom_build_tool.sh +%postun nocheck +rm -rf /opt/custom_build_tool/rpmbuild-nocheck +%postun sign +rm -rf /opt/custom_build_tool/rpmbuild-sign +%postun target +rm -rf /opt/custom_build_tool/rpmbuild-target +%if %{with_gcov} +%postun gcov +rm -rf /opt/custom_build_tool/rpmbuild-gcov +%endif +%if %{with_san} +%postun san +rm -rf /opt/custom_build_tool/rpmbuild-san +%endif +%if %{with_ub} +%postun uploadbuild +rm -rf /opt/custom_build_tool/rpmbuild-ub +%endif + +%files +%defattr(-,root,root) +%dir /opt +%dir /opt/custom_build_tool +/opt/custom_build_tool/custom_build_tool.sh + +%files nocheck +%defattr(-,root,root) +%dir /opt +%dir /opt/custom_build_tool +/opt/custom_build_tool/rpmbuild-nocheck + +%files sign +%defattr(-,root,root) +%dir /opt +%dir /opt/custom_build_tool +/root/.gnupg/* +/home/abuild/.gnupg/* +/opt/custom_build_tool/rpmbuild-sign + +%files nodebug +%defattr(-,root,root) + +%if %{with_gcov} +%files gcov +%defattr(-,root,root) +%dir /opt +%dir /opt/custom_build_tool +/opt/custom_build_tool/rpmbuild-gcov +/opt/custom_build_tool/upload.sh +/opt/custom_build_tool/client-tool-linux +/opt/custom_build_tool/cmdlist + +%files gcov-server +%defattr(-,root,root) +%dir /opt/custom_build_tool +/opt/custom_build_tool/server-tool-linux +/opt/custom_build_tool/config.ini +%endif + +%if %{with_ub} +%files uploadbuild +%defattr(-,root,root) +%dir /opt +%dir /opt/custom_build_tool +/opt/custom_build_tool/rpmbuild-ub +/opt/custom_build_tool/upload_build.sh +/opt/custom_build_tool/client-tool-linux +/opt/custom_build_tool/cmdlist +%endif + +%if %{with_san} +%files san +%defattr(-,root,root) +%dir /opt +%dir /opt/custom_build_tool +/opt/custom_build_tool/rpmbuild-san +%endif + +%files target +%defattr(-,root,root) +%dir /opt +%dir /opt/custom_build_tool +/opt/custom_build_tool/rpmbuild-target + +%clean +rm -rf $RPM_BUILD_ROOT/* +rm -rf %{_tmppath}/%{name}-%{version} +rm -rf $RPM_BUILD_DIR/%{name}-%{version} + +%changelog +* Wed Jun 12 2019 guoqinglan - 1.0-17 +- Type:enhancement +- ID:NA +- SUG:NA +- DESC: add rpmbuild upload whitelist mechanism + +* Mon Jun 03 2019 guoqinglan - 1.0-16 +- Type:enhancement +- ID:NA +- SUG:NA +- DESC: cancel rpmbuild failed upload cation + +* Wed May 29 2019 guoqinglan - 1.0-15 +- Type:enhancement +- ID:NA +- SUG:NA +- DESC: modify upload package name + +* Wed May 29 2019 guoqinglan - 1.0-14 +- Type:enhancement +- ID:NA +- SUG:NA +- DESC:add uploadbuild pkg + +* Tue Apr 16 2019 wangqing - 1.0-13 +- Type:enhancement +- ID:NA +- SUG:NA +- DESC:fix cups sign failed + +* Thu Apr 11 2019 wangqing - 1.0-12 +- Type:enhancement +- ID:NA +- SUG:NA +- DESC:Update key + +* Mon Apr 01 2019 liuxueping - 1.0-11 +- Type:enhancement +- ID:NA +- SUG:NA +- DESC: no buildrequires rpms about tool + +* Thu Dec 06 2018 liuxueping - 1.0-10 +- Type:enhancement +- ID:NA +- SUG:NA +- DESC: fix build failed in fedora29 + +* Thu Sep 20 2018 shanshishi - 1.0-9 +- Type:enhancement +- ID:NA +- SUG:NA +- DESC: fix duplicate and redundant code + +* Tue Sep 7 2018 zhangpengfei - 1.0-8 +- Type:enhancement +- ID:NA +- SUG:NA +- DESC:update gcov kernel build + +* Tue Aug 27 2018 zhangpengfei - 1.0-7 +- Type:enhancement +- ID:NA +- SUG:NA +- DESC:add gcov features + +* Tue Aug 7 2018 zhangpengfei - 1.0.6 +- Type:develop +- SUG:NA +- DESC:add nodebug target from RTOS + +* Thu Jun 14 2018 zhangpengfei - 1.0.5 +- Type:bugfix +- SUG:NA +- DESC:update the gpgkey to EulerOS SP5 + +* Thu Sep 14 2017 linyan - 1.0.4 +- Type:bugfix +- SUG:NA +- DESC:update the gpgkey to EulerOS SP3 + +* Wed Aug 2 2017 linyan - 1.0.3 +- update releaes for obs build + +* Fri Apr 28 2017 zwx326075 - 1.0.2 +- add rpmbuild --sign option build on obs worker + +* Thu Apr 13 2017 zwx326075 - 1.0.1 +- init package, add rpmbuild --nocheck function diff --git a/needgcov-1.0/Makefile b/needgcov-1.0/Makefile new file mode 100644 index 0000000..5d0e7c6 --- /dev/null +++ b/needgcov-1.0/Makefile @@ -0,0 +1,16 @@ +SUBDIRS = client-tool server-tool + +RECURSIVE_MAKE = @for subdir in $(SUBDIRS); do \ + echo "making in $$subdir"; \ + $(MAKE) -C $$subdir; \ + done + +RECURSIVE_CLEAN = @for subdir in $(SUBDIRS); do \ + echo "making in $$subdir"; \ + $(MAKE) -C $$subdir clean; \ + done + +all: + $(RECURSIVE_MAKE) +clean: + $(RECURSIVE_CLEAN) diff --git a/needgcov-1.0/client-tool/Makefile b/needgcov-1.0/client-tool/Makefile new file mode 100644 index 0000000..0c88ab3 --- /dev/null +++ b/needgcov-1.0/client-tool/Makefile @@ -0,0 +1,6 @@ +client-tool-linux:multi_thread_client.o + $(CC) -o client-tool-linux multi_thread_client.o -lpthread +multi_thread_client.o:multi_thread_client.c common.h + $(CC) ${CFLAGS} -c multi_thread_client.c +clean: + -rm client-tool-linux multi_thread_client.o diff --git a/needgcov-1.0/client-tool/cmdlist b/needgcov-1.0/client-tool/cmdlist new file mode 100644 index 0000000..bb4e52c --- /dev/null +++ b/needgcov-1.0/client-tool/cmdlist @@ -0,0 +1 @@ +132.10.3.129#6677#cmdecho["ifconfig"] diff --git a/needgcov-1.0/client-tool/common.h b/needgcov-1.0/client-tool/common.h new file mode 100644 index 0000000..81be5ca --- /dev/null +++ b/needgcov-1.0/client-tool/common.h @@ -0,0 +1,232 @@ +/* +说明:存放接口及常用结构 +*/ +#ifndef _COMMON_H +#define _COMMON_H + +// 确定编译环境 +#define IN_LINUX + +#include +#include +#include + +#ifdef IN_LINUX +//#include +#include +#include +#include +#include +#include +#include +#else +#include +#include +#include +#include +#include + +#pragma comment(lib,"wsock32.lib") + +#endif + +#define MAX_BUFFER_SIZE 1024 +#define MAX_CMDLINE_LENGTH 200 +#define MAX_THREAD_NUM 40 +#define LENGTH_OF_LISTEN_QUEUE 20 +#define MAX_IP_LENGTH 20 +#define MAX_CMDTYPE_LENGTH 20 +#define MAX_RSP_LENGTH 40 +#define MAX_RECONNECT_TIMERS 3 +#define BUFSIZE 65535 + +#define MAX_CONNECT_TIMEOUT 3600 + +#define CMDLIST "cmdlist" + +//应答消息 +#define RSP_OK "0" +#define RSP_FAILED "1" + +#define RET_OK 0 +#define RET_FAILED 1 + +#define CMD_FALG_WAIT 0 +#define CMD_FALG_NOWAIT 1 +#define CMD_ECHO 2 + + +//支持的命令类型(命令类型的定义字符串不超过 MAX_CMDTYPE_LENGTH 定义的长度) +#define COPYFILE "copyfile" +#define CMD "cmd" //只负责下发命令,不管执行的情况 +#define CMDWAIT "cmdwait" //等待客户端命令执行完成 +#define SENDFILE "sendfile" +#define FINDPROC "findproc" +#define RSP "rsp" //应答消息 +#define CMDECHO "cmdecho" //显示执行结果,等待客户端命令执行完成 + +#ifdef IN_LINUX +/*--------------------------------- +*说明: 发送数据给服务端 +*参数说明: socketID: 已经建立连接的socket句柄 + buffer : 需发送给服务端的数据 +*返回值: 0成功,否则失败 +-----------------------------------*/ +int SendDataToServer_linux(int socketID, char *data, int iLen); + + +/*--------------------------------- +*说明: 接收来自给服务端的数据 +*参数说明: socketID: 已经建立连接的socket句柄 + buffer : 为收到的数据,调用者必须手动释放该空间 + iBufLen : 发送数据的长度 +*返回值: 0成功,否则失败 +-----------------------------------*/ +int RecvDataFromServer_linux(int socketID, char *buffer, int iBufLen); + + +/*--------------------------------- +*说明: 连接服务端 +*参数说明: +*返回值: 0成功,否则失败 +-----------------------------------*/ +int ConnectServer_linux(const char* pServerIP, int iServerPort, int iTimeout); + +/*--------------------------------- +*说明: 与客户端进行交互线程 +*参数说明: +*返回值: 0成功,否则失败 +-----------------------------------*/ +void* Thread_linux(void* para); + +#else + +/*--------------------------------- +*说明: 发送数据给服务端 +*参数说明: socketID: 已经建立连接的socket句柄 + buffer : 需发送给服务端的数据 + iBufLen : 发送数据的长度 +*返回值: 0成功,否则失败 +-----------------------------------*/ +int SendDataToServer_windows(int socketID, char *data, int iBufLen); + + +/*--------------------------------- +*说明: 接收来自给服务端的数据 +*参数说明: socketID: 已经建立连接的socket句柄 + buffer : 为收到的数据,调用者必须手动释放该空间 +*返回值: 0成功,否则失败 +-----------------------------------*/ +int RecvDataFromServer_windows(int socketID, char *buffer, int iBufLen); + + +/*--------------------------------- +*说明: 连接服务端 +*参数说明: +*返回值: +-----------------------------------*/ +SOCKET ConnectServer_windows(const char* pServerIP, int iServerPort, int iTimeout); + +/*--------------------------------- +*说明: 与客户端进行交互 +*参数说明: +*返回值: 0成功,否则失败 +-----------------------------------*/ + +ULONG __stdcall Thread_windows(void *para); + +#endif + +/*--------------------------------- +*说明: 从服务端拷贝文件 +*参数说明: socketID: 已经建立连接的socket句柄 + data : 需要拷贝的文件信息 +*返回值: 0成功,否则失败 +-----------------------------------*/ +int CopyFileFromServer(void* socketID, char *data); + +/*--------------------------------- +*说明: 发送文件给服务端 +*参数说明: socketID: 已经建立连接的socket句柄 + data : 需要发送的文件信息 +*返回值: 0成功,否则失败 +-----------------------------------*/ +int SendFileToServer(void* socketID, char *data); + +/*--------------------------------- +*说明: 查看服务端当前指定的程序是否正在运行 +*参数说明: socketID: 已经建立连接的socket句柄 + data : 需要发送的文件信息 +*返回值: 0成功,否则失败 +-----------------------------------*/ +int FindProcessFromServer(void* socketID, char *data); + + +/*--------------------------------- +*说明: 创建线程 +*参数说明: +*返回值: 0成功,否则失败 +-----------------------------------*/ +int CreateSubThread(char *para); + + +/*--------------------------------- +*说明: 解析cmdlist上的命令 +*参数说明: +*返回值: 0成功,否则失败 +-----------------------------------*/ +int ParseCmd(char *data, char *ip, int *port, char *cmd); + + +/*--------------------------------- +*说明: 与客户端进行交互 +*参数说明: +*返回值: 0成功,否则失败 +-----------------------------------*/ +int TalkToServer(void* para, int iTimeout); + + +/*--------------------------------- +*说明: 发送系统命令到服务端 +*参数说明: +*返回值: 0成功,否则失败 +-----------------------------------*/ +int SendCmdToServer(void* socketID, char* para, int isWaitFlag); + +/*--------------------------------- +*说明: 解析应答消息 +*参数说明: +*返回值: 0成功,否则失败 +-----------------------------------*/ +int ParseRsp(char* strRsp); + +/*--------------------------------- +*说明: 发送应答消息给服务端 +*参数说明: socketID: 已经建立连接的socket句柄 + data : 发送数据 +*返回值: 0成功,否则失败 +-----------------------------------*/ +int SendRspToSever(void* socketID, char *result); + +/*--------------------------------- +*说明: 等待服务端的应答 +*参数说明: socketID: 已经建立连接的socket句柄 +*返回值: 0成功,否则失败 +-----------------------------------*/ +int WaitRspFromServer(void* socketID); + +/*--------------------------------- +*说明: 执行cmdlist中的命令 +*参数说明: +*返回值: 0成功,否则失败 +-----------------------------------*/ +int RunCmdlist(); + +/*--------------------------------- +*说明: 执行命令行参数 +*参数说明: +*返回值: 0成功,否则失败 +-----------------------------------*/ +int RunCmd(int argc, char **argv); + +#endif diff --git a/needgcov-1.0/client-tool/multi_thread_client.c b/needgcov-1.0/client-tool/multi_thread_client.c new file mode 100644 index 0000000..3e3956d --- /dev/null +++ b/needgcov-1.0/client-tool/multi_thread_client.c @@ -0,0 +1,1299 @@ +/////////////////////////////////////////////////////////////////////////////////// +// multi_thread_client.c +/////////////////////////////////////////////////////////////////////////////////// +//本文件是客户机多线程多次重复与服务交互的代码 +#include "common.h" + +void myFree(void* ptr) +{ + if(ptr != NULL) + { + free(ptr); + ptr = NULL; + } +} + +int strTerm(char* str) +{ + char* tmpstr = (char*) malloc(strlen(str) + 1); + char* tmpstart = NULL; + char* tmpend = NULL; + int len = 0; + + strcpy(tmpstr, str); + tmpstart = tmpstr; + + while((*tmpstart) == ' ') + { + tmpstart++; + } + + tmpend = tmpstr + strlen(str) -1; + while((*tmpend) == ' ' || (*tmpend) == '\r' || (*tmpend) == '\n') + { + tmpend--; + } + + if (tmpend >= tmpstart) + { + len = tmpend - tmpstart + 1; + strncpy(str, tmpstart, len); + str[len] = '\0'; + } + else + { + strcpy(str, "\0"); + } + + tmpstart = NULL; + tmpend = NULL; + + myFree(tmpstr); + + return 0; +} + +void pritfExit() +{ + int iExit = 0; + + printf("Please enter any key to exit:"); + scanf("%d", iExit); + + return; +} + +#ifdef IN_LINUX + +long send_all(int fd, char *message, int len) +{ + int sended = 0; + int this_time = len; + char *p = message; + while (sended < len) + { + this_time = send(fd, p + sended, this_time, 0); + if (this_time < 0) + if (errno == EINTR) + { + continue; + } + else + { + printf("send error.(%s)", strerror(errno)); + return -1; + } + sended += this_time; + } + return sended; +} + +int SendDataToServer_linux(int socketID, char *data, int iLen) +{ + int iRet = 0; + if (data == NULL) + { + printf("Error: Send Data is NULL!\n"); + return -1; + } + + char buffer[MAX_BUFFER_SIZE]; + bzero(buffer,MAX_BUFFER_SIZE); + memcpy(buffer, data, iLen); + + //向服务端发送buffer中的数据 + if ((iRet = send_all(socketID, buffer, iLen)) != iLen) + { + printf("Error: Send Data Failed!\n"); + return -1; + } + + return 0; +} + +int RecvDataFromServer_linux(int socketID, char *buffer, int iBufLen) +{ + int length = 0; + + if (buffer == NULL) + { + printf("Error: Buffer is NULL!\n"); + return -1; + } + + memset(buffer, 0, iBufLen); + + //接收服务端发送来的信息到buffer中 + length = recv(socketID, buffer, iBufLen, 0); + if (length < 0) + { + printf("Error: Client Recieve Data Failed!\n"); + return -1; + } + else if (length == 0) + { + printf("Server Exit!\n"); + return 0; + } + + return length; +} + + +int ConnectServer_linux(const char* pServerIP, int iServerPort, int iTimeout) +{ + int iReConnectTimes = 0; + + if (pServerIP == NULL) + { + printf("Error: ServerIP is NULL!\n"); + return -1; + } + + //设置一个socket地址结构client_addr,代表客户机internet地址, 端口 + struct sockaddr_in client_addr; + bzero(&client_addr,sizeof(client_addr)); //把一段内存区的内容全部设置为0 + client_addr.sin_family = AF_INET; //internet协议族 + client_addr.sin_addr.s_addr = htons(INADDR_ANY);//INADDR_ANY表示自动获取本机地址 + client_addr.sin_port = htons(0); //0表示让系统自动分配一个空闲端口 + + //创建用于internet的流协议(TCP)socket,用client_socket代表客户机socket + int client_socket = socket(AF_INET, SOCK_STREAM, 0); + if (client_socket == -1) + { + printf("Error: Create Socket Failed!\n"); + close(client_socket); + return -1; + } + + + //把客户机的socket和客户机的socket地址结构联系起来 + if( bind(client_socket, (struct sockaddr*)&client_addr, sizeof(client_addr))) + { + printf("Error: Client Bind Port Failed!\n"); + close(client_socket); + return -1; + } + + //设置一个socket地址结构server_addr,代表服务器的internet地址, 端口 + struct sockaddr_in server_addr; + bzero(&server_addr,sizeof(server_addr)); + server_addr.sin_family = AF_INET; + + //inet_aton比起inet_addr来,可以通过返回值检测IP是否有效 + if (inet_aton(pServerIP, &server_addr.sin_addr) == 0) //服务器的IP地址来自程序的参数 + { + printf("Error: Server IP Address Error!\n"); + return -1; + } + + server_addr.sin_port = htons(iServerPort); + socklen_t server_addr_length = sizeof(server_addr); + + //向服务器发起连接,连接成功后client_socket代表了客户机和服务器的一个socket连接 + //支持失败后重连 + while (connect(client_socket, (struct sockaddr*)&server_addr, server_addr_length) < 0) + { + iReConnectTimes ++; + if (iReConnectTimes > iTimeout) + { + printf("Error: Has Reconnect %d Times! Connect To %s:%d Failed!\n", iReConnectTimes, pServerIP, iServerPort); + return -1; + } + printf("Error: Can Not Connect To %s:%d ! Reconnect : %d ...\n", pServerIP, iServerPort, iReConnectTimes); + sleep(2); + } + + return client_socket; +} + +void* Thread_linux(void* para) +{ + int iRet = 0; + + iRet = TalkToServer(para, MAX_CONNECT_TIMEOUT); + if (iRet == -1) + { + printf("Error: Talk To Server Failed!\n"); + } + + pthread_exit(NULL); +} + + +#else + +int SendDataToServer_windows(int socketID, char *data, int iBufLen) +{ + char buffer[MAX_BUFFER_SIZE]; + int iRet = 0; + + if (data == NULL) + { + printf("Error: Send Data is NULL!\n"); + return -1; + } + + memset(buffer, 0, MAX_BUFFER_SIZE); + memcpy(buffer, data, iBufLen); + + //向客户端发送buffer中的数据 + iRet = send(socketID, buffer, iBufLen, 0); + if (iRet != iBufLen) + { + printf("Error: send error: %d!\n", GetLastError()); + return -1; + } + + return 0; +} + + +int RecvDataFromServer_windows(int socketID, char *buffer, int iBufLen) +{ + int length = 0; + + if (buffer == NULL) + { + printf("Error: Malloc Failed!\n"); + return -1; + } + + //接收客户端发送来的信息到buffer中 + length = recv(socketID, buffer, iBufLen, 0); + if (length == 0 || length == SOCKET_ERROR) + { + printf("Socket Close!\n"); + return 0; + } + + return length; +} + +ULONG __stdcall Thread_windows(void *para) +{ + int iRet = 0; + + iRet = TalkToServer(para, MAX_CONNECT_TIMEOUT); + if (iRet == -1) + { + printf("Error: Talk To Server Failed!\n"); + return -1; + } + + return 0; +} + + +SOCKET ConnectServer_windows(const char* pServerIP, int iServerPort, int iTimeout) +{ + WSADATA Ws; + struct sockaddr_in server_addr; + SOCKET client_socket; + int iRet; + int iReConnectTimes = 0; + + if (pServerIP == NULL) + { + printf("Error: ServerIP is NULL!\n"); + return -1; + } + + //初始化windows socket + if (WSAStartup(MAKEWORD(2, 2), &Ws) != 0) + { + printf("Error: Init Windows Socket Failed: %d !\n", GetLastError()); + return -1; + } + + //创建用于internet的流协议(TCP)socket,用client_socket代表客户机socket + client_socket = socket(AF_INET, SOCK_STREAM, 0); + if (client_socket == INVALID_SOCKET) + { + printf("Error: Create Socket Failed!\n"); + closesocket(client_socket); + return -1; + } + + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = inet_addr(pServerIP); + server_addr.sin_port = htons(iServerPort); + memset(server_addr.sin_zero, 0x00, 8); + + //向服务器发起连接,连接成功后client_socket代表了客户机和服务器的一个socket连接 + //支持失败后重连 + while (connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) + { + iReConnectTimes ++; + if (iReConnectTimes >= iTimeout) + { + printf("Error: Has Reconnect %d Times! Connect To %s:%d Failed!\n", iReConnectTimes, pServerIP, iServerPort); + return -1; + } + printf("Error: Can Not Connect To %s:%d ! Reconnect : %d ...\n", pServerIP, iServerPort, iReConnectTimes); + Sleep(2000); + } + + return client_socket; +} + + +#endif + +int TalkToServer(void* para, int iTimeout) +{ + int iRet = 0; + int iPort = 0; + char strIP[MAX_IP_LENGTH]; + char strCmd[MAX_CMDLINE_LENGTH]; + char *pCmdType = NULL; + char *pPara = NULL; + + #ifdef IN_LINUX + int client_socket; + #else + SOCKET client_socket; + #endif + + memset(strIP, 0, MAX_IP_LENGTH); + memset(strCmd, 0, MAX_CMDLINE_LENGTH); + + iRet = ParseCmd((char*)para, strIP, &iPort, strCmd); + if (iRet == -1) + { + printf("Error: Parse CMD Failed!\n"); + return -1; + } + + #ifdef IN_LINUX + client_socket = ConnectServer_linux(strIP, iPort, iTimeout); + if (client_socket == -1) + { + printf("Error: ConnectServer Failed!\n"); + return -1; + } + + #else + client_socket = ConnectServer_windows(strIP, iPort, iTimeout); + if (client_socket == INVALID_SOCKET) + { + printf("Error: ConnectServer Failed!\n"); + return -1; + } + #endif + + printf("Client Connect Server %s Success!\n", strIP); + + //解析发送给服务端的命令类型 + pCmdType = strtok(strCmd, "["); + if (pCmdType == NULL) + { + printf("Error: Cmdlist para %s error!\n", para); + + #ifdef IN_LINUX + close(client_socket); + #else + closesocket(client_socket); + #endif + + return -1; + } + + strTerm(pCmdType); + + //解析发送给服务端的命令参数 + pPara = strtok(NULL, "]"); + if (pPara == NULL) + { + printf("Error: Cmdlist para %s error!\n", para); + + #ifdef IN_LINUX + close(client_socket); + #else + closesocket(client_socket); + #endif + + return -1; + } + + strTerm(pPara); + + //为拷贝文件类型的命令 + if (strcmp(COPYFILE, pCmdType) == 0) + { + if (CopyFileFromServer((void*)client_socket, pPara) == -1) + { + printf("Error: Copy File From Server Failed!\n"); + + #ifdef IN_LINUX + close(client_socket); + #else + closesocket(client_socket); + #endif + + return -1; + } + } + //只负责下发系统命令,不管执行的情况 + else if(strcmp(CMD, pCmdType) == 0) + { + iRet = SendCmdToServer((void*)client_socket, pPara, CMD_FALG_NOWAIT); + if (iRet == -1) + { + printf("Error: Send Data To Server Failed!\n"); + + #ifdef IN_LINUX + close(client_socket); + #else + closesocket(client_socket); + #endif + + return -1; + } + } + else if(strcmp(CMDECHO,pCmdType) == 0) + { + iRet = SendCmdToServer((void*)client_socket, pPara, CMD_ECHO); + if (iRet == -1) + { + printf("Error: Send Data To Server Failed!\n"); + + #ifdef IN_LINUX + close(client_socket); + #else + closesocket(client_socket); + #endif + + return -1; + } + } + //为等待客户端执行完系统命令 + else if(strcmp(CMDWAIT, pCmdType) == 0) + { + iRet = SendCmdToServer((void*)client_socket, pPara, CMD_FALG_WAIT); + if (iRet == -1) + { + printf("Error: Send Data To Server Failed!\n"); + + #ifdef IN_LINUX + close(client_socket); + #else + closesocket(client_socket); + #endif + + return -1; + } + } + //为发送文件类型的命令 + else if (strcmp(SENDFILE, pCmdType) == 0) + { + if (SendFileToServer((void*)client_socket, pPara) == -1) + { + printf("Error: Send File To Server Failed!\n"); + + #ifdef IN_LINUX + close(client_socket); + #else + closesocket(client_socket); + #endif + + return -1; + } + } + //为发送文件类型的命令 + else if (strcmp(FINDPROC, pCmdType) == 0) + { + if (FindProcessFromServer((void*)client_socket, pPara) == -1) + { + printf("Error: Find Process From Server Failed!\n"); + + #ifdef IN_LINUX + close(client_socket); + #else + closesocket(client_socket); + #endif + + return -1; + } + } + else + { + printf("Error: No Support : %s\n", pCmdType); + + #ifdef IN_LINUX + close(client_socket); + #else + closesocket(client_socket); + #endif + + return -1; + } + + #ifdef IN_LINUX + close(client_socket); + #else + closesocket(client_socket); + #endif + + return 0; +} + + +int SendCmdToServer(void* socketID, char* para, int isWaitFlag) +{ + int iRet = 0; + char buf[BUFSIZE]; + char strCmd[MAX_CMDLINE_LENGTH]; + + memset(strCmd, 0, MAX_CMDLINE_LENGTH); + + if (CMD_FALG_NOWAIT == isWaitFlag) + { + strcpy(strCmd, CMD); + } + else if(CMD_ECHO == isWaitFlag) + { + strcpy(strCmd, CMDECHO); + } + else + { + strcpy(strCmd, CMDWAIT); + } + + strcat(strCmd, "["); + strcat(strCmd, para); + strcat(strCmd, "]"); + iRet = strlen(strCmd); + + #ifdef IN_LINUX + iRet = SendDataToServer_linux((int)socketID, strCmd, iRet); + #else + iRet = SendDataToServer_windows((SOCKET)socketID, strCmd, iRet); + #endif + + if ( iRet == -1) + { + printf("Error: Send Cmd To Server Failed: %s\n", para); + return -1; + } + + printf("Send Cmd To Server Success: %s\n", para); + + //不等待 + if (isWaitFlag == CMD_FALG_NOWAIT) + { + return 0; + } + //等待服务端的应答 + else if(isWaitFlag == CMD_ECHO) + { + memset(buf, 0, BUFSIZE); + recv((int)socketID, buf, BUFSIZE, 0); + printf("%s",buf); + while(recv((int)socketID, buf, BUFSIZE, 0) != 0) + { + printf("%s",buf); + } + } + else + { + iRet = WaitRspFromServer(socketID); + if ( iRet == -1) + { + printf("Error: Wait for Rsp From Server Failed!\n"); + return -1; + } + else if (iRet == RET_OK) + { + printf("---------\n"); + printf("Response: OK!\n"); + } + else if (iRet == RET_FAILED) + { + printf("---------\n"); + printf("Response: Failed!\n"); + printf(" Server Run Cmd Failed! No such file or directory, or permission denied ...\n Please Check!\n"); + } + + printf("---------\n"); + } + + return iRet; +} + + +int ParseCmd(char *data, char *ip, int *port, char *cmd) +{ + //解析命令参数 + unsigned char str[MAX_CMDLINE_LENGTH]; + char * pServerIP = NULL; + char * pServerPort = NULL; + char * pCmd = NULL; + + memset(str, 0, MAX_CMDLINE_LENGTH); + strncpy(str, data, MAX_CMDLINE_LENGTH); + + //解析服务端IP + pServerIP = strtok(str, "#"); + if (pServerIP == NULL) + { + return -1; + } + + strTerm(pServerIP); + strncpy(ip, pServerIP, MAX_IP_LENGTH); + + //解析服务端PORT + pServerPort = strtok(NULL, "#"); + if (pServerPort == NULL) + { + return -1; + } + strTerm(pServerPort); + + *port = atoi(pServerPort); + + //解析发送给服务端的命令 + pCmd = strtok(NULL, "#"); + if (pCmd == NULL) + { + return -1; + } + + strTerm(pCmd); + strncpy(cmd, pCmd, MAX_CMDLINE_LENGTH); + + return 0; +} + +int CopyFileFromServer(void* socketID, char *data) +{ + char strPara[MAX_CMDLINE_LENGTH]; + char strBuffer[MAX_BUFFER_SIZE]; + char *pFrom = NULL; + char *pTo = NULL; + FILE *fr = NULL; + int iRet = 0; + int iTotal = 0; + + #ifdef IN_LINUX + int socket; + #else + SOCKET socket; + #endif + + #ifdef IN_LINUX + socket = (int)socketID; + if (socket == -1) + { + printf("Error: Socket ID Error!\n"); + return -1; + } + #else + socket = (SOCKET)socketID; + if (socket == INVALID_SOCKET) + { + printf("Error: Socket ID Error!\n"); + return -1; + } + #endif + + if (data == NULL) + { + printf("Error: Send Data is NULL!\n"); + return -1; + } + + memset(strPara, 0, MAX_CMDLINE_LENGTH); + memset(strBuffer, 0, MAX_BUFFER_SIZE); + + //解析要拷贝的服务端文件(全路径) + pFrom = strtok(data, ","); + if (pFrom == NULL) + { + printf("Cmdlist para %s error!\n", strPara); + return -1; + } + + strTerm(pFrom); + + //解析文件在客户端的存放路径(全路径) + pTo = strtok(NULL, ","); + if (pTo == NULL) + { + printf("Cmdlist para %s error!\n", strPara); + return -1; + } + + strTerm(pTo); + + //构造发送命令给服务端 + strcpy(strPara, COPYFILE); + strcat(strPara, "["); + strcat(strPara, pFrom); + strcat(strPara, "]"); + iRet = strlen(strPara); + + //发送给服务端 + #ifdef IN_LINUX + iRet = SendDataToServer_linux(socket, strPara, iRet); + #else + iRet = SendDataToServer_windows(socket, strPara, iRet); + #endif + + if ( iRet == -1) + { + printf("Error: Send Data To Server Failed: %s\n", strPara); + return -1; + } + + printf("Send Data To Server Success: %s\n", strPara); + + //打开准备写的文件 + fr = fopen(pTo, "wb"); + if(fr == NULL) + { + printf("Error: Open File :%s Failed!\n", pTo); + return -1; + } + + //接收来自服务端的文件数据 + #ifdef IN_LINUX + iRet = RecvDataFromServer_linux(socket, strBuffer, MAX_BUFFER_SIZE); + #else + iRet = RecvDataFromServer_windows(socket, strBuffer, MAX_BUFFER_SIZE); + #endif + + //出错,直接关闭socket + if (iRet == -1) + { + #ifdef IN_LINUX + close(socket); + #else + closesocket(socket); + #endif + } + + //接收到文件数据,则写入文件 + while ( iRet > 0) + { + iTotal += iRet; + + if (fwrite(strBuffer, sizeof(char), iRet, fr) != iRet) + { + printf("Error: Write File Failed : %s\n", pTo); + fclose(fr); + return -1; + } + + //发送应答消息给服务器 + //发送出错 + if (SendRspToSever(socketID, RSP_OK) == -1) + { + printf("Error: Send Rsp To Server Failed!\n"); + fclose(fr); + return -1; + } + + memset(strBuffer, 0, MAX_BUFFER_SIZE); + + #ifdef IN_LINUX + iRet = RecvDataFromServer_linux(socket, strBuffer, MAX_BUFFER_SIZE); + #else + iRet = RecvDataFromServer_windows(socket, strBuffer, MAX_BUFFER_SIZE); + #endif + } + + printf("Copy File length: %d\n", iTotal); + + fclose(fr); + return 0; +} + +int SendFileToServer(void* socketID, char *data) +{ + char strPara[MAX_CMDLINE_LENGTH]; + char strBuffer[MAX_BUFFER_SIZE]; + char *pFrom = NULL; + char *pTo = NULL; + FILE *fr = NULL; + int iRet = 0; + int iLen = 0; + int iTotal = 0; + + #ifdef IN_LINUX + int socket; + #else + SOCKET socket; + #endif + + #ifdef IN_LINUX + socket = (int)socketID; + if (socket == -1) + { + printf("Error: Socket ID Error!\n"); + return -1; + } + #else + socket = (SOCKET)socketID; + if (socket == INVALID_SOCKET) + { + printf("Error: Socket ID Error!\n"); + return -1; + } + #endif + + if (data == NULL) + { + printf("Error: Send Data is NULL!\n"); + return -1; + } + + memset(strPara, 0, MAX_CMDLINE_LENGTH); + memset(strBuffer, 0, MAX_BUFFER_SIZE); + + //解析要发送的客户端的文件(全路径) + pFrom = strtok(data, ","); + if (pFrom == NULL) + { + printf("Cmdlist para %s error!\n", strPara); + return -1; + } + + strTerm(pFrom); + + //解析文件在服务端的存放路径(全路径) + pTo = strtok(NULL, ","); + if (pTo == NULL) + { + printf("Cmdlist para %s error!\n", strPara); + return -1; + } + + strTerm(pTo); + + //构造发送命令给服务端 + strcpy(strPara, SENDFILE); + strcat(strPara, "["); + strcat(strPara, pTo); + strcat(strPara, "]"); + iRet = strlen(strPara); + + //发送给服务端 + #ifdef IN_LINUX + iLen = SendDataToServer_linux(socket, strPara, iRet); + #else + iLen = SendDataToServer_windows(socket, strPara, iRet); + #endif + + if ( iLen == -1) + { + printf("Error: Send Data To Server Failed: %s\n", strPara); + return -1; + } + + printf("Send Data To Server Success: %s\n", strPara); + + //等待应答消息 + iRet = WaitRspFromServer(socketID); + if ( iRet == -1) + { + printf("Error: Wait for Rsp From Server Failed!\n"); + return -1; + } + else if (iRet == RET_FAILED) + { + printf("Error: Server Error!\n"); + return -1; + } + + printf("Server Read To Receive File!\n"); + + //打开文件 + fr = fopen(pFrom, "rb"); + if(fr == NULL) + { + printf("Error: Open File :%s Failed!\n", pFrom); + return -1; + } + + printf("Sending File to Server...\n"); + + //开始读文件的内容 + while ((iLen = fread(strBuffer, sizeof(char), MAX_BUFFER_SIZE-1, fr)) > 0) + { + iTotal += iLen; + + #ifdef IN_LINUX + iRet = SendDataToServer_linux(socket, strBuffer, iLen); + #else + iRet = SendDataToServer_windows(socket, strBuffer, iLen); + #endif + + if (iRet == -1) + { + printf("Error: Send Data To Server Failed!\n"); + fclose(fr); + return -1; + } + + memset(strBuffer, 0, MAX_BUFFER_SIZE); + + //等待服务端的应答 + if (WaitRspFromServer(socketID) == -1) + { + printf("Error: Wait for Rsp From Server Failed!\n"); + return -1; + } + } + + printf("Send File Length: %ld\n", iTotal); + fclose(fr); + + return 0; +} + + +int FindProcessFromServer(void* socketID, char *data) +{ + int iRet = 0; + char strCmd[MAX_CMDLINE_LENGTH]; + + #ifdef IN_LINUX + int socket; + #else + SOCKET socket; + #endif + + #ifdef IN_LINUX + socket = (int)socketID; + if (socket == -1) + { + printf("Error: Socket ID Error!\n"); + return -1; + } + #else + socket = (SOCKET)socketID; + if (socket == INVALID_SOCKET) + { + printf("Error: Socket ID Error!\n"); + return -1; + } + #endif + + memset(strCmd, 0, MAX_CMDLINE_LENGTH); + strcpy(strCmd, FINDPROC); + strcat(strCmd, "["); + strcat(strCmd, data); + strcat(strCmd, "]"); + iRet = strlen(strCmd); + + #ifdef IN_LINUX + iRet = SendDataToServer_linux((int)socketID, strCmd, iRet); + #else + iRet = SendDataToServer_windows((SOCKET)socketID, strCmd, iRet); + #endif + + if ( iRet == -1) + { + printf("Error: Send Data To Server Failed: %s\n", strCmd); + return -1; + } + + printf("Send Data To Server Success: %s\n", strCmd); + + //等待应答消息 + iRet = WaitRspFromServer(socketID); + if ( iRet == -1) + { + printf("Error: Wait for Rsp From Server Failed!\n"); + return -1; + } + + if (iRet == RET_OK) + { + printf("The Process %s Run Status: YES\n", data); + } + else if (iRet == RET_FAILED) + { + printf("The Process %s Run Status: NO\n", data); + } + + return iRet; +} + +int WaitRspFromServer(void* socketID) +{ + int iRet; + char strPara[MAX_CMDLINE_LENGTH]; + + #ifdef IN_LINUX + int socket; + #else + SOCKET socket; + #endif + + #ifdef IN_LINUX + socket = (int)socketID; + if (socket == -1) + { + printf("Error: Socket ID Error!\n"); + return -1; + } + #else + socket = (SOCKET)socketID; + if (socket == INVALID_SOCKET) + { + printf("Error: Socket ID Error!\n"); + return -1; + } + #endif + + memset(strPara, 0, MAX_CMDLINE_LENGTH); + + #ifdef IN_LINUX + iRet = RecvDataFromServer_linux(socket, strPara, MAX_CMDLINE_LENGTH); + #else + iRet = RecvDataFromServer_windows(socket, strPara, MAX_CMDLINE_LENGTH); + #endif + + if (iRet > 0) + { + iRet = ParseRsp(strPara); + if (iRet == -1) + { + printf("Error: Parse Rsp %s Failed!\n", strPara); + return -1; + } + + return iRet; + } + else + { + return -1; + } +} + + +int ParseRsp(char* pRsp) +{ + char *pPara = NULL; + char strRsp[MAX_RSP_LENGTH]; + + memset(strRsp, 0, MAX_RSP_LENGTH); + + if (strRsp == NULL) + { + printf("Error: strRsp is NULL!\n"); + return -1; + } + + strncpy(strRsp, pRsp, MAX_RSP_LENGTH); + + pPara = strtok(strRsp, "#"); + if (pRsp == NULL) + { + printf("Error: Rsp %s error!\n", pRsp); + return -1; + } + + pPara = strtok(NULL, "#"); + if (pRsp == NULL) + { + printf("Error: Rsp %s error!\n", pRsp); + return -1; + } + + // 服务端应答消息为OK + if (strcmp(pPara, RSP_OK) == 0) + { + return RET_OK; + } + else if (strcmp(pPara, RSP_FAILED) == 0) + { + return RET_FAILED; + } + + return -1; + +} + + +int SendRspToSever(void* socketID, char *result) +{ + int iRet = 0; + char strRsp[MAX_RSP_LENGTH]; + + #ifdef IN_LINUX + int socket; + #else + SOCKET socket; + #endif + + #ifdef IN_LINUX + socket = (int)socketID; + if (socket == -1) + { + printf("Error: Socket ID Error!\n"); + return -1; + } + #else + socket = (SOCKET)socketID; + if (socket == INVALID_SOCKET) + { + printf("Error: Socket ID Error!\n"); + return -1; + } + #endif + + if (result == NULL) + { + printf("Error: Send data is NULL!\n"); + return -1; + } + + memset(strRsp, 0, MAX_RSP_LENGTH); + + //构造应答消息 + strcpy(strRsp, RSP); + strcat(strRsp, "#"); + strcat(strRsp, result); + + #ifdef IN_LINUX + iRet = SendDataToServer_linux(socket, strRsp, strlen(strRsp)); + #else + iRet = SendDataToServer_windows(socket, strRsp, strlen(strRsp)); + #endif + + if (iRet == -1) + { + printf("Error: Send Data To Client Failed!\n"); + return -1; + } + + return 0; +} + + +int CreateSubThread(char *para) +{ + #ifdef IN_LINUX + pthread_t child_thread; + #else + HANDLE child_thread; + #endif + + if (para == NULL) + { + printf("Error: CreateThread func para is NULL\n"); + return -1; + } + + #ifdef IN_LINUX + + if( pthread_create(&child_thread, NULL, Thread_linux, (void *)para) < 0 ) + { + printf("Error: Create Thread Failed : %s\n", strerror(errno)); + return -1; + } + + //等待线程退出 + if(child_thread != 0) + { + pthread_join(child_thread, NULL); + } + + #else + + child_thread = CreateThread(NULL, 0, Thread_windows, (void *)para, 0, NULL); + if (child_thread == NULL) + { + printf("Error: Create Thread Failed!\n"); + return -1; + } + + WaitForSingleObject(child_thread, INFINITE); + + #endif + + return 0; +} + +int RunCmdlist() +{ + FILE* fr = NULL; + char strCmdLine[MAX_BUFFER_SIZE]; + int iCmdNum = 1; + + memset(strCmdLine, 0, MAX_BUFFER_SIZE); + + // 读取cmdlist,串行执行所有命令 + fr = fopen(CMDLIST, "r"); + if(fr == NULL) + { + printf("open the file of %s failed!\n", CMDLIST); + return -1; + } + + while (fgets(strCmdLine, MAX_BUFFER_SIZE, fr) != NULL) + { + printf("=============== begin %d ===============\n", iCmdNum); + //创建线程,由线程来发送操作 + CreateSubThread(strCmdLine); + + printf("=============== end %d ===============\n", iCmdNum); + + iCmdNum++; + } + + fclose(fr); + + return 0; +} + +int RunCmd(int argc, char **argv) +{ + + int iRet = 0; + int iTimeout = MAX_CONNECT_TIMEOUT; + char szCmd[MAX_CMDLINE_LENGTH]; + + printf("=============== begin ===============\n"); + + memset(szCmd, 0, MAX_CMDLINE_LENGTH); + strncpy(szCmd, argv[1], MAX_CMDLINE_LENGTH); + + if (argc >= 3) + { + iTimeout = atoi(argv[2]); + } + + iRet = TalkToServer(szCmd, iTimeout); + if (iRet == -1) + { + printf("Error: Talk To Server Failed!\n"); + } + + printf("=============== end ===============\n"); + + return 0; +} + +int main(int argc, char **argv) +{ + //执行命令行 + if (argc >= 2) + { + RunCmd(argc, argv); + } + //执行cmdlist中的命令 + else + { + RunCmdlist(); + } + + return 0; +} + + + diff --git a/needgcov-1.0/server-tool/Makefile b/needgcov-1.0/server-tool/Makefile new file mode 100644 index 0000000..a7f4a72 --- /dev/null +++ b/needgcov-1.0/server-tool/Makefile @@ -0,0 +1,6 @@ +server-tool-linux:multi_thread_server.o + $(CC) -o server-tool-linux multi_thread_server.o -lpthread +multi_thread_server.o:multi_thread_server.c common.h + $(CC) ${CFLAGS} -c multi_thread_server.c +clean: + -rm server-tool-linux multi_thread_server.o diff --git a/needgcov-1.0/server-tool/common.h b/needgcov-1.0/server-tool/common.h new file mode 100644 index 0000000..5821e97 --- /dev/null +++ b/needgcov-1.0/server-tool/common.h @@ -0,0 +1,258 @@ +/* +说明:存放接口及常用结构 +*/ +#ifndef _COMMON_H +#define _COMMON_H + +// 确定编译环境 +#define IN_LINUX + +#include +#include +#include + +#ifdef IN_LINUX +//#include +#include +#include +#include +#include +#include +#include +#include +#include +#else +#include +#include +#include +#include +#include +#endif + +#pragma comment(lib,"ws2_32.lib") + +#define MAX_BUFFER_SIZE 1024 +#define MAX_CMDLINE_LENGTH 200 +#define MAX_THREAD_NUM 40 +#define LENGTH_OF_LISTEN_QUEUE 20 +#define MAX_IP_LENGTH 20 +#define MAX_CMDTYPE_LENGTH 20 +#define MAX_RSP_LENGTH 40 +#define BUFSIZE 65535 + +#define CONFIG "config.ini" + +//工具的版本 +#define VERSION "1.1.1" + +//应答消息 +#define RSP_OK "0" +#define RSP_FAILED "1" + +#define RET_OK 0 +#define RET_FAILED 1 + + +//支持的命令类型(命令类型的定义字符串不超过 MAX_CMDTYPE_LENGTH 定义的长度) +#define COPYFILE "copyfile" +#define CMD "cmd" +#define CMDWAIT "cmdwait" //等待客户端命令执行完成 +#define SENDFILE "sendfile" +#define FINDPROC "findproc" +#define RSP "rsp" //应答消息 +#define CMDECHO "cmdecho" + + +#define CMD_FALG_WAIT 0 +#define CMD_FALG_NOWAIT 1 +#define CMD_ECHO 2 + + +//服务器端口号,从配置文件中读取 +int g_port; + + +#ifdef IN_LINUX +/*--------------------------------- +*说明: 与客户端进行交互 +*参数说明: +*返回值: 0成功,否则失败 +-----------------------------------*/ +void * TalkToClient_linux(void *data); + +/*--------------------------------- +*说明: 发送数据给客户端 +*参数说明: socketID: 已经建立连接的socket句柄 + buffer : 需发送给客户端的数据 +*返回值: 0成功,否则失败 +-----------------------------------*/ +int SendDataToClient_linux(int socketID, char *data, int iDataLen); + + +/*--------------------------------- +*说明: 接收来自给客户端的数据 +*参数说明: socketID: 已经建立连接的socket句柄 + buffer : 为收到的数据 +*返回值: 0 断开连接 + 大于0 收到数据的长度 + -1 失败 +-----------------------------------*/ +int RecvDataFromClient_linux(int socketID, char *buffer, int iBufLen); + + +/*--------------------------------- +*说明: 在linux环境下查找指定进程是否存在 +*参数说明: +*返回值: 0 存在,其他表示不存在 +-----------------------------------*/ +int FindProcess_linux(char *pProcessName); + + +/*--------------------------------- +*说明: 初始化服务连接及监听 +*参数说明: +*返回值: 0成功,否则失败 +-----------------------------------*/ +int InitServer_linux(); + + +/*--------------------------------- +*说明: 开始提供服务 +*参数说明: +*返回值: 0成功,否则失败 +-----------------------------------*/ +int StartService_linux(int server_socket); + + +#else +/*--------------------------------- +*说明: 与客户端进行交互 +*参数说明: +*返回值: 0成功,否则失败 +-----------------------------------*/ +ULONG __stdcall TalkToClient_windows(void* lpParameter); + +/*--------------------------------- +*说明: 发送数据给客户端 +*参数说明: socketID: 已经建立连接的socket句柄 + buffer : 需发送给客户端的数据 +*返回值: 0成功,否则失败 +-----------------------------------*/ +int SendDataToClient_windows(SOCKET socketID, char *data, int iDataLen); + + +/*--------------------------------- +*说明: 接收来自给客户端的数据 +*参数说明: socketID: 已经建立连接的socket句柄 + buffer : 为收到的数据,调用者必须手动释放该空间 +*返回值: 0 断开连接 + 大于0 收到数据的长度 + -1 失败 +-----------------------------------*/ +int RecvDataFromClient_windows(SOCKET socketID, char *buffer, int iBufLen); + +/*--------------------------------- +*说明: 在windows环境下查找指定进程是否存在 +*参数说明: +*返回值: 0 存在,其他表示不存在 +-----------------------------------*/ +int FindProcess_windows(char *pProcessName); + + +/*--------------------------------- +*说明: 初始化服务连接及监听 +*参数说明: +*返回值: 0成功,否则失败 +-----------------------------------*/ +int InitServer_windows(); + + +/*--------------------------------- +*说明: 开始提供服务 +*参数说明: +*返回值: 0成功,否则失败 +-----------------------------------*/ +int StartService_windows(SOCKET server_socket); + +#endif + +/*--------------------------------- +*说明: 执行拷贝文件到客户端 +*参数说明: socketID: 已经建立连接的socket句柄 + data : 需要拷贝的文件名(全路径) +*返回值: 0成功,否则失败 +-----------------------------------*/ +int CopyFileToClient(void* socketID, char *pFile); + +/*--------------------------------- +*说明: 接收来自客户端的文件 +*参数说明: socketID: 已经建立连接的socket句柄 + file : 需要保存的文件名(全路径) +*返回值: 0成功,否则失败 +-----------------------------------*/ +int RecvFileFromClient(void* socketID, char *pFile); + + +/*--------------------------------- +*说明: 执行系统命令 +*参数说明: socketID: 已经建立连接的socket句柄 + pCmd : 执行的命令 +*返回值: 0成功,否则失败 +-----------------------------------*/ +int RunSystemCmd(void* socketID, char *pCmd, int iWaitFlag); + +/*--------------------------------- +*说明: 解析客户端发送过来的命令 +*参数说明: socketID: 已经建立连接的socket句柄 + data : 客户端发送过来的命令 +*返回值: 0成功,否则失败 +-----------------------------------*/ +int ParseCmd(void* socketID, char *data); + +/*--------------------------------- +*说明: 发送应答消息给客户端 +*参数说明: socketID: 已经建立连接的socket句柄 + data : 发送数据 +*返回值: 0成功,否则失败 +-----------------------------------*/ +int SendRspToClient(void* socketID, char *result); + + +/*--------------------------------- +*说明: 等待客户端的应答 +*参数说明: socketID: 已经建立连接的socket句柄 +*返回值: 0成功,否则失败 +-----------------------------------*/ +int WaitRspFromClient(void* socketID); + +/*--------------------------------- +*说明: 解析应答消息 +*参数说明: +*返回值: 0成功,否则失败 +-----------------------------------*/ +int ParseRsp(char* strRsp); + + +/*--------------------------------- +*说明: 查看指定的程序是否正在运行 +*参数说明: socketID: 已经建立连接的socket句柄 + data : 进程名 +*返回值: 0成功,否则失败 +-----------------------------------*/ +int FindProcess(void* socketID, char *data); + + +/*--------------------------------- +*说明: 初始化 +*参数说明: +*返回值: 0成功,否则失败 +-----------------------------------*/ +int Init(); + +/*--------------------------------- +*说明: 运行 +*参数说明: +*返回值: 0成功,否则失败 +-----------------------------------*/ +int Run(); +#endif diff --git a/needgcov-1.0/server-tool/config.ini b/needgcov-1.0/server-tool/config.ini new file mode 100644 index 0000000..d45d098 --- /dev/null +++ b/needgcov-1.0/server-tool/config.ini @@ -0,0 +1 @@ +port:6677 \ No newline at end of file diff --git a/needgcov-1.0/server-tool/multi_thread_server.c b/needgcov-1.0/server-tool/multi_thread_server.c new file mode 100644 index 0000000..05e46e5 --- /dev/null +++ b/needgcov-1.0/server-tool/multi_thread_server.c @@ -0,0 +1,1231 @@ +/////////////////////////////////////////////////////////////////////////////////// +//multi_thread_server.c +/////////////////////////////////////////////////////////////////////////////////// +//本文件是多线程并发服务器的代码 +#include "common.h" + + +void myFree(void* ptr) +{ + if(ptr != NULL) + { + free(ptr); + ptr = NULL; + } +} + +void pritfExit() +{ + int iExit = 0; + + printf("Please enter any key to exit:"); + scanf("%d", &iExit); + + return; +} + + +int strTerm(char* str) +{ + char* tmpstr = (char*) malloc(strlen(str) + 1); + char* tmpstart = NULL; + char* tmpend = NULL; + int len = 0; + + strcpy(tmpstr, str); + tmpstart = tmpstr; + + while((*tmpstart) == ' ') + { + tmpstart++; + } + + tmpend = tmpstr + strlen(str) -1; + while((*tmpend) == ' ' || (*tmpend) == '\r' || (*tmpend) == '\n') + { + tmpend--; + } + + if (tmpend >= tmpstart) + { + len = tmpend - tmpstart + 1; + strncpy(str, tmpstart, len); + str[len] = '\0'; + } + else + { + strcpy(str, "\0"); + } + + tmpstart = NULL; + tmpend = NULL; + + myFree(tmpstr); + + return 0; +} + +int SetServerPort() +{ + FILE* fr = NULL; + char strPort[MAX_CMDLINE_LENGTH]; + char *pPort = NULL; + + memset(strPort, 0, MAX_CMDLINE_LENGTH); + + // 读取cmdlist,串行执行所有命令 + fr = fopen(CONFIG, "r"); + if(fr == NULL) + { + printf(" Error: Open File of %s Failed!\n", CONFIG); + return -1; + } + + if (fgets(strPort, MAX_CMDLINE_LENGTH, fr) != NULL) + { + pPort = strtok(strPort, ":"); + pPort = strtok(NULL, ":"); + + if (pPort != NULL) + { + strcpy(strPort, pPort); + strTerm(strPort); + g_port = atoi(strPort); + printf(" Read Server Port : %d Sucess!\n", g_port); + } + else + { + printf(" Error: File %s is Invaild!", CONFIG); + printf(" Error: Read Server Port Failed!"); + fclose(fr); + return -1; + } + } + + fclose(fr); + return 0; +} + +// 执行系统命令 +int thr_fn(char* para) +{ + int re; +#ifdef IN_LINUX + return system(para); +#else + PVOID OldValue = NULL; + Wow64DisableWow64FsRedirection(&OldValue); + re = system(para); + Wow64RevertWow64FsRedirection(OldValue); + return re; +#endif +} + + +#ifdef IN_LINUX + +int SendDataToClient_linux(int socketID, char *data, int iLen) +{ + char buffer[MAX_BUFFER_SIZE]; + + if (data == NULL) + { + printf("Error: Send Data is NULL!\n"); + return -1; + } + + bzero(buffer,MAX_BUFFER_SIZE); + memcpy(buffer, data, iLen); + + //向客户端发送buffer中的数据 + if (send(socketID, buffer, iLen, 0) == -1) + { + printf("Error: Send Data Failed!\n"); + return -1; + } + return 0; +} + + +int RecvDataFromClient_linux(int socketID, char *buffer, int iBufLen) +{ + int length = 0; + if (buffer == NULL) + { + printf("Error: Buffer is NULL!\n"); + return -1; + } + + memset(buffer, 0, iBufLen); + + //接收客户端发送来的信息到buffer中 + length = recv(socketID, buffer, iBufLen, 0); + if (length < 0 || length == 0) + { + printf("Client Exit!\n"); + return 0; + } + + return length; +} + +void * TalkToClient_linux(void *data) +{ + //void* 数据可以转换为任意类型数据 + int new_server_socket = (int)data; + char buffer[MAX_CMDLINE_LENGTH]; + char *pCmdType = NULL; + int iRet = 0; + + //将线程状态改为unjoinable,确保线程退出后资源释放 + pthread_detach(pthread_self()); + memset(buffer, 0, MAX_CMDLINE_LENGTH); + + //接收来自客户端的数据 + if (RecvDataFromClient_linux(new_server_socket, buffer, MAX_CMDLINE_LENGTH) > 0) + { + iRet = ParseCmd((void*)new_server_socket, buffer); + if (iRet == -1) + { + printf("Error: Parse Cmd Failed!\n"); + } + } + + close(new_server_socket); + pthread_exit(NULL); +} + +int FindProcess_linux(char *pProcessName) +{ + char szCmd[MAX_BUFFER_SIZE]; + char *pProc = NULL; + FILE *fr = NULL; + + memset(szCmd, 0, MAX_BUFFER_SIZE); + + //构造查询进程系统命令 + strcpy(szCmd, "pidof "); + strcat(szCmd, pProcessName); + strcat(szCmd, " >tmp_process.txt"); + + //执行命令 + thr_fn(szCmd); + + fr = fopen("tmp_process.txt", "r"); + if (fr == NULL) + { + printf("Error: Open file tmp_process.txt Failed!\n"); + return -1; + } + + memset(szCmd, 0, MAX_BUFFER_SIZE); + + while (fgets(szCmd, MAX_BUFFER_SIZE, fr) != NULL) + { + if (strcmp("\n",szCmd) != 0) + { + fclose(fr); + return RET_OK; + } + } + + fclose(fr); + return RET_FAILED; +} + +int InitServer_linux() +{ + int server_socket = -1; + + //设置一个socket地址结构server_addr,代表服务器internet地址, 端口 + struct sockaddr_in server_addr; + bzero(&server_addr,sizeof(server_addr)); //把一段内存区的内容全部设置为0 + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = htonl(INADDR_ANY); + server_addr.sin_port = htons(g_port); + + //创建用于internet的流协议(TCP)socket,用server_socket代表服务器socket + server_socket = socket(AF_INET, SOCK_STREAM, 0); + if (server_socket == -1) + { + printf(" Error: Create Socket Failed!\n"); + close(server_socket); + return -1; + } + printf(" Create Socket Success!\n"); + + //把socket和socket地址结构联系起来 + if( bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr))) + { + printf(" Error: Server Bind Port : %d Failed!\n", g_port); + close(server_socket); + return -1; + } + + printf(" Server Bind Port : %d Success!\n", g_port); + + //server_socket用于监听 + if ( listen(server_socket, LENGTH_OF_LISTEN_QUEUE) ) + { + printf(" Error: Server Listen Failed!\n"); + close(server_socket); + return -1; + } + + return server_socket; +} + + +int StartService_linux(int server_socket) +{ + int new_server_socket; + int err = 0; + pthread_t child_thread; + + pthread_attr_t child_thread_attr; + pthread_attr_init(&child_thread_attr); + pthread_attr_setdetachstate(&child_thread_attr,PTHREAD_CREATE_DETACHED); + + if (server_socket == -1) + { + printf(" Error: Server socket ID invalid!\n"); + return -1; + } + + while(1) //服务器端要一直运行 + { + //定义客户端的socket地址结构client_addr + struct sockaddr_in client_addr; + socklen_t length = sizeof(client_addr); + + //接受一个到server_socket代表的socket的一个连接 + //如果没有连接请求,就等待到有连接请求--这是accept函数的特性 + //accept函数返回一个新的socket,这个socket(new_server_socket)用于同连接到的客户的通信 + //new_server_socket代表了服务器和客户端之间的一个通信通道 + //accept函数把连接到的客户端信息填写到客户端的socket地址结构client_addr中 + new_server_socket = accept(server_socket,(struct sockaddr*)&client_addr, &length); + if (new_server_socket == -1) + { + printf("Error: Server Accept Failed!\n"); + return -1; + } + + printf("\nServer accept a new connect!\n"); + + err = 0; + if( (err = pthread_create(&child_thread, &child_thread_attr, TalkToClient_linux, (void*)new_server_socket)) != 0 ) + { + printf("Error: pthread_create Failed : %s\n",strerror(errno)); + return -1; + } + } +} + + +#else + + +ULONG __stdcall TalkToClient_windows(void* lpParameter) +{ + SOCKET new_server_socket = ((SOCKET)lpParameter); + char buffer[MAX_CMDLINE_LENGTH]; + char *pCmd = NULL; + int iRet = 0; + memset(buffer, 0, MAX_CMDLINE_LENGTH); + + //接收来自客户端的数据 + if (RecvDataFromClient_windows(new_server_socket, buffer, MAX_CMDLINE_LENGTH) > 0) + { + printf("Recv Data From Client: %s\n", buffer); + + iRet = ParseCmd((void*)new_server_socket, buffer); + if (iRet == -1) + { + printf("Error: Parse Cmd Failed!\n"); + closesocket(new_server_socket); + return -1; + } + + } + + + closesocket(new_server_socket); + return 0; +} + +int SendDataToClient_windows(SOCKET socketID, char *data, int iDataLen) +{ + int iRet = 0; + char szBuffer[MAX_BUFFER_SIZE]; + + if (data == NULL) + { + printf("Error: Send Data is NULL!\n"); + return -1; + } + + memset(szBuffer, 0, MAX_BUFFER_SIZE); + memcpy(szBuffer, data, iDataLen); + + //向客户端发送buffer中的数据 + iRet = send(socketID, szBuffer, iDataLen, 0); + if (iRet == SOCKET_ERROR) + { + printf("Error: Send error: %d!\n", GetLastError()); + return -1; + } + + return 0; +} + + +int RecvDataFromClient_windows(SOCKET socketID, char *buffer, int iBufLen) +{ + int length = 0; + + if (buffer == NULL) + { + printf("Error: Malloc Failed!\n"); + return -1; + } + + memset(buffer, 0, iBufLen); + //接收客户端发送来的信息到buffer中 + length = recv(socketID, buffer, iBufLen, 0); + if (length == 0 || length == SOCKET_ERROR) + { + printf("Client Exit!\n"); + return 0; + } + + return length; +} + +int InitServer_windows(SOCKET *pServer_socket) +{ + WSADATA Ws; + SOCKET server_socket; + int iRet = 0; + struct sockaddr_in server_addr; + + *pServer_socket = INVALID_SOCKET; + + //初始化windows socket + if ( WSAStartup(MAKEWORD(2,2), &Ws) != 0 ) + { + printf(" Error: Init Windows Socket Failed: %d \n", GetLastError()); + return -1; + } + + //设置一个socket地址结构server_addr,代表服务器internet地址, 端口 + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = htonl(INADDR_ANY); + server_addr.sin_port = htons(g_port); + memset(server_addr.sin_zero, 0x00, 8); + + server_socket = socket(AF_INET, SOCK_STREAM, 0); + if (server_socket == INVALID_SOCKET) + { + printf(" Error: Create Socket Failed: %d\n", GetLastError()); + closesocket(server_socket); + return -1; + } + + printf(" Create Socket Success!\n"); + + //把socket和socket地址结构联系起来 + iRet = bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr)); + if( iRet != 0) + { + printf(" Error: Server Bind Port : %d Failed!\n", g_port); + closesocket(server_socket); + return -1; + } + + printf(" Server Bind Port : %d Success!\n", g_port); + + //server_socket用于监听 + iRet = listen(server_socket, LENGTH_OF_LISTEN_QUEUE); + if (iRet != 0) + { + printf(" Error: Server Listen Failed!\n"); + closesocket(server_socket); + return -1; + } + + *pServer_socket = server_socket; + return 0; +} + + +int StartService_windows(SOCKET server_socket) +{ + //定义客户端的socket地址结构client_addr + struct sockaddr_in client_addr; + int length = sizeof(client_addr); + SOCKET new_server_socket; + HANDLE hThread = 0; + + if (server_socket == INVALID_SOCKET) + { + printf("Error: Server socket ID invalid!\n"); + return -1; + } + + while(1) //服务器端要一直运行 + { + //接受一个到server_socket代表的socket的一个连接 + //如果没有连接请求,就等待到有连接请求--这是accept函数的特性 + //accept函数返回一个新的socket,这个socket(new_server_socket)用于同连接到的客户的通信 + //new_server_socket代表了服务器和客户端之间的一个通信通道 + //accept函数把连接到的客户端信息填写到客户端的socket地址结构client_addr中 + new_server_socket = accept(server_socket, (struct sockaddr*)&client_addr, &length); + if (new_server_socket == INVALID_SOCKET) + { + printf("Error: Server Accept Failed!\n"); + return -1; + } + + printf("\nServer accept a new connect: %s\n", inet_ntoa(client_addr.sin_addr)); + + hThread = CreateThread(NULL, 0, TalkToClient_windows, (void*)new_server_socket, 0, NULL); + + if( hThread == 0) + { + printf("Error: CreateThread Failed\n"); + return -1; + } + + CloseHandle(hThread); + } +} + +#endif + + +int CopyFileToClient(void* socketID, char *pFile) +{ + FILE *fr = NULL; + int iRet = 0; + int iLen = 0; + int iTotal = 0; + char strBuffer[MAX_BUFFER_SIZE]; + + #ifdef IN_LINUX + int socket; + #else + SOCKET socket; + #endif + + #ifdef IN_LINUX + socket = (int)socketID; + if (socket == -1) + { + printf("Error: Socket ID Error!\n"); + return -1; + } + #else + socket = (SOCKET)socketID; + if (socket == INVALID_SOCKET) + { + printf("Error: Socket ID Error!\n"); + return -1; + } + #endif + + if (pFile == NULL) + { + printf("Error: File Name is NULL!\n"); + return -1; + } + + //打开文件 + fr = fopen(pFile, "rb"); + if(fr == NULL) + { + printf("Error: Open File : %s Failed!\n", pFile); + return -1; + } + + memset(strBuffer, 0, MAX_BUFFER_SIZE); + + //开始读文件的内容 + while ((iLen = fread(strBuffer, sizeof(char), MAX_BUFFER_SIZE-1, fr)) > 0) + { + #ifdef IN_LINUX + iRet = SendDataToClient_linux(socket, strBuffer, iLen); + #else + iRet = SendDataToClient_windows(socket, strBuffer, iLen); + #endif + + iTotal += iLen; + + if (iRet == -1) + { + printf("Error: Send Data To Client Failed!\n"); + fclose(fr); + return -1; + } + + memset(strBuffer, 0, MAX_BUFFER_SIZE); + + iRet = WaitRspFromClient(socketID); + + if (iRet != RET_OK) + { + printf("Error: Wait Rsp From Client Failed!\n"); + fclose(fr); + return -1; + } + } + + printf("Copy File Total length: %d\n", iTotal); + + fclose(fr); + return 0; +} + + +int RunSystemCmd(void* socketID, char *pCmd, int iWaitFlag) +{ + char buf[BUFSIZE]; + char cmd[MAX_CMDLINE_LENGTH]; +#ifdef IN_LINUX + int filename; +#else + FILE *filename; +#endif + int iRet = 0; + char strCmd[MAX_CMDLINE_LENGTH]; + + #ifdef IN_LINUX + int socket; + #else + SOCKET socket; + #endif + + #ifdef IN_LINUX + socket = (int)socketID; + if (socket == -1) + { + printf("Error: Socket ID Error!\n"); + return -1; + } + #else + socket = (SOCKET)socketID; + if (socket == INVALID_SOCKET) + { + printf("Error: Socket ID Error!\n"); + return -1; + } + #endif + + if (pCmd == NULL) + { + printf("Error: Cmd is NULL!\n"); + return -1; + } + + memset(strCmd, 0, MAX_CMDLINE_LENGTH); + strncpy(strCmd, pCmd, MAX_CMDLINE_LENGTH); + + if (iWaitFlag == CMD_ECHO) + { + memset(cmd,0,sizeof(cmd)); + strcat(cmd,strCmd); + strcat(cmd," 1>fileecho 2>&1"); + iRet = system(cmd); +#ifdef IN_LINUX + filename = open("fileecho",O_RDONLY ); +#else + filename = fopen("fileecho","r"); +#endif + if (filename == NULL) + { + iRet = -1; + return 0; + } +#ifdef IN_LINUX + if (read(filename, buf, BUFSIZE) == NULL) +#else + if (fread(buf, 1, BUFSIZE, filename) == NULL) +#endif + { + iRet = -1; + return 0; + } +#ifdef IN_LINUX + system("rm fileecho"); +#endif + printf("%s\n", buf); + printf("%d\n", strlen(buf)); + send(socketID, buf, BUFSIZE, 0); + if (iRet == 0) + { + iRet == CMD_ECHO; + } + } + else + { + //执行系统命令 + iRet = thr_fn(strCmd); + + //需要给客户端应答 + if (iWaitFlag == CMD_FALG_WAIT) + { + if (iRet == 0) + { + printf("Run %s Success!\n", pCmd); + iRet = SendRspToClient(socketID, RSP_OK); + } + else + { + iRet = SendRspToClient(socketID, RSP_FAILED); + } + + //返回执行结果 + if ( iRet == -1) + { + printf("Error: Send Rsp To Client Failed!\n"); + } + + printf("Send Rsp To Client Success!\n"); + + } + } + + return 0; +} + +int RecvFileFromClient(void* socketID, char *pFile) +{ + char strBuffer[MAX_BUFFER_SIZE]; + FILE *fr = NULL; + int iRet = 0; + int iTotal = 0; + + #ifdef IN_LINUX + int socket; + #else + SOCKET socket; + #endif + + #ifdef IN_LINUX + socket = (int)socketID; + if (socket == -1) + { + printf("Error: Socket ID Error!\n"); + return -1; + } + #else + socket = (SOCKET)socketID; + if (socket == INVALID_SOCKET) + { + printf("Error: Socket ID Error!\n"); + return -1; + } + #endif + + if (pFile == NULL) + { + printf("Error: FileName is NULL!\n"); + return -1; + } + + memset(strBuffer, 0, MAX_BUFFER_SIZE); + + //打开准备写的文件 + fr = fopen(pFile, "wb"); + if(fr == NULL) + { + printf("Error: Open File :%s Failed!\n", pFile); + + //告诉客户端:服务端出错,不用发送文件了 + if (SendRspToClient(socketID, RSP_FAILED) == -1) + { + printf("Error: Send Rsp To Client Failed!\n"); + } + + return -1; + } + + //告诉客户端:服务端准备好了,可以发送文件了 + if (SendRspToClient(socketID, RSP_OK) == -1) + { + printf("Error: Send Rsp To Client Failed!\n"); + } + + printf("Send Rsp To Client Success!\n"); + + //接收来自客户端的文件数据 + #ifdef IN_LINUX + iRet = RecvDataFromClient_linux(socket, strBuffer, MAX_BUFFER_SIZE); + #else + iRet = RecvDataFromClient_windows(socket, strBuffer, MAX_BUFFER_SIZE); + #endif + + while ( iRet > 0) + { + iTotal += iRet; + + //发送收到文件数据应答给客户端 + if (SendRspToClient(socketID, RSP_OK) == -1) + { + printf("Error: Send Rsp To Client Failed!\n"); + fclose(fr); + return -1; + } + + if (fwrite(strBuffer, sizeof(char), iRet, fr) != iRet) + { + printf("Error: Write File Failed : %s\n", pFile); + fclose(fr); + return -1; + } + + memset(strBuffer, 0, MAX_BUFFER_SIZE); + + #ifdef IN_LINUX + iRet = RecvDataFromClient_linux(socket, strBuffer, MAX_BUFFER_SIZE); + #else + iRet = RecvDataFromClient_windows(socket, strBuffer, MAX_BUFFER_SIZE); + #endif + } + + printf("Send File Length: %d\n", iTotal); + + fclose(fr); + return 0; +} + + +int ParseCmd(void* socketID, char *data) +{ + char *pCmdType = NULL; + char strCmd[MAX_CMDLINE_LENGTH]; + char *pPara = NULL; + int iRet = 0; + + memset(strCmd, 0, MAX_CMDLINE_LENGTH); + strncpy(strCmd, data, MAX_CMDLINE_LENGTH); + + printf("Recv CMD: %s\n", strCmd); + + //解析命令类型 + pCmdType = strtok(strCmd, "["); + if (pCmdType == NULL) + { + printf("Error: Cmdlist para %s error!\n", strCmd); + return -1; + } + + strTerm(pCmdType); + + //解析命令 + pPara = strtok(NULL, "]"); + if (pPara == NULL) + { + printf("Error: Cmdlist para %s error!\n", strCmd); + return -1; + } + strTerm(pPara); + + //为拷贝文件类型的命令 + if (strcmp(COPYFILE, pCmdType) == 0) + { + if (CopyFileToClient(socketID, pPara) == -1) + { + printf("Error: Copy File To Client Failed!\n"); + return -1; + } + + printf("Copy File To Client Success!\n"); + } + //为客户端发送文件类型的命令 + else if (strcmp(SENDFILE, pCmdType) == 0) + { + if (RecvFileFromClient(socketID, pPara) == -1) + { + printf("Error: Receive File From Client Failed!\n"); + return -1; + } + + printf("Receive File From Client Success!\n"); + + } + else if(strcmp(CMDECHO,pCmdType) == 0) + { + + if (RunSystemCmd(socketID, pPara, CMD_ECHO) == -1) + { + return -1; + } + } + //为系统命令 + else if(strcmp(CMD, pCmdType) == 0) + { + //执行命令 + if (RunSystemCmd(socketID, pPara, CMD_FALG_NOWAIT) == -1) + { + return -1; + } + } + //为系统命令,并等待执行完系统命令再给客户端应答 + else if(strcmp(CMDWAIT, pCmdType) == 0) + { + //执行命令 + if (RunSystemCmd(socketID, pPara, CMD_FALG_WAIT) == -1) + { + return -1; + } + } + //为查看某个进程是否存在 + else if(strcmp(FINDPROC, pCmdType) == 0) + { + if (FindProcess(socketID, pPara) == -1) + { + printf("Error: Find Process Failed!\n"); + return -1; + } + } + else + { + printf("Error: No Support : %s\n", pCmdType); + return -1; + } + + return 0; +} + +int FindProcess_windows(char *pProcessName) +{ + char szCmd[MAX_BUFFER_SIZE]; + char *pProc = NULL; + FILE *fr = NULL; + + memset(szCmd, 0, MAX_BUFFER_SIZE); + + //构造查询进程系统命令 + strcpy(szCmd, "tasklist /FI \"IMAGENAME eq "); + strcat(szCmd, pProcessName); + strcat(szCmd, "\" /NH>tmp_process.txt"); + + //执行命令 + thr_fn(szCmd); + + fr = fopen("tmp_process.txt", "r"); + if (fr == NULL) + { + printf("Error: Open file tmp_process.txt Failed!\n"); + return -1; + } + + memset(szCmd, 0, MAX_BUFFER_SIZE); + + while (fgets(szCmd, MAX_BUFFER_SIZE, fr) != NULL) + { + if (strlen(szCmd) > 0) + { + pProc = strtok(szCmd, " "); + strTerm(pProc); + + //找到了 + if (strcmp(pProcessName, pProc) == 0) + { + fclose(fr); + return RET_OK; + } + } + + memset(szCmd, 0, MAX_BUFFER_SIZE); + } + + fclose(fr); + return RET_FAILED; +} + + +int FindProcess(void* socketID, char *data) +{ + int iRet = 0; + + #ifdef IN_LINUX + iRet = FindProcess_linux(data); + #else + iRet = FindProcess_windows(data); + #endif + + if (iRet == -1) + { + return -1; + } + + if (iRet == RET_OK) + { + iRet = SendRspToClient(socketID, RSP_OK); + } + else + { + iRet = SendRspToClient(socketID, RSP_FAILED); + } + + if (iRet == -1) + { + printf("Error: Send Rsp To Client Failed!\n"); + return -1; + } + + printf("Send Rsp To Client Success!\n"); + + return 0; +} + +int SendRspToClient(void* socketID, char *result) +{ + int iRet = 0; + char strRsp[MAX_RSP_LENGTH]; + + #ifdef IN_LINUX + int socket; + #else + SOCKET socket; + #endif + + #ifdef IN_LINUX + socket = (int)socketID; + if (socket == -1) + { + printf("Error: Socket ID Error!\n"); + return -1; + } + #else + socket = (SOCKET)socketID; + if (socket == INVALID_SOCKET) + { + printf("Error: Socket ID Error!\n"); + return -1; + } + #endif + + if (result == NULL) + { + printf("Error: Send data is NULL!\n"); + return -1; + } + + memset(strRsp, 0, MAX_RSP_LENGTH); + + //构造应答消息 + strcpy(strRsp, RSP); + strcat(strRsp, "#"); + strcat(strRsp, result); + + #ifdef IN_LINUX + iRet = SendDataToClient_linux(socket, strRsp, strlen(strRsp)); + #else + iRet = SendDataToClient_windows(socket, strRsp, strlen(strRsp)); + #endif + + if (iRet == -1) + { + printf("Error: Send Data To Client Failed!\n"); + return -1; + } + + return 0; +} + + +int WaitRspFromClient(void* socketID) +{ + char strRsp[MAX_RSP_LENGTH]; + int iRet; + + #ifdef IN_LINUX + int socket; + #else + SOCKET socket; + #endif + + #ifdef IN_LINUX + socket = (int)socketID; + if (socket == -1) + { + printf("Error: Socket ID Error!\n"); + return -1; + } + #else + socket = (SOCKET)socketID; + if (socket == INVALID_SOCKET) + { + printf("Error: Socket ID Error!\n"); + return -1; + } + #endif + + memset(strRsp, 0, MAX_RSP_LENGTH); + + //接收来自客户端的应答 + #ifdef IN_LINUX + iRet = RecvDataFromClient_linux(socket, strRsp, MAX_RSP_LENGTH); + #else + iRet = RecvDataFromClient_windows(socket, strRsp, MAX_RSP_LENGTH); + #endif + + if (iRet <= 0) + { + return -1; + } + + iRet = ParseRsp(strRsp); + + return iRet; +} + +int ParseRsp(char* pRsp) +{ + char *pPara = NULL; + char strRsp[MAX_RSP_LENGTH]; + + memset(strRsp, 0, MAX_RSP_LENGTH); + + if (strRsp == NULL) + { + printf("Error: strRsp is NULL!\n"); + return -1; + } + + strncpy(strRsp, pRsp, MAX_RSP_LENGTH); + + pPara = strtok(strRsp, "#"); + if (pRsp == NULL) + { + printf("Error: Rsp %s error!\n", pRsp); + return -1; + } + + pPara = strtok(NULL, "#"); + if (pRsp == NULL) + { + printf("Error: Rsp %s error!\n", pRsp); + return -1; + } + + // 服务端应答消息为OK + if (strcmp(pPara, RSP_OK) == 0) + { + return RET_OK; + } + else if (strcmp(pPara, RSP_FAILED) == 0) + { + return RET_FAILED; + } + + return -1; + +} + +int Init() +{ + //从配置文件中获取端口 + if (SetServerPort() == -1) + { + return -1; + } + + return 0; +} + + +int Run() +{ + #ifdef IN_LINUX + int server_socket = -1; + int iRet = 0; + + //初始化socket + server_socket = InitServer_linux(); + if (server_socket == -1) + { + printf(" Error: InitServer Failed! Exit...\n"); + return -1; + } + + printf(" InitServer Success!\n"); + printf(" Server is running...\n\n"); + printf("**************************************\n"); + + iRet = StartService_linux(server_socket); + if (iRet == -1) + { + printf(" Error: StartService Failed! Exit...\n"); + close(server_socket); + return -1; + } + + //关闭监听用的socket + close(server_socket); + + #else + + SOCKET server_socket; + int iRet = 0; + + //初始化socket + iRet = InitServer_windows(&server_socket); + if (iRet == -1) + { + printf(" Error: InitServer Failed! Exit...\n"); + return -1; + } + + printf(" InitServer Success!\n"); + printf(" Server is running...\n"); + printf("**************************************\n"); + + iRet = StartService_windows(server_socket); + if (iRet == -1) + { + printf("Error: StartService Failed! Exit...\n"); + closesocket(server_socket); + WSACleanup(); + return -1; + } + + //关闭监听用的socket + closesocket(server_socket); + WSACleanup(); + + #endif + + return 0; +} + + +int main(int argc, char **argv) +{ + int iRet = 0; + + printf("======================================\n"); + printf(" server-tool\n"); + printf(" Version: %s\n", VERSION); + printf("======================================\n"); + + //初始化 + iRet = Init(); + if (iRet == -1) + { + printf(" Error: Init Failed! Server Exiting...\n"); + + #ifndef IN_LINUX + pritfExit(); + #endif + + return -1; + } + + iRet = Run(); + + + return 0; +} diff --git a/needgcov-1.0/upload.sh b/needgcov-1.0/upload.sh new file mode 100644 index 0000000..af5dd5d --- /dev/null +++ b/needgcov-1.0/upload.sh @@ -0,0 +1,87 @@ +#!/bin/bash +# Author: yhon +# Copyright Huawei Technologies Co., Ltd. 2010-2018. All rights reserved. +set -e + +cpath=/opt/custom_build_tool +build_dir=$(rpm --eval %{_builddir}) +#if find /home/abuild/rpmbuild/RPMS/ -name "*.rpm" |egrep 'i386$|i586.rpm$|i686.rpm$' &> /dev/null; then +# echo "xdfgcov: Don't need i386 i586 i686" +# exit 0 +#fi + +ip="10.175.100.158" +user="root" +password="huawei" +http_dir="/srv/www/htdocs/gcov" +project=$(rpm --eval %{_project}) +repo=$(rpm --eval %{_repository}) +arch=$(rpm --eval %{_build_cpu}) +release_dir="${http_dir}/${project}/${repo}/${arch}" + +cd "${build_dir}" +if ! find ./ | grep 'gcno$' &> /dev/null; then + echo "xdf: No gcno file!!!" + exit 0 +fi +module=$(find ./ | grep 'gcno$' | head -1 | awk -F '/' '{print $2}') + +if [ -z "${module}" ]; then + echo "xdf: has some problem" + exit 1 + #exit 0 +fi +tar_name=${module} +if find /home/abuild/rpmbuild/RPMS/ | grep mirro ; then + tar_name="${module}-mirror" + mv "${module}" "${tar_name}" + module="${module}-mirror" +fi +if find /home/abuild/rpmbuild/RPMS/ | grep openvswitch | grep kmod ; then + tar_name="${module}-kmod" +fi +tar --exclude *.o --exclude *.so -czf "${tar_name}".tar.gz "${module}" + +#chmod 777 /opt/custom_build_tool/client-tool-linux +/opt/custom_build_tool/client-tool-linux "$ip"#4123#cmdwait["mkdir -p $release_dir"] +/opt/custom_build_tool/client-tool-linux "$ip"#4123#sendfile["${module}.tar.gz,$release_dir/${module}.tar.gz"] + +cd - +exit 0 + + + + +echo "xdf: cp gcno and src to $release_dir" +SSH_CMD="mkdir -p $release_dir" +sh "$cpath"/sshcmd.sh -c "$SSH_CMD" -m "$ip" -u "$user" -p "$password" +if [ $? -ne 0 ]; then + echo "Failed in sshcmd.sh on mkdir -p $release_dir" + exit 1 + #exit 0 +fi + +if find ./ | grep 'gcno$' &> /dev/null; then + echo "xdf: No gcno file!!!" + exit 0 +fi +cd $build_dir +MODULE=$(find ./ | grep 'gcno$' | head -1 | awk -F '/' '{print $2}') + +if [ -z "${MODULE}" ]; then + echo "xdf: has some problem" + exit 1 + #exit 0 +fi +tar --exclude *.o --exclude *.so -czf "${MODULE}".tar.gz "${MODULE}" +cd - +echo "xdf: $build_dir/$MODULE gcov done" +sh "$cpath"/sshscp.sh -p "$password" -s "$build_dir/${MODULE}.tar.gz" -d "root@$ip:$release_dir" -r +if [ $? -ne 0 ]; then + echo "Failed in sshscp.sh $build_dir/$MODULE" + exit 1 + #exit 0 +fi + +echo "xdf:Gcov successful" +exit 0 diff --git a/needgcov-1.0/upload_build.sh b/needgcov-1.0/upload_build.sh new file mode 100644 index 0000000..c833dee --- /dev/null +++ b/needgcov-1.0/upload_build.sh @@ -0,0 +1,26 @@ +#!/bin/bash +# Author: yhon +# Copyright Huawei Technologies Co., Ltd. 2010-2018. All rights reserved. +set -e + +rpm_name=$1 + +cpath=/opt/custom_build_tool +build_dir=$(rpm --eval %{_builddir}) + +ip="10.175.100.158" +user="root" +password="huawei" +http_dir="/srv/www/htdocs/rpmbuild_middleware" +project=$(rpm --eval %{_project}) +repo=$(rpm --eval %{_repository}) +arch=$(rpm --eval %{_build_cpu}) +release_dir="${http_dir}/${project}/${repo}/${arch}" +release_dir="${http_dir}/${project}/${repo}/${arch}" + +tar_name=$(grep "/rpmbuild/SOURCES" /.build.command | awk -F 'rpmbuild/SOURCES/' '{print $2}' |awk -F '\\.spec' '{print $1}') + +tar -I pbzip2 -cf "${tar_name}".tar.bz2 "${build_dir}" + +/opt/custom_build_tool/client-tool-linux "$ip"#4123#cmdwait["mkdir -p $release_dir"] +/opt/custom_build_tool/client-tool-linux "$ip"#4123#sendfile["${tar_name}.tar.bz2,$release_dir/${tar_name}.tar.bz2"]