sdf
This commit is contained in:
parent
5872d5a21a
commit
29843d6a9a
77
main.c
77
main.c
@ -3,20 +3,31 @@
|
||||
#include<curl/curl.h>
|
||||
#include<string.h>
|
||||
#include<dbus/dbus.h>
|
||||
#include<stdbool.h>
|
||||
|
||||
int gotify_message(char *,char *,char *);
|
||||
int systemdCall(DBusConnection *,char *, char *);
|
||||
int checkIsActive(DBusConnection *connection, char *service_name);
|
||||
int systemdUnitCall(DBusConnection *connection,char *target,char *method,char *param,char **result);
|
||||
int checkState(char *ServiceName,char *result);
|
||||
/*
|
||||
*sytemctl show gotify-server | grep PID
|
||||
*sudo busctl tree org.freedesktop.systemd1 > get object paths
|
||||
* */
|
||||
typedef struct service
|
||||
{
|
||||
char *name;
|
||||
char *targetPath;
|
||||
bool alert;
|
||||
} Service;
|
||||
|
||||
int main(void)
|
||||
int main(int argc,char *argv[])
|
||||
{
|
||||
unsigned long uid;
|
||||
int ret,i;
|
||||
|
||||
Service serviceSys;
|
||||
|
||||
char **objects = NULL;
|
||||
dbus_bool_t allow_user=0;
|
||||
DBusConnection *connection=NULL;
|
||||
@ -24,14 +35,14 @@ int main(void)
|
||||
char *result=NULL;
|
||||
|
||||
dbus_error_init(&error);
|
||||
connection = dbus_bus_get(DBUS_BUS_SESSION,&error);//DBUS_BUS_SYSTEM
|
||||
connection = dbus_bus_get(DBUS_BUS_SYSTEM,&error);//DBUS_BUS_SYSTEM DBUS_BUS_SESSION
|
||||
if(dbus_error_is_set(&error))
|
||||
{
|
||||
fprintf(stderr,"%s",error.message);
|
||||
return -1;
|
||||
}
|
||||
printf("This is my unique dbus name: %s\n",dbus_bus_get_unique_name(connection));
|
||||
|
||||
/*
|
||||
ret = dbus_bus_request_name(connection,"test.method.client",DBUS_NAME_FLAG_REPLACE_EXISTING,&error);
|
||||
if(dbus_error_is_set(&error))
|
||||
{
|
||||
@ -42,7 +53,7 @@ int main(void)
|
||||
{
|
||||
printf("name requested from dbus: %d\n",ret);
|
||||
}
|
||||
|
||||
*/
|
||||
dbus_connection_set_allow_anonymous(connection,allow_user);
|
||||
printf("user auth dbus requ: %d\n",allow_user);
|
||||
if(!dbus_connection_list_registered(connection,"/",&objects))
|
||||
@ -62,21 +73,53 @@ int main(void)
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("NO userID set in dbus session: %d\n",ret);
|
||||
printf("NO userID set in dbus session, servers normali do not authenthicate against users/slaves: %d\n",ret);
|
||||
}
|
||||
|
||||
//org/freedesktop/systemd1
|
||||
//systemdCall(connection,"GetUnit","timers.target");
|
||||
//systemdCall(connection,"ListUnits",NULL);
|
||||
systemdCall(connection,"ListUnits",NULL);
|
||||
printf("&result:%p",&result);
|
||||
systemdUnitCall(connection,"/org/freedesktop/systemd1/unit/timers_2etarget","Get","SubState",&result);
|
||||
printf("\n\nresult systemdUnitCall: %s\n",result);
|
||||
systemdUnitCall(connection,"/org/freedesktop/systemd1/unit/mullvad_2ddaemon_2eservice","Get","SubState",&result);
|
||||
checkState("mullvar.service",result);
|
||||
systemdUnitCall(connection,"/org/freedesktop/systemd1/unit/sysinit_2etarget","Get","SubState",&result);
|
||||
printf("\n\nresult systemdUnitCall: %s\n",result);
|
||||
checkState("sysinit.target",result);
|
||||
gotify_message("https://5ccppi.org:4433/message?token=ApPFbwayM5zDXf3","anothertitle","anothermessage");
|
||||
|
||||
char userinput[128];
|
||||
while(true)
|
||||
{
|
||||
scanf("%s",userinput);
|
||||
printf("\nuserinput: %s",userinput);
|
||||
systemdUnitCall(connection,userinput,"Get","SubState",&result);
|
||||
checkState(userinput,result);
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
int checkState(char *ServiceName,char *result)
|
||||
{
|
||||
int i=0;
|
||||
char *error_strings[]={"dead","inactive","exited","teeeeeeeeeeeeessssst"};
|
||||
printf("\n\nresult systemdUnitCall: %s\n",result);
|
||||
for(i=0;i<(sizeof(error_strings)/sizeof(error_strings[0]));i++)
|
||||
{
|
||||
printf("%s %d\n",error_strings[i],sizeof(error_strings)/sizeof(error_strings[0]));
|
||||
if(strcmp(result,error_strings[i])==0)
|
||||
{
|
||||
gotify_message("https://5ccppi.org:4433/message?token=AnV26OM_KjPdvY9",ServiceName,"dead!");
|
||||
printf("!!system unit died!! Alert gotify server!\n");
|
||||
}
|
||||
else if(strcmp(result,"active")==0)
|
||||
{
|
||||
printf("!!service seems okay!!\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int gotify_message(char *url,char *title,char *message)
|
||||
{
|
||||
@ -87,7 +130,7 @@ int gotify_message(char *url,char *title,char *message)
|
||||
|
||||
curl = curl_easy_init();
|
||||
|
||||
ret = snprintf(post_buffer,sizeof(post_buffer),"title=%s&message=%s",title,message);
|
||||
ret = snprintf(post_buffer,sizeof(post_buffer),"title=%s&message=%s&priority=6",title,message);
|
||||
if(ret < sizeof(post_buffer))
|
||||
{
|
||||
printf("%s %d",post_buffer,ret);
|
||||
@ -239,6 +282,8 @@ int systemdUnitCall(DBusConnection *connection,char *target,char *method,char *p
|
||||
DBusMessageIter iter;
|
||||
DBusMessageIter array_iter;
|
||||
DBusMessageIter variant_iter;
|
||||
DBusMessageIter sub_iter;
|
||||
int ret3;
|
||||
|
||||
char *reply_msg=NULL;
|
||||
int ret1,ret2;
|
||||
@ -301,7 +346,7 @@ int systemdUnitCall(DBusConnection *connection,char *target,char *method,char *p
|
||||
if(ret1 == DBUS_TYPE_STRING)
|
||||
{
|
||||
printf("succesfull received message\narg type %c\n",ret1);
|
||||
printf("[func]reply_msg:%p , &reply_msg:%p , *reply_msg:%p\n", reply_msg,&reply_msg,*reply_msg);
|
||||
printf("[func]reply_msg:%p , &reply_msg:%p , *reply_msg:%d\n", reply_msg,&reply_msg,*reply_msg);
|
||||
dbus_message_iter_get_basic(&reply_iter,&reply_msg);
|
||||
printf("Received message:%s\n",reply_msg);
|
||||
}
|
||||
@ -314,6 +359,7 @@ int systemdUnitCall(DBusConnection *connection,char *target,char *method,char *p
|
||||
{
|
||||
printf("[func]&result:%p , result:%p",&result,result);
|
||||
dbus_message_iter_get_basic(&variant_iter,result);
|
||||
printf("\n--\n*result:%s",*result);
|
||||
}
|
||||
}
|
||||
if(ret1 == DBUS_TYPE_ARRAY)
|
||||
@ -322,13 +368,10 @@ int systemdUnitCall(DBusConnection *connection,char *target,char *method,char *p
|
||||
printf("Array:Elem count %d\n",element_count);
|
||||
//ret2 = dbus_message_iter_get_element_type(&reply_iter);
|
||||
dbus_message_iter_recurse(&reply_iter,&array_iter);
|
||||
int i2=0;
|
||||
ret2 = dbus_message_iter_get_arg_type(&array_iter);
|
||||
while(ret2 != DBUS_TYPE_INVALID)
|
||||
{
|
||||
ret2 = dbus_message_iter_get_arg_type(&array_iter);
|
||||
DBusMessageIter sub_iter;
|
||||
int ret3;
|
||||
dbus_message_iter_recurse(&array_iter,&sub_iter);
|
||||
ret3 = dbus_message_iter_get_arg_type(&sub_iter);
|
||||
while(ret3 != DBUS_TYPE_INVALID)
|
||||
@ -341,17 +384,11 @@ int systemdUnitCall(DBusConnection *connection,char *target,char *method,char *p
|
||||
}
|
||||
ret3=dbus_message_iter_next(&sub_iter);
|
||||
}
|
||||
|
||||
ret2=dbus_message_iter_next(&array_iter);
|
||||
i2++;
|
||||
//printf("i2:%d\n",i2);
|
||||
}
|
||||
}
|
||||
//printf("iter:%c\n",ret1);
|
||||
ret1=dbus_message_iter_next(&reply_iter);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
if(dbus_error_is_set(&error))
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user