Commit fe3fe3b2 authored by dsteklof@us.ibm.com's avatar dsteklof@us.ibm.com Committed by Greg KH
Browse files

[PATCH] new version of libsysfs patch

Here's the patch applying the latest libsysfs.
	- adds the latest libsysfs code to udev
		* new code includes dlist implementation, a generic linked list
			implementation. Needed our own because LGPL
		* rearranged structures
		* provided more functions for accessing directory and attributes
	- gets rid of ->directory->path references in namedev.c
	- replaces sysfs_get_value_from_attributes with sysfs_get_classdev_attr
parent 3370fb21
# Makefile for libsysfs.a
# Copyright (c) International Business Machines Corp., 2003
H_INCLUDE=../include
H_INCLUDE=.
LIB_INCLUDE=.
OBJS=sysfs_bus.o sysfs_class.o sysfs_device.o sysfs_dir.o sysfs_driver.o \
sysfs_utils.o
sysfs_utils.o dlist.o
# Install directory
# Options
CFLAGS=-O2 -Wall -ansi -g
CFLAGS=-O2 -Wall -g
# sysfs library
LIBSYSFS=libsysfs.a
......@@ -38,5 +38,8 @@ sysfs_driver.o: sysfs_driver.c
sysfs_utils.o: sysfs_utils.c
$(CC) -I$(H_INCLUDE) -I$(LIB_INCLUDE) $(CFLAGS) -c sysfs_utils.c
dlist.o: dlist.c
$(CC) -I$(H_INCLUDE) -I$(LIB_INCLUDE) $(CFLAGS) -c dlist.c
clean:
$(RM) *.o *~ core $(LIBSYSFS)
/*
* dlist.c
*
* Copyright (C) 2003 Eric J Bohm
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021110307 USA
*
*/
/* Double linked list implementation.
* You allocate the data and give dlist the pointer.
* If your data is complex set the dlist->del_func to a an appropriate
* delete function. Otherwise dlist will just use free.
*/
#include "dlist.h"
/*
* Return pointer to node at marker.
* else null if no nodes.
*/
inline void *dlist_mark(Dlist *list)
{
if(list->marker!=NULL)
return(list->marker->data);
else
return(NULL);
}
/*
* Set marker to start.
*/
inline void dlist_start(Dlist *list)
{
list->marker=list->head;
}
/*
* Set marker to end.
*/
inline void dlist_end(Dlist *list)
{
list->marker=list->head;
}
/* internal use function
* quickie inline to consolidate the marker movement logic
* in one place
*
* when direction true it moves marker after
* when direction false it moves marker before.
* return pointer to data at new marker
* if nowhere to move the marker in desired direction return null
*/
inline void *_dlist_mark_move(Dlist *list,int direction)
{
if(direction)
{
if( list->marker->next!=NULL)
list->marker=list->marker->next;
else
return(NULL);
}
else
{
if( list->marker->prev!=NULL)
list->marker=list->marker->prev;
else
return(NULL);
}
if(list->marker!=list->head)
return(list->marker->data);
else
return(NULL);
}
/*
* Create new linked list to store nodes of datasize.
* return null if list cannot be created.
*/
Dlist *dlist_new(size_t datasize)
{
Dlist *list=NULL;
if((list=malloc(sizeof(Dlist))))
{
list->marker=NULL;
list->count=0L;
list->data_size=datasize;
list->del_func=free;
list->head=&(list->headnode);
list->head->prev=NULL;
list->head->next=NULL;
list->head->data=NULL;
}
return(list);
}
/*
* Create new linked list to store nodes of datasize set list
* data node delete function to the passed in del_func
* return null if list cannot be created.
*/
Dlist *dlist_new_with_delete(size_t datasize,void (*del_func)(void*))
{
Dlist *list=NULL;
list=dlist_new(datasize);
if(list!=NULL)
list->del_func=del_func;
return(list);
}
/*
* remove marker node from list
* call data_delete function on data if registered.
* otherwise call free.
* when direction true it moves marker after
* when direction false it moves marker before.
* free marker node
* return nothing.
*/
void dlist_delete(Dlist *list,int direction)
{
if((list->marker != list->head)&&(list->marker!=NULL))
{
DL_node *corpse;
corpse=list->marker;
_dlist_mark_move(list,direction);
if(list->head->next==corpse)
list->head->next=corpse->next;
if(list->head->prev==corpse)
list->head->prev=corpse->prev;
if(corpse->prev!=NULL) //should be impossible
corpse->prev->next=corpse->next;
if(corpse->next!=NULL) //should be impossible
corpse->next->prev=corpse->prev;
list->del_func(corpse->data);
list->count--;
free(corpse);
}
}
/*
* Insert node containing data at marker.
* If direction true it inserts after.
* If direction false it inserts before.
* move marker to inserted node
* return pointer to inserted node
*/
void *dlist_insert(Dlist *list,void *data,int direction)
{
DL_node *new_node=NULL;
if(list==NULL || data==NULL)
return(NULL);
if(list->marker==NULL) //in case the marker ends up unset
list->marker=list->head;
if((new_node=malloc(sizeof(DL_node))))
{
new_node->data=data;
new_node->prev=NULL;
new_node->next=NULL;
list->count++;
if(list->head->next==NULL) //no l
{
list->head->next=list->head->prev=new_node;
new_node->prev=list->head;
new_node->next=list->head;
}
else if(direction)
{
new_node->next=list->marker->next;
new_node->prev=list->marker;
list->marker->next->prev=new_node;
list->marker->next=new_node;
}
else
{
new_node->prev=list->marker->prev;
new_node->next=list->marker;
list->marker->prev->next=new_node;
list->marker->prev=new_node;
}
list->marker=new_node;
}
else
{
return(NULL);
}
return(list->marker->data);
}
/*
* Remove DL_node from list without deallocating data.
* if marker == killme .
* when direction true it moves marker after
* when direction false it moves marker before.
* to previous if there is no next.
*/
void *_dlist_remove(Dlist *list,DL_node *killme,int direction)
{
if(killme!=NULL)
{
void *killer_data=killme->data;
// take care of head and marker pointers.
if(list->marker==killme)
_dlist_mark_move(list,direction);
if(killme ==list->head->next)
list->head->next=killme->next;
if(killme==list->head->prev)
list->head->prev=killme->prev;
// remove from list
if(killme->prev !=NULL)
killme->prev->next=killme->next;
if(killme->next !=NULL)
killme->next->prev=killme->prev;
list->count--;
free(killme);
return(killer_data);
}
else
return (NULL);
}
/*
* Insert node containing data after end.
*/
void dlist_push(Dlist *list,void *data)
{
list->marker=list->head->prev;
dlist_insert(list,data,1);
}
/*
* Insert node containing data at start.
*/
void dlist_unshift(Dlist *list,void *data)
{
list->marker=list->head->next;
dlist_insert(list,data,0);
}
/*
* Remove end node from list.
* Return pointer to data in removed node.
* Null if no nodes.
*/
void *dlist_pop(Dlist *list)
{
return(_dlist_remove(list,list->head->prev,0));
}
/*
* Remove start node from list.
* Return pointer to data in removed node.
* Null if no nodes.
*/
void *dlist_shift(Dlist *list)
{
return(_dlist_remove(list,list->head->next,1));
}
/*
* destroy the list freeing all memory
*/
void dlist_destroy(Dlist *list)
{
if(list !=NULL)
{
dlist_start(list);
dlist_next(list);
while (dlist_mark(list)) {
dlist_delete(list,1);
}
free(list);
}
}
/**
* Return void pointer to list_data element matching comp function criteria
* else null
* Does not move the marker.
*/
void *dlist_find_custom(struct dlist *list, void *target, int (*comp)(void *, void *))
{
/* test the comp function on each node */
struct dl_node *nodepointer;
dlist_for_each_nomark(list,nodepointer)
if(comp(target,nodepointer->data))
return(nodepointer->data);
return(NULL);
}
/**
* Apply the node_operation function to each data node in the list
*/
void dlist_transform(struct dlist *list, void (*node_operation)(void *))
{
struct dl_node *nodepointer;
dlist_for_each_nomark(list,nodepointer)
node_operation(nodepointer->data);
}
/**
* insert new into list in sorted order
* sorter function in form int sorter(new,ith)
* must return 1 for when new should go before ith
* else 0
* return pointer to inserted node
* NOTE: assumes list is already sorted
*/
void *dlist_insert_sorted(struct dlist *list, void *new, int (*sorter)(void *, void *))
{
for(dlist_start(list),dlist_next(list); \
list->marker!=list->head && !sorter(new,list->marker->data);dlist_next(list));
return(dlist_insert_before(list,new));
}
/*
* dlist.h
*
* Copyright (C) 2003 Eric J Bohm
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef _DLIST_H_
#define _DLIST_H_
/* Double linked list header.
* navigate your list with DLIST_PREV and DLIST_NEXT. These are macros
* so function call overhead is minimized.
* Supports perl style push, pop, shift, unshift list semantics.
* You allocate the data and give dlist the pointer. If your data is
* complex set the dlist->del_func to a an appropriate delete using
* dlist_new_with_delete. Your delete function must match
(void * )(del(void *)
*Otherwise dlist will just use free.
* NOTE: The small amount of pain involved in doing that allows us to
* avoid copy in copy out semantics.
* Dlist uses an internal mark pointer to keep track of where you are
* in the list.
* insert and delete take a directional parameter. Where direction
* corresponds to the direction in which you want the list to go.
* true direction corresponded to progressing forward in the last
* false to regressing in the list.
* so a dlist_insert(yourlist,item,1) will insert it after the mark
* so a dlist_insert(yourlist,item,0) will insert it before the mark
* any insert will move the mark to the new node regardless of the direction.
* Just use the dlist_(insert|delete)_(before|after) macros if you do not want
* to think about it.
*/
#include <malloc.h>
typedef struct dl_node {
struct dl_node *prev;
struct dl_node *next;
void *data;
} DL_node;
typedef struct dlist {
DL_node *marker;
unsigned long count;
size_t data_size;
void (*del_func)(void *);
DL_node headnode;
DL_node *head;
} Dlist;
Dlist *dlist_new(size_t datasize);
Dlist *dlist_new_with_delete(size_t datasize,void (*del_func)(void*));
void *_dlist_mark_move(Dlist *list,int direction);
void *dlist_mark(Dlist *);
void dlist_start(Dlist *);
void dlist_end(Dlist *);
void *dlist_insert(Dlist *,void *,int) ;
void *dlist_insert_sorted(struct dlist *list, void *new, int (*sorter)(void *, void *));
void dlist_delete(Dlist *,int);
void dlist_push(Dlist *,void *);
void dlist_unshift(Dlist *,void *);
void *dlist_pop(Dlist *);
void *dlist_shift(Dlist *);
void dlist_destroy(Dlist *);
void *dlist_find_custom(struct dlist *list, void *target, int (*comp)(void *, void *));
void dlist_transform(struct dlist *list, void (*node_operation)(void *));
/*
* _dlist_remove is for internal use only
* _dlist_mark_move is for internal use only
*/
void *_dlist_remove(struct dlist *,struct dl_node *,int );
#define dlist_prev(A) _dlist_mark_move((A),0)
#define dlist_next(A) _dlist_mark_move((A),1)
#define dlist_insert_before(A,B) dlist_insert((A),(B),0)
#define dlist_insert_after(A,B) dlist_insert((A),(B),1)
#define dlist_delete_before(A) dlist_delete((A),0)
#define dlist_delete_after(A) dlist_delete((A),1)
/**
* provide for loop header which iterates the mark from start to end
* list: the dlist pointer, use dlist_mark(list) to get iterator
*/
#define dlist_for_each(list) \
for(dlist_start(list),dlist_next(list); \
(list)->marker!=(list)->head;dlist_next(list))
/**
* provide for loop header which iterates the mark from end to start
* list: the dlist pointer, use dlist_mark(list) to get iterator
*/
#define dlist_for_each_rev(list) \
for(dlist_end(list),dlist_prev(list); \
(list)->marker!=(list)->head;dlist_prev(list))
/**
* provide for loop header which iterates through the list without moving mark
* list: the dlist_pointer
* iterator: dl_node pointer to iterate
*/
#define dlist_for_each_nomark(list,iterator) \
for((iterator)=(list)->head->next; (iterator)!=(list)->head; \
(iterator)=(iterator)->next)
/**
* provide for loop header which iterates through the list without moving mark
* in reverse
* list: the dlist_pointer
* iterator: dl_node pointer to iterate
*/
#define dlist_for_each_nomark_rev(list,iterator) \
for((iterator)=(list)->head->prev; (iterator)!=(list)->head; \
(iterator)=(iterator)->prev)
/**
* provide for loop header which iterates through the list providing a
* data iterator
* list: the dlist pointer
* data_iterator: the pointer of type datatype to iterate
* datatype: actual type of the contents in the dl_node->data
*/
#define dlist_for_each_data(list,data_iterator,datatype) \
for(dlist_start(list), (data_iterator)=(datatype *) dlist_next(list); \
(list)->marker!=(list)->head;(data_iterator)=(datatype *) dlist_next(list))
/**
* provide for loop header which iterates through the list providing a
* data iterator in reverse
* list: the dlist pointer
* data_iterator: the pointer of type datatype to iterate
* datatype: actual type of the contents in the dl_node->data
*/
#define dlist_for_each_data_rev(list,data_iterator,datatype) \
for(dlist_end(list), (data_iterator)=(datatype *) dlist_prev(list); \
(list)->marker!=(list)->head;(data_iterator)=(datatype *) dlist_prev(list))
/**
* provide for loop header which iterates through the list providing a
* data iterator without moving the mark
* list: the dlist pointer
* iterator: the dl_node pointer to iterate
* data_iterator: the pointer of type datatype to iterate
* datatype: actual type of the contents in the dl_node->data
*/
#define dlist_for_each_data_nomark(list,iterator,data_iterator,datatype) \
for((iterator)=(list)->head->next, (data_iterator)=(datatype *) (iterator)->data; \
(iterator)!=(list)->head;(iterator)=(iterator)->next,(data_iterator)=(datatype *) (iterator))
/**
* provide for loop header which iterates through the list providing a
* data iterator in reverse without moving the mark
* list: the dlist pointer
* iterator: the dl_node pointer to iterate
* data_iterator: the pointer of type datatype to iterate
* datatype: actual type of the contents in the dl_node->data
*/
#define dlist_for_each_data_nomark_rev(list,iterator, data_iterator,datatype) \
for((iterator)=(list)->head->prev, (data_iterator)=(datatype *) (iterator)->data; \
(iterator)!=(list)->head;(iterator)=(iterator)->prev,(data_iterator)=(datatype *) (iterator))
#endif /* _DLIST_H_ */
......@@ -3,7 +3,7 @@
*
* Header Definitions for libsysfs
*
* Copyright (C) 2003 International Business Machines, Inc.
* Copyright (C) IBM Corp. 2003
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
......@@ -24,6 +24,7 @@
#define _LIBSYSFS_H_
#include <sys/types.h>
#include "dlist.h"
/*
* Generic #defines go here..
......@@ -32,11 +33,17 @@
#define SYSFS_PROC_MNTS "/proc/mounts"
#define SYSFS_BUS_DIR "/bus"
#define SYSFS_CLASS_DIR "/class"
#define SYSFS_BLOCK_DIR "/block"
#define SYSFS_DEVICES_DIR "/devices"
#define SYSFS_DEVICES_NAME "devices"
#define SYSFS_DRIVERS_DIR "/drivers"
#define SYSFS_DRIVERS_NAME "drivers"
#define SYSFS_NAME_ATTRIBUTE "name"
#define SYSFS_UNKNOWN "unknown"
/* Some "block" subsystem specific #defines */
#define SYSFS_QUEUE_NAME "queue"
#define SYSFS_IOSCHED_NAME "iosched"
#define SYSFS_PATH_MAX 255
#define SYSFS_NAME_LEN 50
......@@ -46,65 +53,84 @@
#define SYSFS_METHOD_STORE 0x02 /* attr can be changed by user */
struct sysfs_attribute {
struct sysfs_attribute *next;
char path[SYSFS_PATH_MAX];
char *value;
unsigned char *value;
unsigned short len; /* value length */
unsigned short method; /* show and store */
unsigned char name[SYSFS_NAME_LEN];
unsigned char path[SYSFS_PATH_MAX];
};
struct sysfs_dlink {
struct sysfs_dlink *next;
char name[SYSFS_NAME_LEN];
struct sysfs_directory *target;
struct sysfs_link {
unsigned char name[SYSFS_NAME_LEN];
unsigned char path[SYSFS_PATH_MAX];
unsigned char target[SYSFS_PATH_MAX];
};
struct sysfs_directory {
struct sysfs_directory *next;
char path[SYSFS_PATH_MAX];
struct sysfs_directory *subdirs;
struct sysfs_dlink *links;
struct sysfs_attribute *attributes;
struct dlist *subdirs;
struct dlist *links;
struct dlist *attributes;
unsigned char name[SYSFS_NAME_LEN];
unsigned char path[SYSFS_PATH_MAX];
};