freebsd network program compilation - unix

I am trying to compile some simple networking programs on freebsd 8 and running into compilation issues. I am creating a simple client-server programs but no function or structure from networking is not getting compiled.
For eg. I use standard socket() call to create a socket but I run into an error "Called object socket is not a function."
If I remove the network code then my toy program compiles. For simplicity I have just put a simple example which does not compile. :
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void main(){
int socket = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
printf("Could create sockets without any issues.\n");
}
I compiled it with "cc toy_prog.c -lc" and gave me the mentioned error.

A very simple error. You have defined a local variable with the same name as the external function you are trying to call (socket). Try the following and you will get the same error:
int f()
{
return 0;
}
void main()
{
int f = f();
}

Related

QApplication recover from segmentation fault

I want to be able to recover from a Segmentation Fault in MyApplication by catching the SIGSEGV and restarting QApplication. So for testing purposes I'm injecting a segmentation fault in my code.
The issue is that the signal handler that catches the SIGSEGV is getting a non-stop stream of SIGSEGVs. At first I thought it was the while loop in my main but it still happens even though I comment out the while loop. So my questions are simple: Is it even possible to recover from a Segmentation Fault in Qt? Why am I getting rolling SIGSEGVs non-stop?
#include <QApplication>
#include <QDebug>
#include "MyApplication.h"
#include <initializer_list>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#define RESTART_CODE 1000
void catchUnixSignals(std::initializer_list<int> quitSignals)
{
auto handler = [](int sig) -> void
{
if (sig == SIGSEGV)
{
QCoreApplication::exit(RESTART_CODE);
}
else
{
QCoreApplication::quit();
}
};
sigset_t blocking_mask;
sigemptyset(&blocking_mask);
for (auto sig : quitSignals)
sigaddset(&blocking_mask, sig);
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_mask = blocking_mask;
sa.sa_flags = 0;
for (auto sig : quitSignals)
sigaction(sig, &sa, nullptr);
}
int main(int argc, char *argv[])
{
catchUnixSignals({SIGSEGV, SIGQUIT, SIGINT, SIGTERM, SIGHUP, SIGKILL});
int i = 0;
do
{
QApplication app(argc, argv);
MyApp myapp;
MyApp.start();
app.exec();
if (app.exec() != RESTART_CODE) break;
} while(1);
return 0;
}
This does not directly answers your question, but is another way to achieve similar behavior.
To recover from a segmentation fault, an option is to use a watchdog, i.e. another independent process that checks the status of your main software and restarts it when needed.
When you start your software, you create another process that runs a 2nd software, the watchdog. Ensure to start it in "detached" mode to avoid that it gets closed if your main software crashes.
In the watchdog, frequently call "tasklist" on Windows or "ps" or "top" on Linux and parse the output to check whether your software is still running. OR, use a UDP or TCP port to communicate between the main software and the watchdog, to tell the watchdog that the main software is still running well.
In the watchdog, if the main software is no longer running, restart the main software process (also in detached).
CAUTION: You need to manage the case where the main software is exited correctly. In that case the main software should either kill the watchdog itself when exiting normally (calling "kill" on the pid), or send it a message so that the watchdog exits as well.

QtDBus Simply Example With PowerManager

I'm trying to use QtDbus to communicate with interface provided by PowerManager in my system. My goal is very simple. I will be writing code which causes my system to hibernate using DBus interface.
So, I installed d-feet application to see what interfaces DBus is available on my system, and what I saw:
As we see, I have a few interfaces and methods from which I can choose something. My choice is Hibernate(), from interface org.freedesktop.PowerManagment
In this goal I prepared some extremely simple code to only understand mechanism. I of course used Qt library:
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtCore/QStringList>
#include <QtDBus/QtDBus>
#include <QDBusInterface>
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
if (!QDBusConnection::sessionBus().isConnected()) {
fprintf(stderr, "Cannot connect to the D-Bus session bus.\n"
"To start it, run:\n"
"\teval `dbus-launch --auto-syntax`\n");
return 1;
}
QDBusInterface iface("org.freedesktop.PowerManagement" ,"/" , "" , QDBusConnection::sessionBus());
if(iface.isValid())
{
qDebug() << "Is good";
QDBusReply<QString> reply = iface.call("Methods" , "Hibernate");
if(reply.isValid())
{
qDebug() << "Hibernate by by " << qPrintable(reply.value());
}
qDebug() << "some error " << qPrintable(reply.error().message());
}
return 0;
}
Unfortunately I get error in my terminal:
Is good
some error Method "Methods" with signature "s" on interface "(null)" doesn't exist
So please tell me what's wrong with this code? I am sure that I forgot some arguments in function QDBusInterface::call() but what ?
When creating interface you have to specify correct interface, path, service. So that's why your iface object should be created like this:
QDBusInterface iface("org.freedesktop.PowerManagement", // from list on left
"/org/freedesktop/PowerManagement", // from first line of screenshot
"org.freedesktop.PowerManagement", // from above Methods
QDBusConnection::sessionBus());
Moreover, when calling a method you need to use it's name and arguments (if any):
iface.call("Hibernate");
And Hibernate() doesn't have an output argument, so you have to use QDBusReply<void> and you can't check for .value()
QDBusReply<void> reply = iface.call("Hibernate");
if(reply.isValid())
{
// reply.value() is not valid here
}

How to verify a network device has an IP address in VxWorks

I am trying to verify the Ethernet devices on a device are working correctly. I'm running the command:
ifconfig("interfaceName dhcp")
for each ethernet interface.
What I would also like to do is verify that each device got an ip address as well. I know I can just run 'ifconfig' by itself and look at the output, but I'm writing automated test code. So is there a function that can return the ip address of a specific interface in VxWorks?
I believe ifAddrGet() in ifLib.h may be what you're looking for. The first argument takes an interface name and the second argument takes a buffer, into which the address will be returned.
I haven't tested the following on an actual target, but it should be a start toward what you need:
#include <stdlib.h>
#include <stdio.h>
#include "inetLib.h"
#include "ifLib.h"
void print_if_address (void);
void print_if_address ()
{
char if_name[] = "dhcp";
char ip_address[INET_ADDR_LEN] = {0};
ifAddrGet (if_name, ip_address);
printf ("%s\n", ip_address);
}

Getting runtime architecture information in Qt

How might I discover, at runtime, using Qt, if a user's system is Win 7-32 or Win7-64?
There is no way of doing it exclusively using Qt, AFAIK. Below is how you can do it.
#include <windows.h>
#include <tchar.h>
#include <QtCore/QSysInfo>
typedef enum { Win_64, Win_32, Error, Other } OsType;
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
OsType checkOS() {
#ifndef Q_OS_WIN32
return Other;
#else
// An application compiled for 64 bits can only run on a 64 bit os,
// so no need to check any further.
if (QSysInfo::WordSize == 64) return Win7_64;
// A 32 bit application may be running on a 64 bit OS.
BOOL is64 = FALSE;
// IsWow64Process may not be available in kernel32 on all Windows versions, so we bind to it
// at runtime.
LPFN_ISWOW64PROCESS fnIsWow64Process;
fnIsWow64Process = (LPFN_ISWOW64PROCESS)
GetProcAddress(GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
// No way it's a 64 bit OS if it doesn't have this API.
if (fnIsWow64Process == NULL) return Win_32;
// Note that GetCurrentProcess() can't fail.
if (!IsWow64Process(GetCurrentProcess(), &is64)) return Error; // The check has failed.
return is64 ? Win_64 : Win_32;
#endif
}
For the case of Qt 5 you can use QSysInfo static functions like prettyProductName() and currentCpuArchitecture().

Linking CIL with native code

In C++/CLI the following is sample code that links native and managed code within the same file.
#include "stdafx.h"
#pragma unmanaged
__declspec( dllexport ) void func2()
{
//native code goes here
}
#pragma managed
void func_clr()
{
func2(); //managed code calls native
}
#pragma unmanaged
__declspec( dllexport ) void func()
{
func_clr(); //native calls managed
}
#pragma managed
I am trying to experiment in getting other languages to link with managed code. I can compile other languages with the available tools and can compile CIL with ilasm. Ilasm produces the final .dll/.exe directly and I cant figure out a way to link in .obj files from other compilers.

Resources