AIX: Welcome to the ODM

Written By :

Category :

aix

,

C

Posted On :

Share This :

AIX friend, aren’t you proud to be a big boy and to tease your linux kiddos with your big ODM? Let’s make them cry then. Today we are not going to f@ck around. We use C language baby, yeah! Like many things in C, we start defining a structure, but this structure is a required IBM stuff and it looks this way, store that in a file called create_class.cre:
class mystuff {
  char		Customer[100];
  int		ActivationDate;
  int		ExpirationDate;
  char		Key[100];
  char		Product[100];
  char		Version[100];
};
Then, let’s define some functions in an odm.h file:
#include <stdio.h>
#include <stdlib.h>
#include <odmi.h>
#include <errno.h>
#include <strings.h>
#include <time.h>
#include <unistd.h>

#include "create_class.h"

typedef struct mystuff mystuff_t;

#define TRIAL_PERIOD  2592000

int create_odm_class();
int write_odm();
int check_odm_class();
mystuff_t *read_odm();

And then the implementation of these functions in odm.c:
#include "odm.h"

#ifdef __PPC__

int create_odm_class() {
  char *error_message;

  if(DEBUG>1) printf("Creation of class mystuff.\n");
  syslog(LOG_DEBUG, "Creation of ODM class.");
  if(odm_create_class(mystuff_CLASS)!=0) {
    odm_err_msg ( odmerrno, &error_message );
    if(DEBUG>1) printf("Problem accessing ODM. %s", error_message);
    syslog(LOG_ERR, "Problem accessing ODM. %s", error_message);
    return(1);
  }

  return(0);
}

int write_odm() {
  struct mystuff *data2write;
  data2write=(struct mystuff*)malloc(sizeof(struct mystuff));

  time_t now = time(NULL);

  data2write->ActivationDate = now;

  odm_add_obj(mystuff_CLASS, data2write);
  return(0);
}

int check_odm_class() {
  char *error_message;

  if(odm_mount_class("mystuff")==(CLASS_SYMBOL)-1) {
    odm_err_msg ( odmerrno, &error_message );
    if(DEBUG>1) printf("[check_odm_class] Error searching for class mystuff #%d: %s\n", odmerrno, error_message);
    syslog(LOG_ERR, "[check_odm_class] Error searching for class mystuff #%d: %s", odmerrno, error_message);
    if(odmerrno==0) {
      create_odm_class();
      write_odm();
    } else {
      if(DEBUG) printf("[create_odm_class] Class mystuff created successfully.\n");
      syslog(LOG_DEBUG, "[create_odm_class] Class mystuff created successfully.");
    }
  }

  return(0);
}

mystuff_t *read_odm() {
  struct tm ts;
  char buf[80];
  mystuff_t *data2read;
  data2read=(struct mystuff*)malloc(sizeof(struct mystuff));

  odm_get_first(mystuff_CLASS, NULL, data2read);
  if(DEBUG>1) {
    ts = *localtime((const time_t *)&data2read->ActivationDate);
    strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
    printf("\n[read_odm] Activation Date: %ld [%s]\n", data2read->ActivationDate, buf);
    printf("[read_odm] Customer: %s\n", data2read->Customer);
    printf("[read_odm] Key: %s\n", data2read->Key);
    printf("\n");
  }
  syslog(LOG_DEBUG, "[read_odm] Activation Date: %ld; Key: %s", data2read->ActivationDate, data2read->Key);

  return(data2read);
}
Alright, that looks sexy but what do I do with all this mess buddy!? That:
int main(int argc, char **argv) {
  check_odm_class();
  return(0);
}
And this is already the time for compilation baby! Here is your Makefile:
CC=gcc
OS ?= $(shell uname -s)
CFLAGS=-g -ggdb -Wall 
LFLAGS=-L/opt/freeware/lib
LDFLAGS=-lodm -lerrlog -lm

SRC:= src

all: mystuff

src/create_class.o:
	/usr/bin/odmcreate -h src/create_class.cre
	$(CC) -g -ggdb  -c src/create_class.c -o src/create_class.o

%.o: $(SRC)/%.c
	$(CC) $(CFLAGS) -c $< -o $@

mystuff: src/create_class.o src/odm.o
	$(CC) $(CFLAGS) $(LFLAGS) $(LDFLAGS) src/main.c src/create_class.o src/odm.o -o build/mystuff 

clean:
	\rm -f build/mystuff src/*.o .toc src/create_class.?

If you do not understand it all, do not call me, I am in holidays. So that means you can literally store anything in the ODM and access and read it back anytime with this set of functions. Be aware that anyone with ODM skills can access them also using the set of get_odm commands. So for sensitive informations, you will need to encrypt that. Have fun! I’ll see you next time.

Leave a Reply