ssize_t my_write(int fd, const void *buf, size_t count) { //获取真实地址 typedef ssize_t (*REALFUNC)(int fd, const void *buf, size_t count); REALFUNC real_write = (REALFUNC)get_real_func("write"); //忽略标准输入/输出 if(fd >=0 && fd <= 2) { return real_write(fd, buf, count); } //通过fd获取文件名并过滤 char filename[256]; getfilename(fd, filename, sizeof(filename)); char *fname = "data.txt"; //判断文件名是否为"data.txt",如果不是则调用原函数 if(strncmp(filename, fname, strlen(fname)) != 0) { return real_write(fd, buf, count); } //考虑到printf实际内部会调用write函数,避免循环调用,执行printf前先detach之后再attach detach_func("write"); printf("[%s] fd:%d(%s), buf:%s\n", __FUNCTION__, fd, filename, buf); attach_func("write", (void *)my_write); errno = ENOSPC; return -1; }
/* * Temporary work around until we have real reference counting * * We keep the bits about calling dlil_if_release (which should be * called recycle) transparent by calling it from our if_free function * pointer. We have to keep the client's original detach function * somewhere so we can call it. */ static void ifnet_kpi_free(ifnet_t ifp) { ifnet_detached_func detach_func = ifp->if_kpi_storage; if (detach_func != NULL) detach_func(ifp); if (ifp->if_broadcast.length > sizeof (ifp->if_broadcast.u.buffer)) { FREE(ifp->if_broadcast.u.ptr, M_IFADDR); ifp->if_broadcast.u.ptr = NULL; } dlil_if_release(ifp); }