Skip to content
Snippets Groups Projects
Commit 49f47315 authored by Lais Frigério's avatar Lais Frigério
Browse files

SCRUM#87 - Get distro info on windows

parent d9700c81
No related branches found
No related tags found
2 merge requests!103Merge dev -> master,!56Issue/89
Pipeline #
......@@ -4,8 +4,11 @@
#include <string>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#ifdef __unix__
#include <unistd.h>
#endif
void save_PID();
int kill_agent(int);
#pragma once
#include <sstream>
#include <agent/open_file.h>
#include <agent/windows_helper.h>
#ifdef WIN32
#include <windows.h>
#include <VersionHelpers.h>
......@@ -11,7 +13,8 @@
* @brief Get info from Ubuntu, debian, Linux Comunicações, Linux Educacional, fedora and OpenSUSE distro; windows use dtwinver
*/
std::string get_distro();
#ifdef WIN32
std::string get_winver();
#endif // WIN32
#ifdef __unix__
std::string get_distro_linux();
#else
std::string get_distro_win();
#endif // __unix__
......@@ -19,10 +19,11 @@
* USA.
*/
#include "agent/agent_uninstall.h"
#include <agent/agent_uninstall.h>
#include <string>
extern std::string path_to_agent_tmp;
#ifdef __unix__
void save_PID() {
std::ofstream ofs;
int pid = ::getpid();
......@@ -42,3 +43,4 @@ int kill_agent(int sig) {
ifs.close();
return(ret);
}
#endif
......@@ -64,7 +64,7 @@ bool Curl::downloadAgent(std::string url, std::fstream* logFile,
#ifdef __unix__
FILE* fp = fopen(tmp_dot_run.c_str(), "wb");
#elif _WIN32
FILE* fp = fopen((const char*)test_file.c_str(), "wb");
FILE* fp = fopen((const char*)tmp_dot_run.c_str(), "wb");
#endif
if (!fp) {
......
......@@ -149,34 +149,40 @@ std::string get_distro() {
#ifdef WIN32
/** Get Windows version */
std::string get_winver() {
std::string version_name;
if (!IsWindowsXPOrGreater())
return version_name.assign("unknown");
if (!IsWindowsXPSP1OrGreater())
std::string get_distro_win() {
std::string version_name, line, cmd;
if (IsWindowsVistaOrGreater()) {
try {
cmd = exec("systeminfo");
} catch (std::string err) {
throw std::string("unknown distro");
}
const std::string key = "Nome do sistema operacional:";
std::size_t found;
found = cmd.find(key);
std::istringstream iss(cmd);
while (std::getline(iss, line)) {
found = line.find(key);
if (found != std::string::npos) {
return line.substr(found+key.length()+1,
// remove key
line.length()-(found+key.length()+1));
}
}
} else if (!IsWindowsXPSP1OrGreater()) {
return version_name.assign("Windows XP");
if (!IsWindowsXPSP2OrGreater())
} else if (!IsWindowsXPSP2OrGreater()) {
return version_name.assign("Windows XP SP1");
if (!IsWindowsXPSP3OrGreater())
} else if (!IsWindowsXPSP3OrGreater()) {
return version_name.assign("Windows XP SP2");
if (!IsWindowsVistaOrGreater())
} else if (!IsWindowsVistaOrGreater()) {
return version_name.assign("Windows XP SP3");
if (!IsWindowsVistaSP1OrGreater())
return version_name.assign("Windows Vista");
if (!IsWindowsVistaSP2OrGreater())
return version_name.assign("Windows Vista SP1");
if (!IsWindows7OrGreater())
return version_name.assign("Windows Vista SP2");
if (!IsWindows7SP1OrGreater())
return version_name.assign("Windows 7");
if (!IsWindows8OrGreater())
return version_name.assign("Windows 7 SP1");
if (!IsWindows8Point1OrGreater())
return version_name.assign("Windows 8");
if (!IsWindows10OrGreater())
return version_name.assign("Windows 8.10");
else
return version_name.assign("Windows 10");
} else {
throw std::string("unknown distro");
}
throw std::string("unknown distro");
}
#endif // WIN32
......
......@@ -22,7 +22,7 @@
/** Get processor model info from /proc/cpuinfo file */
std::string get_processor_model() {
std::string line;
std::string line;
#ifdef __unix__
std::ifstream cpuinfo_file;
if (open_file("/proc/cpuinfo", cpuinfo_file)) {
......
......@@ -59,9 +59,9 @@ Json::Value get_inventory() {
try {
#ifdef WIN32
// use dtwinver
inv["data_inventory"]["os_distro"] = get_winver();
inv["data_inventory"]["os_distro"] = get_distro_win();
#else
inv["data_inventory"]["os_distro"] = get_distro();
inv["data_inventory"]["os_distro"] = get_distro_linux();
#endif
}
catch ( std::string err ) {
......
......@@ -102,12 +102,14 @@ int parse_command_line(int ac, char* av[], int *increment, int *lim,
*print = false;
}
if (vm.count("uninstall")) {
// remove setup to run on boot
kill_agent(SIGTERM);
fs::remove_all(path_to_agent + "agentC3SL");
return (1);
}
#ifdef __unix__
if (vm.count("uninstall")) {
// remove setup to run on boot
kill_agent(SIGTERM);
fs::remove_all(path_to_agent + "agentC3SL");
return (1);
}
#endif
return(0);
}
......@@ -133,13 +135,16 @@ int main(int argc, char* argv[]) {
return (0);
}
#ifdef __unix__
if (fs::exists(path_to_agent_tmp + "agent.pid")) {
if (kill_agent(0) == 0) {
std::cout << "ERROR: an agent is already running\n";
return (2);
}
}
save_PID();
#endif
int i = 0;
while (i < lim) {
......
/* Copyright (C) 2004-2017 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of simmc-agent
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
#include <agent\windows_helper.h>
/** Execute command and return the output to a string */
......@@ -11,4 +31,4 @@ std::string exec(const char* cmd) {
result += buffer.data();
}
return result;
}
\ No newline at end of file
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment