Sponsored Content
Full Discussion: Defining Custom Signal
Top Forums Programming Defining Custom Signal Post 302339049 by joha on Wednesday 29th of July 2009 03:02:51 PM
Old 07-29-2009
Here is the code:

Code:
#include <Python.h>
#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>

#define MAX_STR_LEN 255

void catch_int(int sig_num);
void catch_usr1(int sig_num);
void write_run_file();
void run_action(char *line);
void run_script_function(char *fname, char *func);

int main(int argc, char *argv[]) {

  signal(SIGUSR1, catch_usr1);
  /*signal(SIGINT, catch_int);*/

  write_run_file();

  Py_SetProgramName(argv[0]);
  Py_Initialize();

  while(1) {
    printf("Still Alive!\n");
    sleep(1);
  }

  Py_Finalize();

  return 0;

}

void write_run_file() {

  FILE *fh;

  fh = fopen("test.RUN", "w");
  if(fh == NULL) {
    printf("Could not open .RUN file.\n");
    exit(1);
  }

  fprintf(fh, "%i", getpid());

  fclose(fh);

}

void catch_int(int sig_num) {

  exit(1);

}

void catch_usr1(int sig_num) {

  FILE *fh;
  char line[MAX_STR_LEN];

  sigset_t mask_set, old_set;

  /* reset for next time */
  signal(SIGUSR1, catch_usr1);

  /* mask any further signals */
  sigfillset(&mask_set);
  sigprocmask(SIG_SETMASK, &mask_set, &old_set);

  printf("Caught SIGUSR1\n");

  fh = fopen("action", "r");
  if(fh == NULL) {
    printf("Could not open file.\n");
    return;
  }
  
  while(fgets(line, MAX_STR_LEN, fh)) {
    printf("Running %s\n", line);
    run_action(line);
    printf("Finished action\n");
    fflush(stdout);
    fflush(stderr);
  }

  fclose(fh);

  /* restore old signal mask */
  sigprocmask(SIG_SETMASK, &old_set, NULL);

}

void run_action(char *line) {

  char *action, *fname, *func;
  char py_fun[] = "PYFUN";

  action = strtok(line, " ");

  if(strlen(action) == 0) {
    return;
  }

  if(strcmp(action, py_fun) == 0) {
    fname = strtok(NULL, " ");
    func = strtok(NULL, " ");
    run_script_function(fname, func);
  }

}

void run_script_function(char *fname, char *func) {

  FILE *fh;
  char call[MAX_STR_LEN];

  fh = fopen(fname, "r");
  if(fh == NULL) {
    printf("Could not open file %s\n", fname);
    exit(1);
  }

  PyRun_SimpleFile(fh, fname);

  fclose(fh);

  strcpy(call, func);
  strcat(call, "()");

  PyRun_SimpleString(call);

}

It actually happens both before and after receiving the first SIGUSR1. It also happens both with and without the mask.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Defining Variables

I'm trying to define a variable named sin I already have a variable named cos, which has the value "hello" I want sin to have the value of "hellothere", so sin would be something like sin = $cos & "there" but I'm not sure that I know the syntax. Can anyone help? :confused: (4 Replies)
Discussion started by: sailorliones
4 Replies

2. AIX

defining a printer in qconfig

I've got a modified samba script (named it winprint) that I can use to print out to a to a shared Win Network printer from an AIX machine. This is a modification of the samba provided smbprint script changed to work under AIX as the backend for a queue. It does not read a config file I can print... (0 Replies)
Discussion started by: matheeq
0 Replies

3. Shell Programming and Scripting

defining variables

Hey all, I was wondering if someone would take a look at this script I'm working on. I don't know if i have the syntax correct for my variables and if the for loop is written correctly. any assistance would be greatly appreciated. #!/usr/bin/bash ###########################################... (12 Replies)
Discussion started by: em23
12 Replies

4. UNIX and Linux Applications

Problems with defining triggers

I am running the example from the following webpage: MySQL :: MySQL 5.0 Reference Manual :: 12.1.11 CREATE TRIGGER Syntax and the problem is that triggers cannot be defined for some reason: CREATE DATABASE IF NOT EXISTS triggertest; USE triggertest; CREATE TABLE test1(a1 INT); CREATE TABLE... (3 Replies)
Discussion started by: figaro
3 Replies

5. Programming

Defining the inputStream object

Question regarding extend issue What is the reason behind defining the inputStream object in this way... InputStream inputStream = new FileInputStream("c:\\input.txt"); I know that FileInputStream extends InputStream, is there anything else ? I mean we could have define it like ... (0 Replies)
Discussion started by: yahyaaa
0 Replies

6. UNIX for Dummies Questions & Answers

defining variable in .profile

In root dir i have created a .profile file and added variable and assigned a path to it: a = '/dir/dir' export a but when i echo (echo $a) the path or use this variable the value or path not getting displayed. i tried executing the .profile and logging out and logging in, didnt workout. am... (1 Reply)
Discussion started by: abhi_n123
1 Replies

7. UNIX for Dummies Questions & Answers

Defining an alias FreeBSD

I have defined this alias as quick way to find out which mount point to use for a USB drive after inserting it: # alias da='dmesg | grep da | grep MB' However, when invoking it, it states the following: # da da: Command not found. Can someone explain what is the issue here and how it can be... (3 Replies)
Discussion started by: figaro
3 Replies

8. UNIX for Dummies Questions & Answers

Help with defining PATH

Hi All, I have a trivial question but I dont know how to solve it. So basically I'm working on a USB key and I have a directory with some scripts which I use to work on files present in other directories within the USB or sometimes on the main harddisk too. The problem is every time I have to... (3 Replies)
Discussion started by: pawannoel
3 Replies

9. Programming

Problem defining a struct

I have the following code and getting the compilation errors baseLib/DynBaseObj.h:80: error: expected constructor, destructor, or type conversion before ‘(' token baseLib/DynBaseObj.h:89: error: expected constructor, destructor, or type conversion before ‘(' token baseLib/DynBaseObj.h:101:... (0 Replies)
Discussion started by: kristinu
0 Replies

10. UNIX for Dummies Questions & Answers

Defining variable problem

Hi I'd say I'm having this weird problem where my script isn't taking the value off a variable or printing it. My code is like this: set count_C= `grep -c C mols` set count_H= `grep -c H mols` set count_O= `grep -c O mols` sed -i '7,7 s/$/ $count_C $count_O $count_H/g' input It... (8 Replies)
Discussion started by: saleheen
8 Replies
GETLASTLOGX(3)						   BSD Library Functions Manual 					    GETLASTLOGX(3)

NAME
getlastlogx, getutmp, getutmpx, updlastlogx, updwtmpx, utmpxname -- user accounting database functions LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <utmpx.h> struct lastlogx * getlastlogx(const char *fname, uid_t uid, struct lastlogx *ll); void getutmp(const struct utmpx *ux, struct utmp *u); void getutmpx(const struct utmp *u, struct utmpx *ux); int updlastlogx(const char *fname, uid_t uid, struct lastlogx *ll); int updwtmpx(const char *file, const struct utmpx *utx); int utmpxname(const char *fname); DESCRIPTION
The getlastlogx() function looks up the entry for the user with user id uid in the lastlogx(5) file given by fname and returns it in ll. If the provided ll is NULL, the necessary space will be allocated by getlastlogx() and should be free()d by the caller. The getutmp() function fills out the entries in the struct utmp u with the data provided in the struct utmpx ux. getutmpx() does the oppo- site, filling out the entries in the struct utmpx ux with the data provided in the struct utmp u, and initializing all the unknown fields to 0. The sole exception is the ut_type field, which will be initialized to USER_PROCESS. The updlastlogx() function tries to update the information for the user with the user id uid in the lastlogx(5) file given by fname with the data supplied in ll. A struct lastlogx is defined like this: struct lastlogx { struct timeval ll_tv; /* time entry was created */ char ll_line[_UTX_LINESIZE]; /* tty name */ char ll_host[_UTX_HOSTSIZE]; /* host name */ struct sockaddr_storage ll_ss; /* address where entry was made from */ }; All the fields should be filled out by the caller. The updwtmpx() function updates the wtmpx(5) file file with the utmpx(5) entry utx. The utmpxname() function sets the default utmpx(5) database file name to fname. RETURN VALUES
getlastlogx() returns the found entry on success, or NULL if it could not open the database, could not find an entry matching uid in there, or could not allocate the necessary space (in case ll was NULL). utmpxname() returns 1 on success, or 0 if the supplied file name was too long or did not end with 'x'. updlastlogx() and updwtmpx() return 0 on success, or -1 in case the database or file respectively could not be opened or the data not written into it. SEE ALSO
endutxent(3), loginx(3), utmpx(5) HISTORY
The functions getutmp(), getutmpx(), updwtmpx(), and utmpxname() first appeared in Solaris. getlastlogx and updlastlogx first appeared in NetBSD 2.0. BSD
August 26, 2003 BSD
All times are GMT -4. The time now is 10:32 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy