完成获取系统激活信息dbus接口调用

This commit is contained in:
chenzhikai 2021-11-18 11:45:28 +08:00
parent 50b5dcb328
commit 67d25e6214
7 changed files with 216 additions and 41 deletions

View File

@ -1,5 +1,19 @@
#ifndef KDK_SYSTEM_HW_CPU_H__
#define KDK_SYSTEM_HW_CPU_H__
/**
* @file libkycpu.h
* @author liuyunhe (liuyunhe@kylinos.cn)
* @brief CPU信息
* @version 0.1
* @date 2021-11-17
*
* @copyright Copyright (c) 2021
* @defgroup
* @{
*
*/
/**
* @brief CPU架构
@ -43,4 +57,7 @@ extern unsigned int kdk_cpu_get_corenums();
*/
extern const char* kdk_cpu_get_virt();
#endif
#endif
/**
* @}
*/

View File

@ -1,5 +1,18 @@
#ifndef KDK_SYSTEM_HW_NWCARD_H__
#define KDK_SYSTEM_HW_NWCARD_H__
/**
* @file libkync.h
* @author liuyunhe (liuyunhe@kylinos.cn)
* @brief
* @version 0.1
* @date 2021-11-17
*
* @copyright Copyright (c) 2021
*
* @defgroup
* @{
*
*/
/**
* @brief
@ -70,4 +83,7 @@ extern char** kdk_nc_get_ipv6(const char *nc);
*/
extern inline void kdk_nc_freeall(char **ptr);
#endif
#endif
/**
* @}
*/

View File

@ -1,6 +1,19 @@
#ifndef KDK_SYSTEM_PACKAGES_H__
#define KDK_SYSTEM_PACKAGES_H__
/**
* @file libkypackages.h
* @author liuyunhe (liuyunhe@kylinos.cn)
* @brief
* @version 0.1
* @date 2021-11-17
*
* @copyright Copyright (c) 2021
* @defgroup
* @{
*
*
*/
// typedef enum kdk_packageStatus {
// UNINIT,
// INSTALLED,
@ -70,4 +83,7 @@ extern char* kdk_package_get_version(const char *name);
*/
extern int kdk_package_is_installed(const char *name, const char *version);
#endif
#endif
/**
* @}
*/

View File

@ -1,6 +1,19 @@
#ifndef KDK_SYSTEM_PROC_H__
#define KDK_SYSTEM_PROC_H__
/**
* @file libkyrtinfo.h
* @author liuyunhe (liuyunhe@kylinos.cn)
* @brief
* @version 0.1
* @date 2021-11-17
*
* @copyright Copyright (c) 2021
* @defgroup 使
* @{
*
*/
/**
* @brief
*
@ -96,4 +109,7 @@ extern float kdk_rti_get_cpu_current_usage();
*/
extern int kdk_rti_get_uptime(unsigned int *day, unsigned int *hour, unsigned int *min, unsigned int *sec);
#endif
#endif
/**
* @}
*/

View File

@ -96,8 +96,14 @@ char* kdk_system_get_version(bool verbose)
return sysversion;
}
int kdk_system_get_activationStatus()
int kdk_system_get_activationStatus(int *status_error_num,int *date_error_num)
{
if (NULL == status_error_num | NULL == date_error_num)
{
klog_err("激活状态接口调用失败:参数错误\n");
return -1;
}
int res = 0;
#ifdef __linux__
#ifdef _KYLIN_ACTIVATION_H_
@ -140,75 +146,165 @@ int kdk_system_get_activationStatus()
}
if (NULL == conn) {
exit(1);
return -1;
}
DBusMessage* msg;
DBusMessage* status_msg=NULL;
DBusMessage* status_pending_msg=NULL;
DBusMessage* date_msg=NULL;
DBusMessage* date_pending_msg=NULL;
DBusMessageIter args;
DBusPendingCall* pending;
DBusPendingCall* status_pending=NULL;
DBusPendingCall* date_pending=NULL;
int stat;
int err_nu;
msg = dbus_message_new_method_call("org.freedesktop.activation", // target for the method call
//获取激活状态
status_msg = dbus_message_new_method_call("org.freedesktop.activation", // target for the method call
"/org/freedesktop/activation", // object to call on
"org.freedesktop.activation.interface", // interface to call on
"status"); // method name
if (!dbus_connection_send_with_reply (conn, msg, &pending, -1)) { // -1 is default timeout
fprintf(stderr, "Out Of Memory!\n");
if (!dbus_connection_send_with_reply (conn, status_msg, &status_pending, -1)) { // -1 is default timeout
klog_err("status_msgdbus_connection_send_with_reply调用失败\n");
return -1;
}
if (NULL == pending){
fprintf(stderr, "Pending Call Null\n");
if (NULL == status_pending){
klog_err("status_pending返回值为null\n");
return -1;
}
dbus_connection_flush(conn);
dbus_message_unref(status_msg);
dbus_pending_call_block(pending);
msg = dbus_pending_call_steal_reply(pending);
if (NULL == msg) {
fprintf(stderr, "Reply Null\n");
//接收返回值
dbus_pending_call_block(status_pending);
status_pending_msg = dbus_pending_call_steal_reply(status_pending);
if (NULL == status_pending_msg) {
klog_err("status_pending_msg获取激活状态接口返回值异常\n");
return -1;
}
if (!dbus_message_iter_init(msg, &args)){
klog_err("未收到激活状态接口返回值\n");
//释放status_pending
dbus_pending_call_unref(status_pending);
if (!dbus_message_iter_init(status_pending_msg, &args)){
klog_err("status_pending_msgdbus_message_iter_init获取激活状态接口返回值异常\n");
return -1;
}
else
dbus_message_iter_get_basic(&args, &stat);
if (!dbus_message_iter_next(&args)){
klog_err("未收到激活状态接口返回值\n");
klog_err("status_pending_msgdbus_message_iter_next获取激活状态接口返回值异常\n");
return -1;
}
else
dbus_message_iter_get_basic(&args, &err_nu);
klog_debug("激活状态:%d错误码%d\n",stat,err_nu);
if (err_nu == 0)
{
dbus_message_iter_get_basic(&args, status_error_num);
klog_debug("激活状态:%d错误码%d\n",stat,*status_error_num);
//释放status_pending_msg
dbus_message_unref(status_pending_msg);
if (*status_error_num == 0)
{
char *date_stat="";
if (stat == 0)
{
return 0;
//获取技术服务日期
date_msg = dbus_message_new_method_call("org.freedesktop.activation", // target for the method call
"/org/freedesktop/activation", // object to call on
"org.freedesktop.activation.interface", // interface to call on
"date"); // method name
if (!dbus_connection_send_with_reply (conn, date_msg, &date_pending, -1)) { // -1 is default timeout
klog_err("date_msgdbus_connection_send_with_reply 接口调用失败\n");
return -1;
}
if (NULL == date_pending){
klog_err("date_pending返回值为null\n");
return -1;
}
dbus_connection_flush(conn);
dbus_message_unref(date_msg);
//接收返回值
dbus_pending_call_block(date_pending);
date_pending_msg = dbus_pending_call_steal_reply(date_pending);
if (NULL == date_pending_msg) {
klog_err(stderr, "date_pending_msgReply Null\n");
return -1;
}
if (!dbus_message_iter_init(date_pending_msg, &args)){
dbus_message_unref(date_pending_msg);
klog_err("date_pending_msgdbus_message_iter_init获取技术服务日期接口返回值异常\n");
return -1;
}
else
dbus_message_iter_get_basic(&args, &date_stat);
if (!dbus_message_iter_next(&args)){
dbus_message_unref(date_pending_msg);
klog_err("date_pending_msgdbus_message_iter_next获取技术服务日期接口返回值异常\n");
return -1;
}
else
dbus_message_iter_get_basic(&args, date_error_num);
klog_debug("技术服务日期:%s错误码%d\n",date_stat,*date_error_num);
if (*date_error_num == 0)
{
if (date_stat)
{
dbus_message_unref(date_pending_msg);
return 2;
}
else
{
dbus_message_unref(date_pending_msg);
return 0;
}
}
}
else if(stat == 1)
else if (stat == 1)
{
return 1;
return stat;
}
else if (stat == 2)
else
{
return 2;
}
// /usr/include/kylin-activation.h规定了错误码
klog_err("技术服务状态:技术服务日期:%s错误码%d\n",date_stat,*date_error_num);
return -1;
}
}
else{
// /usr/include/kylin-activation.h规定了错误码
klog_err("激活状态:激活状态码:%d错误码%d\n",stat,*status_error_num);
return -1;
}
#endif // _KYLIN_ACTIVATION_H_
#endif // __linux__
return res;
data_free:
if (status_msg)
{
dbus_message_unref(status_msg);
}
if (status_pending_msg)
{
dbus_message_unref(status_pending_msg);
}
if (date_msg)
{
dbus_message_unref(date_msg);
}
if (date_pending_msg)
{
dbus_message_unref(date_pending_msg);
}
if (status_pending)
{
dbus_pending_call_unref(status_pending);
}
if (date_pending)
{
dbus_pending_call_unref(date_pending);
}
}
char* kdk_system_get_serialNumber()

View File

@ -10,6 +10,9 @@
*
* @copyright Copyright (c) 2021
*
* @defgroup
* @{
*
*/
#include <string.h>
@ -40,9 +43,12 @@ extern char* kdk_system_get_version(bool verbose);
/**
* @brief
*
* @return int 2/01-1
* @param status_error_num
* @param date_error_num
*
* @return int 201-1
*/
extern int kdk_system_get_activationStatus();
extern int kdk_system_get_activationStatus(int *status_error_num, int *date_error_num);
/**
* @brief
@ -65,4 +71,8 @@ extern char* kdk_system_get_kernelVersion();
*/
extern char* kdk_system_get_currentUser();
#endif // KYSDK_SYSTEM_SYSINFO_H__
#endif // KYSDK_SYSTEM_SYSINFO_H__
/**
* @}
*/

View File

@ -22,7 +22,11 @@ int main()
res = kdk_system_get_serialNumber();
printf("序列号:%s\n", res);
free(res);
int act = kdk_system_get_activationStatus();
int p;
int d;
int act = kdk_system_get_activationStatus(&p,&d);
printf("--------------\n");
printf("激活状态码:%d\n",act);
printf("激活状态:%s\n", act == 1 ? "已激活" : act == 0 ? "未激活" : "已过期");
return 0;
}