Gio::DBus 中创建复合类型的参数

首先,需要知道两个基本知识:

  • 每一个参数都需要被包装为 Variant。
  • 全体参数需要被整体包装成一个 Tuple。

即使只有一个参数也需要包装为 Tuple,例如一个 string:

1const auto arg1 = Glib::Variant<Glib::ustring>::create("hello world");
2Glib::VariantContainerBase args = Glib::VariantContainerBase::create_tuple(arg1);

Linux C API 获取广播地址

广播地址通常为网段的最后一个地址

通过对一个 socket 进行 ioctl(SIOCGIFBRDADDR) 即可获取广播地址。

1struct ifreq req;
2strcpy(req.ifr_name, "网卡名");
3ioctl(sock, SIOCGIFBRDADDR, &req);

Linux C API 获取本机的 IP 地址

方法1:遍历网卡

1#include <stdio.h>      
2#include <ifaddrs.h>
3#include <arpa/inet.h>
4
5int main (void) 

Linux Hook 技术

Hook 是一种覆盖重写进程中符号的技术,在 Linux 中,通过环境变量 LD_PRELOAD 预加载包含同名符号的动态库即可实现。

覆盖 malloc 和 free 检查内存泄漏

1// 文件名: memcheck.c
2// 编译命令: gcc -o memcheck.so memcheck.c --shared -fPIC
3#define _GNU_SOURCE

怎样 detach 子进程

通过 fork 创建进程后,父进程可以使用 waitpid 来获取子进程的结束状态。 如果子进程先于父进程退出,而父进程没有调用 waitpid 的话,子进程将一直存续下去以保持结束状态可以被获取。 此时,子进程被称为僵尸进程

如果子进程没有被调用 waitpid 而父进程退出了,此时子进程被称为孤儿进程,它会被 init进程(PID为1) 领养并调用waitpid回收。

有时我们并不关心子进程的结束状态,希望像 pthread_detach 一样示释放子进程。有以下两种方法可以实现:

调用 PAM 进行认证

1// gcc main.c -lpam
2
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include <unistd.h>
7#include <termio.h>

通过 ALSA 显示音频设备的声道

1// apt install libasound2-dev
2// LDFLAGS := -lasound
3#include <stdio.h>
4#include <stdlib.h>
5#include <alsa/asoundlib.h>
6
7int main(int argc, char* argv[])

通过 PulseAudio 播放和录制声音

1// libs: -lpulse 
2#include <stdio.h>
3#include <stdlib.h>
4#include <errno.h>
5#include <string.h>
6#include <stdbool.h>
7#include <pulse/pulseaudio.h>

通过 V4L2 读取摄像头

1
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <errno.h>
6#include <stdint.h>
7#include <stdbool.h>

APT 代理配置

最近遇到 APT 连不上 docker 源的问题,需要配置 APT 的代理。

创建 /etc/apt/apt.conf.d/50proxy.conf 文件,填入代理配置:

1Acquire::http::Proxy "http://ADDRESS:PORT";