Missing header <fluent-bit.h> while compiling C program - debian-jessie

I'm trying to compile my C program using library Api of fluent-bit , but the header fluent-bit.h is missing and I don't understand why.
I installed fluent-bit using installation guide
Here is my code I want to test:
#include <fluent-bit.h>
int main()
{
int i;
int n;
char tmp[256];
flb_ctx_t *ctx;
int in_ffd;
int out_ffd;
/* Initialize library */
ctx = flb_create();
if (!ctx) {
exit(EXIT_FAILURE);
}
in_ffd = flb_input(ctx, "lib", NULL);
flb_input_set(ctx, in_ffd, "tag", "test", NULL);
out_ffd = flb_output(ctx, "stdout", NULL);
flb_output_set(ctx, out_ffd, "match", "test", NULL);
/* Start the background worker */
flb_start(ctx);
/* Push some data */
for (i = 0; i < 100; i++) {
n = snprintf(tmp, sizeof(tmp) - 1,
"[%f, {\"key\": \"val %i\"}]",
flb_time_now(), i);
flb_lib_push(ctx, in_ffd, tmp, n);
}
flb_stop(ctx);
/* Release Resources */
flb_destroy(ctx);
return 0;
}
Here the error I got:
hello.c:1:24: fatal error: fluent-bit.h: No such file or directory
#include <fluent-bit.h>
^
compilation terminated.

Problem solved, I didn't install fluent-bit and headers properly.
Here was the problem : the headers were missing, so move on to cd /path/to/downloaded/fluent-bit-x.y.z/includes
Then use
sudo cmake .
sudo make install
You'll get an output saying that the headers had been installed on your system.
To make sure fluent-bit is correctly installed too :
cd ..
sudo cmake .
sudo make install
You can now use the fluent-bit API without problems

Related

a problem about ffi function call in openresty

I have some code writen in c, and I wraped these code by lua ffi. When I call my lua file directly by luajit,It works fine.but when I call it from openresty, openresty got an error log: exit on signal 11
nginx version: openresty/1.15.8.1
operating system: debian 9
system luajit: LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2017 Mike Pall.
openresty luajit: LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2017 Mike Pall.
I write a test file to show the different result of two platform
-- filename: gateway/test.lua
local ffi = require "ffi"
ffi.cdef [=[
typedef struct _TrigxNode {
int val;
struct _TrigxNode *char_nodes[64];
struct _TrigxNode *rgx_next;
int rgx_raw_len;
void *re;
char *rgx;
} TrigxNode;
TrigxNode *create_trigx_node();
void trigx_insert(TrigxNode *root, const char *word, int len_word, int val);
int trigx_search(TrigxNode *root, const char *word, int len_word);
void trigx_free(TrigxNode *root);
]=];
local C = ffi.load("trigx_tree")
local node = C.create_trigx_node()
C.trigx_insert(node, '/menu/', 6, 10)
local result = C.trigx_search(node, '/menu/', 6)
if ngx ~= nil then
-- test in openresty
ngx.say("result: " .. result)
else
-- test in luajit
print("result: " .. result)
end
return
http {
init_worker_by_lua_block {
local uuid = require 'resty.jit-uuid'
uuid.seed()
local verbose = false
if verbose then
local dump = require "jit.dump"
dump.on(nil, "./jit.log")
else
local v = require "jit.v"
v.on("./jit.log")
end
require "resty.core"
}
server {
listen 9090;
location / {
content_by_lua_file "gateway/test.lua";
}
location /login {
default_type 'application/json';
content_by_lua_file 'gateway/login.lua';
}
location /wechat-callback {
content_by_lua_file 'gateway/wechat_identify.lua';
}
}
}
when i call the ffi function directly by luajit, luajit -i gateway/test.lua,I can see the right result are printed
but when i try to call test.lua in nginx worker by curl 'localhost:9090/abc',error occured
two pictures show the detail of result
I use calloc rather malloc in source file then problem solved.but still can't find out why this can work in luajit

JeMalloc does not create memory leak dump

I need help with memory profiling using JeMalloc.
I do the following things:
git clone https://github.com/jemalloc/jemalloc
cd jemalloc
./autogen.sh --enable-perf
make dist
make
sudo make install
export MALLOC_CONF=prof_leak:true,lg_prof_sample:0,prof_final:true
export LD_PRELOAD=/usr/local/Cellar/jemalloc/5.1.0/lib/libjemalloc.dylib
Then I run my application:
./some_executed_file
It is 100% that this binary file will use jemalloc
Because when I call
typedef struct {
char *cur;
char *end;
} MallocStatus;
static void GetJemallocStatus(void *mstat_arg, const char *status) {
MallocStatus *mstat = reinterpret_cast<MallocStatus *>(mstat_arg);
size_t status_len = status ? strlen(status) : 0;
size_t buf_size = (size_t)(mstat->end - mstat->cur);
if (!status_len || status_len > buf_size) {
return;
}
snprintf(mstat->cur, buf_size, "%s", status);
mstat->cur += status_len;
}
MallocStatus mstat;
const unsigned int kMallocStatusLen = 1000000;
std::unique_ptr<char[]> buf{new char[kMallocStatusLen + 1]};
mstat.cur = buf.get();
mstat.end = buf.get() + kMallocStatusLen;
je_malloc_stats_print(GetJemallocStatus, &mstat, "");
stats->append(buf.get());
I see JeMalloc statistics.
Regarding to
https://github.com/jemalloc/jemalloc/wiki/Use-Case:-Leak-Checking
I do everything correct - but I still don't see jeprof dump file do analyze memory leaks.
Thanks in advance.
Try adding prof:true,prof_active:true to your MALLOC_CONF, and using --enable-prof flag instead of --enable-perf.

Ruby Gems C extension example not working

I am trying to follow this tutorial on building c extension in ruby gems http://guides.rubygems.org/gems-with-extensions/.
I have the following files:
ext/my_malloc/extconf.rb
require "mkmf"
abort "missing malloc()" unless have_func "malloc"
abort "missing free()" unless have_func "free"
create_makefile "my_malloc/my_malloc"
ext/my_malloc/my_malloc.c
#include <ruby.h>
struct my_malloc {
size_t size;
void *ptr;
};
static void
my_malloc_free(void *p) {
struct my_malloc *ptr = p;
if (ptr->size > 0)
free(ptr->ptr);
}
static VALUE
my_malloc_alloc(VALUE klass) {
VALUE obj;
struct my_malloc *ptr;
obj = Data_Make_Struct(klass, struct my_malloc, NULL, my_malloc_free, ptr);
ptr->size = 0;
ptr->ptr = NULL;
return obj;
}
static VALUE
my_malloc_init(VALUE self, VALUE size) {
struct my_malloc *ptr;
size_t requested = NUM2SIZET(size);
if (0 == requested)
rb_raise(rb_eArgError, "unable to allocate 0 bytes");
Data_Get_Struct(self, struct my_malloc, ptr);
ptr->ptr = malloc(requested);
if (NULL == ptr->ptr)
rb_raise(rb_eNoMemError, "unable to allocate %ld bytes", requested);
ptr->size = requested;
return self;
}
static VALUE
my_malloc_release(VALUE self) {
struct my_malloc *ptr;
Data_Get_Struct(self, struct my_malloc, ptr);
if (0 == ptr->size)
return self;
ptr->size = 0;
free(ptr->ptr);
return self;
}
void
Init_my_malloc(void) {
VALUE cMyMalloc;
cMyMalloc = rb_const_get(rb_cObject, rb_intern("MyMalloc"));
rb_define_alloc_func(cMyMalloc, my_malloc_alloc);
rb_define_method(cMyMalloc, "initialize", my_malloc_init, 1);
rb_define_method(cMyMalloc, "free", my_malloc_release, 0);
}
I do the following to build the extension:
$ cd ext/my_malloc
$ ruby extconf.rb
checking for malloc()... yes
checking for free()... yes
creating Makefile
$ make
compiling my_malloc.c
linking shared-object my_malloc.bundle
$ cd ../..
$ ruby -Ilib:ext -r my_malloc -e "p MyMalloc.new(5).free"
However, I get the following error on the last command:
/Users/Daniel/.rbenv/versions/2.1.6/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- my_malloc (LoadError)
from
/Users/Daniel/.rbenv/versions/2.1.6/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
Note, I'm using rbenv, I've tried running rbenv rehash and I'm running version 2.1.6.
The correct shell command is:
ruby -Iext/my_malloc -r my_malloc -e "p MyMalloc.new(5).free"
The code also has some other mistakes. Correct
rb_raise(rb_eNoMemError, "unable to allocate %ld bytes", requested);
to
rb_raise(rb_eNoMemError, "unable to allocate %" PRIuSIZE " bytes", requested);
since requested has type size_t and not long (PRIuSIZE is non-standard and defined in "ruby/ruby.h"). And
cMyMalloc = rb_const_get(rb_cObject, rb_intern("MyMalloc"));
to
cMyMalloc = rb_define_class("MyMalloc", rb_cObject);
to actually define the MyMalloc class.

not able to read a text file in vs2010 using c .. i am new to vs please help me

i kept my text file at exactly same place where .exe is existing , then also its not working ..
hi this is my code , i kept my text file at exactly same place where .exe is existing , then also its not working ..
hi this is my code , i kept my text file at exactly same place where .exe is existing , then also its not working ..
int main(int argc, _TCHAR* argv[])
{
int result = 0;
char ca, file_name[25];
FILE *fp;
//printf("Enter the name of file you wish to see\n");
gets(file_name);
fp = fopen("sample.txt","r"); // read mode
if( fp == NULL )
{
perror("Error while opening the file.\n");
//exit(EXIT_FAILURE);
}
if( fgets (str, 60, fp)!=NULL )
{
/* writing content to stdout */
puts(str);
}
fclose(fp);
}
Try this , i basically work in C & C++ , i use this code to perform file operation
int main()
{
char filename[10];char extension[5]=".txt";
printf("Enter the name of file you wish to see\n");
gets(filename);
fflush(stdin);
filename[10]='\0';
strcat(filename,extension);
puts(filename);
FILE *p; char acline[80];
p=fopen(filename,"r");
if(p==NULL)
{
printf("%s file is missing\n",filename);system("pause");
}
fseek(p,0,SEEK_SET); // Setting file pointer to beginning of the file
while (!feof(p)) // Detecting end of file
{
fgets(acline,80,p);
puts(acline);
}
printf("\n File end\n");
system("pause");
}
*but while(!feof()) has certain issues see this

AuthorizationExecuteWithPrivileges() as root Error

I am a beginner in cocoa...
I just want to start Apache and other process in my Cocoa App.
Here is my code :
OSStatus myStatus;
AuthorizationFlags myFlags = kAuthorizationFlagDefaults;
AuthorizationRef myAuthorizationRef;
FILE *pipe = NULL;
myStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, myFlags, &myAuthorizationRef);
AuthorizationItem myItems = {kAuthorizationRightExecute, 0, NULL, 0};
AuthorizationRights myRights = {1, &myItems};
myFlags = kAuthorizationFlagDefaults |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize |
kAuthorizationFlagExtendRights;
myStatus = AuthorizationCopyPrivilegedReference (&myAuthorizationRef,kAuthorizationFlagDefaults);
myStatus = AuthorizationCopyRights (myAuthorizationRef,&myRights, NULL, myFlags, NULL );
char *tool = "/usr/sbin/apachectl";
char *args[] = { "start",NULL} ;
myStatus = AuthorizationExecuteWithPrivileges(myAuthorizationRef, tool, kAuthorizationFlagDefaults, args, &pipe);
char c[100];
int n=fread(c,1,100,pipe);
c[n] = '\0';
NSLog(#"%s\n",c);
theResult : This operation requires rootWhen I run a 'whoami', I'm 'root' but when I run a getuid(), I'm '501'...
I try to use setuid(0); But it doesn't set !!
Can you help me? Thanks
I had this exact same problem. AuthorizationExecuteWithPrivileges allows you escalate your permission to root, but does not do it automatically (I guess to preserve the user session or whatever).
I ended up making a generic executable that would be run via AuthorizationExecuteWithPrivileges, and then that executable would setuid to root, and then exec the process you actually want to run as root.
Here's the source for the setuid wrapper executable:
#include <stdio.h>
int main(int argc, char** argv) {
if (argc < 2) {
printf("not enough arguments\n");
return -1;
}
if (0 != setuid(0)) {
printf("setuid failed.\n");
return -3;
}
int i;
char** argvz = (char**)malloc(sizeof(char*) * (argc - 1));
for (i = 1; i < argc; i++) {
argvz[i - 1] = argv[i];
}
execv(argv[1], argvz);
printf("execv returned?\n");
return -2;
}
Then, basically run (calling it via AuthorizationExecuteWithPrivileges) it as:
setuid my-program-to-run and arguments to pass
It will setuid as root, and then run the program in question with the args given.
Note that you must call setuid from AuthorizationExecuteWithPrivileges As only the pid that was created by AuthorizationExecuteWithPrivileges will have escalated privileges (and setuid will exec and replace the process with your own).
I would define access for user with uid=501 access to /usr/sbin/apachectl in /etc/sudoers, and execute "sudo /usr/sbin/apachectl" instead of /usr/sbin/apachectl in the code.
I'm facing exactly the same problem !
This is insane that a whoami returns "root" and a root command returns "root required" !!
The sudoers solution may works but I think we would have to search around the setuid bit, as written here https://github.com/notbrien/OSXSlightlyBetterAuth/blob/master/OSXSlightlyBetterAuth.m#L94

Resources